init project
This commit is contained in:
commit
d63cc8d9f5
33 files changed
+1529
No files matched your search
Vendored
+146
@@ -0,0 +1,146 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"git.xiaotuo.co/micro/skeleton/internal/logger"
|
||||
"github.com/go-redis/redis"
|
||||
)
|
||||
|
||||
type BaseCache struct {
|
||||
rs *redis.Client
|
||||
isLog bool
|
||||
}
|
||||
|
||||
func (c *BaseCache) exists(ctx context.Context, key string) (bool, error) {
|
||||
value, err := c.rs.Exists(key).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_exists", "key: %s, not_found", key)
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
if c.isLog {
|
||||
logger.Errorf(ctx, "redis_exists", "key: %s, err: %v", key, err)
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
exists := value > 0
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_exists", "key: %s, exists", key)
|
||||
}
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func (c *BaseCache) get(ctx context.Context, key string) (string, error) {
|
||||
value, err := c.rs.Get(key).Result()
|
||||
if err != nil {
|
||||
if c.isLog {
|
||||
logger.Errorf(ctx, "redis_get", "key: %s, err: %v", key, err)
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_get", "key: %s, value: %s", key, value)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (c *BaseCache) set(ctx context.Context, key string, value interface{}, expire time.Duration) (string, error) {
|
||||
result, err := c.rs.Set(key, value, expire).Result()
|
||||
if err != nil {
|
||||
if c.isLog {
|
||||
logger.Errorf(ctx, "redis_set", "key: %s, value: %v, err: %v", key, value, err)
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_set", "key: %s, value: %v", key, value)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (c *BaseCache) del(ctx context.Context, key string) (int64, error) {
|
||||
affect, err := c.rs.Del(key).Result()
|
||||
if err != nil {
|
||||
if c.isLog {
|
||||
logger.Errorf(ctx, "redis_del", "key: %s, err: %v", key, err)
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_del", "key: %s, affect: %v", key, affect)
|
||||
}
|
||||
return affect, nil
|
||||
}
|
||||
|
||||
func (c *BaseCache) push(ctx context.Context, key string, values ...interface{}) (int64, error) {
|
||||
affect, err := c.rs.LPush(key, values...).Result()
|
||||
if err != nil {
|
||||
if c.isLog {
|
||||
logger.Errorf(ctx, "redis_lpush", "key: %s, err: %v", key, err)
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_lpush", "key: %s, values: %v, affect: %v", key, values, affect)
|
||||
}
|
||||
return affect, nil
|
||||
}
|
||||
|
||||
func (c *BaseCache) pop(ctx context.Context, key string) (string, error) {
|
||||
value, err := c.rs.RPop(key).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
if c.isLog {
|
||||
logger.Tracef(ctx, "redis_rpop", "key: %s null", key)
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
if err != nil {
|
||||
if c.isLog {
|
||||
logger.Errorf(ctx, "redis_rpop", "key: %s, err: %v", key, err)
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_rpop", "key: %s, value: %v", key, value)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (c *BaseCache) length(ctx context.Context, key string) (int64, error) {
|
||||
l, err := c.rs.LLen(key).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_llen", "key: %s null", key)
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
if err != nil {
|
||||
if c.isLog {
|
||||
logger.Errorf(ctx, "redis_llen", "key: %s, err: %v", key, err)
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_llen", "key: %s, length: %v", key, l)
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
|
||||
func (c *BaseCache) lock(ctx context.Context, key, value string, expire time.Duration) (bool, error) {
|
||||
result, err := c.rs.SetNX(key, value, expire).Result()
|
||||
if err != nil {
|
||||
if c.isLog {
|
||||
logger.Errorf(ctx, "redis_setnx", "key: %s, err: %v", key, err)
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
if c.isLog {
|
||||
logger.Debugf(ctx, "redis_setnx", "key: %s, result: %t", key, result)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
package cache
|
||||
Reference in new issue
Block a user