Kevin Wan 3 éve
szülő
commit
974ba5c9aa
2 módosított fájl, 25 hozzáadás és 7 törlés
  1. 13 5
      core/stores/cache/cachenode.go
  2. 12 2
      core/stores/cache/cachenode_test.go

+ 13 - 5
core/stores/cache/cachenode.go

@@ -123,7 +123,8 @@ func (c cacheNode) SetWithExpire(key string, val interface{}, expire time.Durati
 }
 
 // SetWithExpireCtx sets the cache with key and v, using given expire.
-func (c cacheNode) SetWithExpireCtx(ctx context.Context, key string, val interface{}, expire time.Duration) error {
+func (c cacheNode) SetWithExpireCtx(ctx context.Context, key string, val interface{},
+	expire time.Duration) error {
 	data, err := jsonx.Marshal(val)
 	if err != nil {
 		return err
@@ -145,7 +146,8 @@ func (c cacheNode) Take(val interface{}, key string, query func(val interface{})
 
 // TakeCtx takes the result from cache first, if not found,
 // query from DB and set cache using c.expiry, then return the result.
-func (c cacheNode) TakeCtx(ctx context.Context, val interface{}, key string, query func(val interface{}) error) error {
+func (c cacheNode) TakeCtx(ctx context.Context, val interface{}, key string,
+	query func(val interface{}) error) error {
 	return c.doTake(ctx, val, key, query, func(v interface{}) error {
 		return c.SetCtx(ctx, key, v)
 	})
@@ -153,13 +155,15 @@ func (c cacheNode) TakeCtx(ctx context.Context, val interface{}, key string, que
 
 // TakeWithExpire takes the result from cache first, if not found,
 // query from DB and set cache using given expire, then return the result.
-func (c cacheNode) TakeWithExpire(val interface{}, key string, query func(val interface{}, expire time.Duration) error) error {
+func (c cacheNode) TakeWithExpire(val interface{}, key string, query func(val interface{},
+	expire time.Duration) error) error {
 	return c.TakeWithExpireCtx(context.Background(), val, key, query)
 }
 
 // TakeWithExpireCtx takes the result from cache first, if not found,
 // query from DB and set cache using given expire, then return the result.
-func (c cacheNode) TakeWithExpireCtx(ctx context.Context, val interface{}, key string, query func(val interface{}, expire time.Duration) error) error {
+func (c cacheNode) TakeWithExpireCtx(ctx context.Context, val interface{}, key string,
+	query func(val interface{}, expire time.Duration) error) error {
 	expire := c.aroundDuration(c.expiry)
 	return c.doTake(ctx, val, key, func(v interface{}) error {
 		return query(v, expire)
@@ -239,7 +243,11 @@ func (c cacheNode) doTake(ctx context.Context, v interface{}, key string,
 		return nil
 	}
 
-	// got the result from previous ongoing query
+	// got the result from previous ongoing query.
+	// why not call IncrementTotal at the beginning of this function?
+	// because a shared error is returned, and we don't want to count.
+	// for example, if the db is down, the query will be failed, we count
+	// the shared errors with one db failure.
 	c.stat.IncrementTotal()
 	c.stat.IncrementHit()
 

+ 12 - 2
core/stores/cache/cachenode_test.go

@@ -88,7 +88,7 @@ func TestCacheNode_InvalidCache(t *testing.T) {
 	assert.Equal(t, miniredis.ErrKeyNotFound, err)
 }
 
-func TestCacheNode_Take(t *testing.T) {
+func TestCacheNode_SetWithExpire(t *testing.T) {
 	store, clean, err := redistest.CreateRedis()
 	assert.Nil(t, err)
 	defer clean()
@@ -100,8 +100,18 @@ func TestCacheNode_Take(t *testing.T) {
 		lock:           new(sync.Mutex),
 		unstableExpiry: mathx.NewUnstable(expiryDeviation),
 		stat:           NewStat("any"),
-		errNotFound:    errTestNotFound,
+		errNotFound:    errors.New("any"),
 	}
+	assert.NotNil(t, cn.SetWithExpire("key", make(chan int), time.Second))
+}
+
+func TestCacheNode_Take(t *testing.T) {
+	store, clean, err := redistest.CreateRedis()
+	assert.Nil(t, err)
+	defer clean()
+
+	cn := NewNode(store, syncx.NewSingleFlight(), NewStat("any"), errTestNotFound,
+		WithExpiry(time.Second), WithNotFoundExpiry(time.Second))
 	var str string
 	err = cn.Take(&str, "any", func(v interface{}) error {
 		*v.(*string) = "value"