mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
parent
7b3c3de35e
commit
b88ba14597
@ -21,6 +21,7 @@ func NewRing(n int) *Ring {
|
|||||||
func (r *Ring) Add(v interface{}) {
|
func (r *Ring) Add(v interface{}) {
|
||||||
r.lock.Lock()
|
r.lock.Lock()
|
||||||
defer r.lock.Unlock()
|
defer r.lock.Unlock()
|
||||||
|
|
||||||
r.elements[r.index%len(r.elements)] = v
|
r.elements[r.index%len(r.elements)] = v
|
||||||
r.index++
|
r.index++
|
||||||
}
|
}
|
||||||
|
@ -31,17 +31,6 @@ func TestRingMore(t *testing.T) {
|
|||||||
assert.ElementsMatch(t, []interface{}{6, 7, 8, 9, 10}, elements)
|
assert.ElementsMatch(t, []interface{}{6, 7, 8, 9, 10}, elements)
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkRingAdd(b *testing.B) {
|
|
||||||
ring := NewRing(500)
|
|
||||||
b.RunParallel(func(pb *testing.PB) {
|
|
||||||
for pb.Next() {
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
ring.Add(i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRingAdd(t *testing.T) {
|
func TestRingAdd(t *testing.T) {
|
||||||
ring := NewRing(5051)
|
ring := NewRing(5051)
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
@ -57,3 +46,14 @@ func TestRingAdd(t *testing.T) {
|
|||||||
wg.Wait()
|
wg.Wait()
|
||||||
assert.Equal(t, 5050, len(ring.Take()))
|
assert.Equal(t, 5050, len(ring.Take()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkRingAdd(b *testing.B) {
|
||||||
|
ring := NewRing(500)
|
||||||
|
b.RunParallel(func(pb *testing.PB) {
|
||||||
|
for pb.Next() {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
ring.Add(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
5
core/stores/cache/cache.go
vendored
5
core/stores/cache/cache.go
vendored
@ -14,6 +14,7 @@ type (
|
|||||||
Cache interface {
|
Cache interface {
|
||||||
DelCache(keys ...string) error
|
DelCache(keys ...string) error
|
||||||
GetCache(key string, v interface{}) error
|
GetCache(key string, v interface{}) error
|
||||||
|
IsNotFound(err error) bool
|
||||||
SetCache(key string, v interface{}) error
|
SetCache(key string, v interface{}) error
|
||||||
SetCacheWithExpire(key string, v interface{}, expire time.Duration) error
|
SetCacheWithExpire(key string, v interface{}, expire time.Duration) error
|
||||||
Take(v interface{}, key string, query func(v interface{}) error) error
|
Take(v interface{}, key string, query func(v interface{}) error) error
|
||||||
@ -91,6 +92,10 @@ func (cc cacheCluster) GetCache(key string, v interface{}) error {
|
|||||||
return c.(Cache).GetCache(key, v)
|
return c.(Cache).GetCache(key, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cc cacheCluster) IsNotFound(err error) bool {
|
||||||
|
return err == cc.errNotFound
|
||||||
|
}
|
||||||
|
|
||||||
func (cc cacheCluster) SetCache(key string, v interface{}) error {
|
func (cc cacheCluster) SetCache(key string, v interface{}) error {
|
||||||
c, ok := cc.dispatcher.Get(key)
|
c, ok := cc.dispatcher.Get(key)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
8
core/stores/cache/cache_test.go
vendored
8
core/stores/cache/cache_test.go
vendored
@ -42,6 +42,10 @@ func (mc *mockedNode) GetCache(key string, v interface{}) error {
|
|||||||
return mc.errNotFound
|
return mc.errNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (mc *mockedNode) IsNotFound(err error) bool {
|
||||||
|
return err == mc.errNotFound
|
||||||
|
}
|
||||||
|
|
||||||
func (mc *mockedNode) SetCache(key string, v interface{}) error {
|
func (mc *mockedNode) SetCache(key string, v interface{}) error {
|
||||||
data, err := json.Marshal(v)
|
data, err := json.Marshal(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -117,7 +121,7 @@ func TestCache_SetDel(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for i := 0; i < total; i++ {
|
for i := 0; i < total; i++ {
|
||||||
var v int
|
var v int
|
||||||
assert.Equal(t, errPlaceholder, c.GetCache(fmt.Sprintf("key/%d", i), &v))
|
assert.True(t, c.IsNotFound(c.GetCache(fmt.Sprintf("key/%d", i), &v)))
|
||||||
assert.Equal(t, 0, v)
|
assert.Equal(t, 0, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -155,7 +159,7 @@ func TestCache_OneNode(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for i := 0; i < total; i++ {
|
for i := 0; i < total; i++ {
|
||||||
var v int
|
var v int
|
||||||
assert.Equal(t, errPlaceholder, c.GetCache(fmt.Sprintf("key/%d", i), &v))
|
assert.True(t, c.IsNotFound(c.GetCache(fmt.Sprintf("key/%d", i), &v)))
|
||||||
assert.Equal(t, 0, v)
|
assert.Equal(t, 0, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
5
core/stores/cache/cachenode.go
vendored
5
core/stores/cache/cachenode.go
vendored
@ -82,6 +82,11 @@ func (c cacheNode) GetCache(key string, v interface{}) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsNotFound checks if the given error is the defined errNotFound.
|
||||||
|
func (c cacheNode) IsNotFound(err error) bool {
|
||||||
|
return err == c.errNotFound
|
||||||
|
}
|
||||||
|
|
||||||
// SetCache sets the cache with key and v, using c.expiry.
|
// SetCache sets the cache with key and v, using c.expiry.
|
||||||
func (c cacheNode) SetCache(key string, v interface{}) error {
|
func (c cacheNode) SetCache(key string, v interface{}) error {
|
||||||
return c.SetCacheWithExpire(key, v, c.aroundDuration(c.expiry))
|
return c.SetCacheWithExpire(key, v, c.aroundDuration(c.expiry))
|
||||||
|
8
core/stores/cache/cachenode_test.go
vendored
8
core/stores/cache/cachenode_test.go
vendored
@ -115,8 +115,8 @@ func TestCacheNode_TakeNotFound(t *testing.T) {
|
|||||||
err = cn.Take(&str, "any", func(v interface{}) error {
|
err = cn.Take(&str, "any", func(v interface{}) error {
|
||||||
return errTestNotFound
|
return errTestNotFound
|
||||||
})
|
})
|
||||||
assert.Equal(t, errTestNotFound, err)
|
assert.True(t, cn.IsNotFound(err))
|
||||||
assert.Equal(t, errTestNotFound, cn.GetCache("any", &str))
|
assert.True(t, cn.IsNotFound(cn.GetCache("any", &str)))
|
||||||
val, err := store.Get("any")
|
val, err := store.Get("any")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, `*`, val)
|
assert.Equal(t, `*`, val)
|
||||||
@ -125,8 +125,8 @@ func TestCacheNode_TakeNotFound(t *testing.T) {
|
|||||||
err = cn.Take(&str, "any", func(v interface{}) error {
|
err = cn.Take(&str, "any", func(v interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
assert.Equal(t, errTestNotFound, err)
|
assert.True(t, cn.IsNotFound(err))
|
||||||
assert.Equal(t, errTestNotFound, cn.GetCache("any", &str))
|
assert.True(t, cn.IsNotFound(cn.GetCache("any", &str)))
|
||||||
|
|
||||||
store.Del("any")
|
store.Del("any")
|
||||||
var errDummy = errors.New("dummy")
|
var errDummy = errors.New("dummy")
|
||||||
|
Loading…
Reference in New Issue
Block a user