1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-08 14:42:26 +08:00
This commit is contained in:
houseme
2024-10-07 16:09:19 +08:00
committed by GitHub
127 changed files with 1064 additions and 1039 deletions

View File

@@ -22,12 +22,16 @@ func TestMemcache(t *testing.T) {
exists := mem.IsExist("unknown-key")
assert.Equal(t, false, exists)
name := mem.Get("username").(string)
name, ok := mem.Get("username").(string)
if !ok {
t.Error("get Error")
}
if name != "" {
if name != "silenceper" {
t.Error("get Error")
}
}
data := mem.Get("unknown-key")
assert.Nil(t, data)

14
cache/redis.go vendored
View File

@@ -37,12 +37,12 @@ func NewRedis(ctx context.Context, opts *RedisOpts) *Redis {
return &Redis{ctx: ctx, conn: conn}
}
// SetConn 设置conn
// SetConn 设置 conn
func (r *Redis) SetConn(conn redis.UniversalClient) {
r.conn = conn
}
// SetRedisCtx 设置redis ctx 参数
// SetRedisCtx 设置 redis ctx 参数
func (r *Redis) SetRedisCtx(ctx context.Context) {
r.ctx = ctx
}
@@ -71,15 +71,17 @@ func (r *Redis) SetContext(ctx context.Context, key string, val interface{}, tim
return r.conn.SetEX(ctx, key, val, timeout).Err()
}
// IsExist 判断key是否存在
// IsExist 判断 key 是否存在
func (r *Redis) IsExist(key string) bool {
return r.IsExistContext(r.ctx, key)
}
// IsExistContext 判断key是否存在
// IsExistContext 判断 key 是否存在
func (r *Redis) IsExistContext(ctx context.Context, key string) bool {
result, _ := r.conn.Exists(ctx, key).Result()
result, err := r.conn.Exists(ctx, key).Result()
if err != nil {
return false
}
return result > 0
}

5
cache/redis_test.go vendored
View File

@@ -35,7 +35,10 @@ func TestRedis(t *testing.T) {
t.Error("IsExist Error")
}
name := redis.Get(key).(string)
name, ok := redis.Get(key).(string)
if !ok {
t.Error("get Error")
}
if name != val {
t.Error("get Error")
}