diff --git a/mycache/cache2go/cachetable.go b/mycache/cache2go/cachetable.go index 5e40f32..7ca5ddc 100644 --- a/mycache/cache2go/cachetable.go +++ b/mycache/cache2go/cachetable.go @@ -275,6 +275,24 @@ func (table *CacheTable) NotFoundAdd(key interface{}, lifeSpan time.Duration, da return true } +// Value returns an item from the cache and marks it to be kept alive. You can +// pass additional arguments to your DataLoader callback function. +func (table *CacheTable) Refresh(key interface{}, lifeSpan time.Duration) error { + table.RLock() + r, ok := table.items[key] + table.RUnlock() + + if ok { + // Update access counter and timestamp. + table.Add(key, lifeSpan, r.data) + r.KeepAlive() + // r.lifeSpan = lifeSpan + return nil + } + + return ErrKeyNotFound +} + // Value returns an item from the cache and marks it to be kept alive. You can // pass additional arguments to your DataLoader callback function. func (table *CacheTable) Value(key interface{}, args ...interface{}) (*CacheItem, error) { diff --git a/mycache/mycache.go b/mycache/mycache.go index 52c3c91..2fb3d9b 100644 --- a/mycache/mycache.go +++ b/mycache/mycache.go @@ -24,6 +24,7 @@ type CacheIFS interface { TryLock(key interface{}, value interface{}, lifeSpan time.Duration) (err error) // 试着加锁 Unlock(key interface{}) (err error) // 解锁 GetKeyS(key interface{}) ([]string, error) // 查询所有key + Refresh(key interface{}, lifeSpan time.Duration) error // 更新时间 } // MyCache 内存缓存 @@ -111,6 +112,14 @@ func (mc *MyCache) Unlock(key interface{}) (err error) { return mc.Delete(key) } +// Refresh 更新时间 +func (mc *MyCache) Refresh(key interface{}, lifeSpan time.Duration) error { + mc.mtx.Lock() + defer mc.mtx.Unlock() + + return mc.cache.Refresh(key, lifeSpan) +} + func encodeValue(value interface{}) []byte { data, _ := serializing.Encode(value) return data diff --git a/myredis/myredis.go b/myredis/myredis.go index e697ef5..6369e85 100644 --- a/myredis/myredis.go +++ b/myredis/myredis.go @@ -22,6 +22,7 @@ type RedisDial interface { Do(commandName string, args ...interface{}) (reply interface{}, err error) // 一次操作 TryLock(key interface{}, value interface{}, lifeSpan time.Duration) (err error) // 试着加锁 Unlock(key interface{}) (err error) // 解锁 + Refresh(key interface{}, lifeSpan time.Duration) error // 更新时间 } // DefaultConf ... diff --git a/myredis/myredis_test.go b/myredis/myredis_test.go index a521cdc..deaee4f 100644 --- a/myredis/myredis_test.go +++ b/myredis/myredis_test.go @@ -16,7 +16,14 @@ func Test_cache(t *testing.T) { fmt.Println(err) aaa := "ccccc" - res.Add("aaaa", aaa, 20*time.Second) + res.Add("aaaa", aaa, 2*time.Second) + res.Refresh("aaaa", -1) + for i := 0; i < 10; i++ { + aaa = "" + err := res.Value("aaaa", &aaa) + fmt.Println(err, aaa) + time.Sleep(1 * time.Second) + } res.Close() res.Add("bbbb", aaa, 0) res.Close() diff --git a/myredis/pool.go b/myredis/pool.go index 2f48421..1344076 100644 --- a/myredis/pool.go +++ b/myredis/pool.go @@ -175,3 +175,34 @@ func (mc *redisConPool) Do(commandName string, args ...interface{}) (reply inter func (mc *redisConPool) Close() (err error) { return nil } + +// Refresh 更新时间 +func (mc *redisConPool) Refresh(key interface{}, lifeSpan time.Duration) error { + expire := "EXPIRE" + var args []interface{} + args = append(args, mc.getKey(key)) + if lifeSpan > 0 { + if usePrecise(lifeSpan) { + expire = "PEXPIRE" + args = append(args, formatMs(lifeSpan)) + } else { + expire = "EXPIRE" + args = append(args, formatSec(lifeSpan)) + } + } else if lifeSpan == keepTTL { + expire = "EXPIRE" + args = append(args, 2147483647) + } + + con := mc.GetRedisClient() + defer con.Close() + repy, err := mc.DO(con, expire, args...) + if mc.conf.isLog { + mylog.Info(redis.String(repy, err)) + } + + if err != nil { + mylog.Error(err) + } + return err +}