This commit is contained in:
xxj
2024-01-13 14:10:05 +08:00
parent 7f3129f228
commit da00414b8a
5 changed files with 67 additions and 1 deletions

View File

@@ -275,6 +275,24 @@ func (table *CacheTable) NotFoundAdd(key interface{}, lifeSpan time.Duration, da
return true 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 // Value returns an item from the cache and marks it to be kept alive. You can
// pass additional arguments to your DataLoader callback function. // pass additional arguments to your DataLoader callback function.
func (table *CacheTable) Value(key interface{}, args ...interface{}) (*CacheItem, error) { func (table *CacheTable) Value(key interface{}, args ...interface{}) (*CacheItem, error) {

View File

@@ -24,6 +24,7 @@ type CacheIFS interface {
TryLock(key interface{}, value interface{}, lifeSpan time.Duration) (err error) // 试着加锁 TryLock(key interface{}, value interface{}, lifeSpan time.Duration) (err error) // 试着加锁
Unlock(key interface{}) (err error) // 解锁 Unlock(key interface{}) (err error) // 解锁
GetKeyS(key interface{}) ([]string, error) // 查询所有key GetKeyS(key interface{}) ([]string, error) // 查询所有key
Refresh(key interface{}, lifeSpan time.Duration) error // 更新时间
} }
// MyCache 内存缓存 // MyCache 内存缓存
@@ -111,6 +112,14 @@ func (mc *MyCache) Unlock(key interface{}) (err error) {
return mc.Delete(key) 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 { func encodeValue(value interface{}) []byte {
data, _ := serializing.Encode(value) data, _ := serializing.Encode(value)
return data return data

View File

@@ -22,6 +22,7 @@ type RedisDial interface {
Do(commandName string, args ...interface{}) (reply interface{}, err error) // 一次操作 Do(commandName string, args ...interface{}) (reply interface{}, err error) // 一次操作
TryLock(key interface{}, value interface{}, lifeSpan time.Duration) (err error) // 试着加锁 TryLock(key interface{}, value interface{}, lifeSpan time.Duration) (err error) // 试着加锁
Unlock(key interface{}) (err error) // 解锁 Unlock(key interface{}) (err error) // 解锁
Refresh(key interface{}, lifeSpan time.Duration) error // 更新时间
} }
// DefaultConf ... // DefaultConf ...

View File

@@ -16,7 +16,14 @@ func Test_cache(t *testing.T) {
fmt.Println(err) fmt.Println(err)
aaa := "ccccc" 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.Close()
res.Add("bbbb", aaa, 0) res.Add("bbbb", aaa, 0)
res.Close() res.Close()

View File

@@ -175,3 +175,34 @@ func (mc *redisConPool) Do(commandName string, args ...interface{}) (reply inter
func (mc *redisConPool) Close() (err error) { func (mc *redisConPool) Close() (err error) {
return nil 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
}