Ai
1 Star 0 Fork 32

wangyfbe/go-admin-core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
redis.go 1.58 KB
一键复制 编辑 原始数据 按行查看 历史
lwnmengjing 提交于 2021-04-19 16:58 +08:00 . feat :sparkles:扩展queue locker
package cache
import (
"time"
"github.com/go-redis/redis/v7"
)
// NewRedis redis模式
func NewRedis(client *redis.Client, options *redis.Options) (*Redis, error) {
if client == nil {
client = redis.NewClient(options)
}
r := &Redis{
client: client,
}
err := r.connect()
if err != nil {
return nil, err
}
return r, nil
}
// Redis cache implement
type Redis struct {
client *redis.Client
}
func (*Redis) String() string {
return "redis"
}
// connect connect test
func (r *Redis) connect() error {
var err error
_, err = r.client.Ping().Result()
return err
}
// Get from key
func (r *Redis) Get(key string) (string, error) {
return r.client.Get(key).Result()
}
// Set value with key and expire time
func (r *Redis) Set(key string, val interface{}, expire int) error {
return r.client.Set(key, val, time.Duration(expire)*time.Second).Err()
}
// Del delete key in redis
func (r *Redis) Del(key string) error {
return r.client.Del(key).Err()
}
// HashGet from key
func (r *Redis) HashGet(hk, key string) (string, error) {
return r.client.HGet(hk, key).Result()
}
// HashDel delete key in specify redis's hashtable
func (r *Redis) HashDel(hk, key string) error {
return r.client.HDel(hk, key).Err()
}
// Increase
func (r *Redis) Increase(key string) error {
return r.client.Incr(key).Err()
}
func (r *Redis) Decrease(key string) error {
return r.client.Decr(key).Err()
}
// Set ttl
func (r *Redis) Expire(key string, dur time.Duration) error {
return r.client.Expire(key, dur).Err()
}
// GetClient 暴露原生client
func (r *Redis) GetClient() *redis.Client {
return r.client
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/wangyfbe/go-admin-core.git
git@gitee.com:wangyfbe/go-admin-core.git
wangyfbe
go-admin-core
go-admin-core
v1.10.3

搜索帮助