安装go-redis 库
go get github.com/gomodule/redigo/redis
创建链接池
package gredis
import (
"time"
"github.com/gomodule/redigo/redis"
)
var RedisConn *redis.Pool
func Setup() error {
RedisConn = &redis.Pool{
// Maximum number of idle connections in the pool.(连接池idle连接数)
MaxIdle: 30,
// Maximum number of connections allocated by the pool at a given time.(连接池在给定时间内分配的最大连接数)
// When zero, there is no limit on the number of connections in the pool(当连接池中的连接数为0时,没有限制)
MaxActive:30 #
// Close connections after remaining idle for this duration. If the value
// is zero, then idle connections are not closed. Applications should set
// the timeout to a value less than the server's timeout.
IdleTimeout: 200 * time.Second
// If Wait is true and the pool is at the MaxActive limit, then Get() waits
// for a connection to be returned to the pool before returning.
Wait:true
// Close connections older than this duration. If the value is zero, then
// the pool does not close connections based on age.
MaxConnLifetime 200 * time.Second
//创建和配置链接
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
return nil, err
}
if setting.RedisSetting.Password != "" {
if _, err := c.Do("AUTH", ""); err != nil {
c.Close()
return nil, err
}
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
return nil
}
string
package gredis
import (
"encoding/json"
"github.com/gomodule/redigo/redis"
)
// Set a key/value
func StringSet(key string, data interface{}) error {
conn := RedisConn.Get()
defer conn.Close()
value, err := json.Marshal(data)
if err != nil {
return err
}
_, err = conn.Do("SET", key, value)
if err != nil {
return err
}
return nil
}
// SET if Not eXists
func StringSetNX(key string, data interface{}, seconds int) error {
conn := RedisConn.Get()
defer conn.Close()
value, err := json.Marshal(data)
if err != nil {
return err
}
_, err = conn.Do("SETNX", key, value, seconds)
if err != nil {
return err
}
return nil
}
//SET key value EXPIRE key seconds
func StringSetEX(key string, data interface{}, seconds int) error {
conn := RedisConn.Get()
defer conn.Close()
value, err := json.Marshal(data)
if err != nil {
return err
}
_, err = conn.Do("SETEX", key, value, seconds)
if err != nil {
return err
}
return nil
}
// Get get a key value
func StringGet(key string) ([]byte, error) {
conn := RedisConn.Get()
defer conn.Close()
reply, err := redis.Bytes(conn.Do("GET", key))
if err != nil {
return nil, err
}
return reply, nil
}
//incr key
func StringIncr(key string) error {
conn := RedisConn.Get()
defer conn.Close()
_, err := conn.Do("INCR", key)
if err != nil {
return err
}
return nil
}
//incr key by value
func StringIncrBy(key string, value int) error {
conn := RedisConn.Get()
defer conn.Close()
_, err := conn.Do("INCRBY", key, value)
if err != nil {
return err
}
return nil
}
//DECR key
func StringDecr(key string) error {
conn := RedisConn.Get()
defer conn.Close()
_, err := conn.Do("DECR", key)
if err != nil {
return err
}
return nil
}
//DECR key by value
func StringDecrBy(key string, value int) error {
conn := RedisConn.Get()
defer conn.Close()
_, err := conn.Do("DECRBY", key, value)
if err != nil {
return err
}
return nil
}
hash
package gredis
import (
"encoding/json"
"github.com/gomodule/redigo/redis"
)
// Set a HASH key/value
func HashSet(key string, field string, data interface{}) error {
conn := RedisConn.Get()
defer conn.Close()
value, err := json.Marshal(data)
if err != nil {
return err
}
_, err = conn.Do("HSET", key, value)
if err != nil {
return err
}
return nil
}
// Get get a HASH key value
func HashGet(key string, field string) ([]byte, error) {
conn := RedisConn.Get()
defer conn.Close()
reply, err := redis.Bytes(conn.Do("HGET", key, field))
if err != nil {
return nil, err
}
return reply, nil
}
// Delete delete a HASH kye
func HashKeyDel(key string) (int, error) {
conn := RedisConn.Get()
defer conn.Close()
return redis.Int(conn.Do("HDEL", key))
}
//incr value by hash key
func HashIncrBy(key string, field string, value int) (int, error) {
conn := RedisConn.Get()
defer conn.Close()
return redis.Int(conn.Do("HINCRBY", key, field, value))
}
应用
添加缓存基类用于规范前缀及缓存名称
package cache
type Cache struct {
Prefix string
Name string
}
//get cache key
func GetCacheKey(cache Cache) string {
return cache.Prefix+":"+cache.Name
}
继承基类,并声明缓存前缀及名称
package cache
import (
"ginIMApi/packages/gredis"
)
func NewApplyFriend() *Cache {
return &Cache{
Prefix: "im",
Name: "apply-friend",
}
}
func (c Cache) IncrApplyFriendUnRead(friendId string) (int, error) {
return gredis.HashIncrBy(GetCacheKey(c), friendId, 1)
}
func (c Cache) GetApplyFriendUnRead(friendId string) ([]byte, error) {
return gredis.HashGet(GetCacheKey(c), friendId)
}
小结
- 感觉写起来还是有点别扭,可能未能真正的了解及应用go 语言特性。有应用不当的请指正
- 像string,hash这些类型的操作封装虽然感觉有点别扭(其实可以直接用do做),好处就是在函数中申明defer,结束完后直接回收连接;