| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package cache
- import (
- "encoding/json"
- "time"
- "github.com/garyburd/redigo/redis"
- )
- //Redis redis cache
- type Redis struct {
- conn *redis.Pool
- }
- //RedisOpts redis 连接属性
- type RedisOpts struct {
- Host string
- Password string
- Database int
- MaxIdle int
- MaxActive int
- IdleTimeout time.Duration //second
- }
- //NewRedis 实例化
- func NewRedis(opts *RedisOpts) *Redis {
- pool := &redis.Pool{
- MaxActive: opts.MaxActive,
- MaxIdle: opts.MaxIdle,
- IdleTimeout: opts.IdleTimeout,
- Dial: func() (redis.Conn, error) {
- return redis.Dial("tcp", opts.Host,
- redis.DialDatabase(opts.Database),
- redis.DialPassword(opts.Password),
- )
- },
- TestOnBorrow: func(conn redis.Conn, t time.Time) error {
- if time.Since(t) < time.Minute {
- return nil
- }
- _, err := conn.Do("PING")
- return err
- },
- }
- return &Redis{pool}
- }
- //Get 获取一个值
- func (r *Redis) Get(key string) interface{} {
- conn := r.conn.Get()
- defer conn.Close()
- var data []byte
- var err error
- if data, err = redis.Bytes(conn.Do("GET", key)); err != nil {
- return nil
- }
- var reply interface{}
- if err = json.Unmarshal(data, &reply); err != nil {
- return nil
- }
- return reply
- }
- //Set 设置一个值
- func (r *Redis) Set(key string, val interface{}, timeout time.Duration) (err error) {
- conn := r.conn.Get()
- defer conn.Close()
- var data []byte
- if data, err = json.Marshal(val); err != nil {
- return
- }
- _, err = conn.Do("SETEX", key, int64(timeout/time.Second), data)
- return
- }
- //IsExist 判断key是否存在
- func (r *Redis) IsExist(key string) bool {
- conn := r.conn.Get()
- defer conn.Close()
- a, _ := conn.Do("EXISTS", key)
- i := a.(int64)
- if i > 0 {
- return true
- }
- return false
- }
- //Delete 删除
- func (r *Redis) Delete(key string) error {
- conn := r.conn.Get()
- defer conn.Close()
- if _, err := conn.Do("DEL", key); err != nil {
- return err
- }
- return nil
- }
|