4 Star 18 Fork 3

cristiane / micro-mall-pay

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
cache.go 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
雨化田 提交于 2021-09-03 22:38 . 优化app启动流程
package cache
import (
"crypto/md5"
"fmt"
"github.com/gomodule/redigo/redis"
)
const nameSpace = "micro-mall-pay_"
type Cache struct {
}
func NewCache() *Cache {
return &Cache{}
}
func (c *Cache) Set(redisConn redis.Conn, key, value string, expire int32) error {
if expire < 0 {
expire = 0
}
_, err := redisConn.Do("SETEX", c.BuildKey(key), expire, value)
//defer redisConn.Close()
return err
}
func (c *Cache) Get(redisConn redis.Conn, key string) (string, error) {
reply, err := redisConn.Do("GET", c.BuildKey(key))
rsp, err := redis.String(reply, err)
//defer redisConn.Close()
return rsp, err
}
func (c *Cache) HGet(redisConn redis.Conn, key, field string) (string, error) {
reply, err := redisConn.Do("HGET", c.BuildKey(key), field)
//defer redisConn.Close()
data, err := redis.String(reply, err)
return data, nil
}
func (c *Cache) HSet(redisConn redis.Conn, key, field, value string) error {
_, err := redisConn.Do("HSET", c.BuildKey(key), field, value)
//defer redisConn.Close()
return err
}
func (c *Cache) HDelete(redisConn redis.Conn, key, field string) error {
_, err := redisConn.Do("HDEL", c.BuildKey(key), field)
//defer redisConn.Close()
return err
}
func (c *Cache) Delete(redisConn redis.Conn, key string) error {
//defer redisConn.Close()
_, err := redisConn.Do("DEL", c.BuildKey(key))
return err
}
func (c *Cache) Exists(redisConn redis.Conn, key string) (bool, error) {
//defer redisConn.Close()
r, err := redisConn.Do("EXISTS", c.BuildKey(key))
return redis.Bool(r, err)
}
func (c *Cache) BuildKey(key string) string {
if len(key) > 32 {
m := md5.New()
m.Write([]byte(key))
key = fmt.Sprintf("%x", m.Sum(nil))
}
return nameSpace + key
}
Go
1
https://gitee.com/cristiane/micro-mall-pay.git
git@gitee.com:cristiane/micro-mall-pay.git
cristiane
micro-mall-pay
micro-mall-pay
5f6f923dbc21

搜索帮助

53164aa7 5694891 3bd8fe86 5694891