packages/net/mosdns/patches/010-redis_cache-fixed-cache-could-store-a-key-with-zero-ttl.patch
2022-01-07 06:21:47 +00:00

31 lines
1.1 KiB
Diff

From 0565ec359eb16b7cf10dcab9dd8e5be221ed9539 Mon Sep 17 00:00:00 2001
From: IrineSistiana <49315432+IrineSistiana@users.noreply.github.com>
Date: Thu, 6 Jan 2022 16:50:46 +0800
Subject: [PATCH] redis_cache: fixed cache could store a key with zero ttl
---
dispatcher/pkg/cache/redis_cache/redis_cache.go | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
--- a/dispatcher/pkg/cache/redis_cache/redis_cache.go
+++ b/dispatcher/pkg/cache/redis_cache/redis_cache.go
@@ -61,14 +61,16 @@ func (r *RedisCache) Get(ctx context.Con
}
func (r *RedisCache) Store(ctx context.Context, key string, v []byte, storedTime, expirationTime time.Time) error {
- if time.Now().After(expirationTime) {
+ now := time.Now()
+ ttl := expirationTime.Sub(now)
+ if ttl <= 0 { // For redis, zero ttl means the key has no expiration time.
return nil
}
data := packRedisData(storedTime, expirationTime, v)
defer pool.ReleaseBuf(data)
- return r.client.Set(ctx, key, data, expirationTime.Sub(time.Now())).Err()
+ return r.client.Set(ctx, key, data, ttl).Err()
}
// Close closes the redis client.