代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。