go 创建连接池操作redis

6 篇文章 0 订阅

安装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)
}

github代码

小结

  1. 感觉写起来还是有点别扭,可能未能真正的了解及应用go 语言特性。有应用不当的请指正
  2. 像string,hash这些类型的操作封装虽然感觉有点别扭(其实可以直接用do做),好处就是在函数中申明defer,结束完后直接回收连接;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值