2014-08-18 08:41:43 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-09-08 16:15:03 +00:00
|
|
|
// Package redis for cache provider
|
2014-08-18 08:41:43 +00:00
|
|
|
//
|
2018-03-26 08:59:01 +00:00
|
|
|
// depend on github.com/gomodule/redigo/redis
|
2014-08-18 08:41:43 +00:00
|
|
|
//
|
2018-03-26 08:59:01 +00:00
|
|
|
// go install github.com/gomodule/redigo/redis
|
2014-08-18 08:41:43 +00:00
|
|
|
//
|
|
|
|
// Usage:
|
|
|
|
// import(
|
|
|
|
// _ "github.com/astaxie/beego/cache/redis"
|
|
|
|
// "github.com/astaxie/beego/cache"
|
|
|
|
// )
|
|
|
|
//
|
|
|
|
// bm, err := cache.NewCache("redis", `{"conn":"127.0.0.1:11211"}`)
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// more docs http://beego.me/docs/module/cache.md
|
2014-07-12 07:51:47 +00:00
|
|
|
package redis
|
2013-04-22 10:56:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2017-05-20 11:56:38 +00:00
|
|
|
"fmt"
|
2015-02-16 13:56:32 +00:00
|
|
|
"strconv"
|
2014-01-10 10:31:15 +00:00
|
|
|
"time"
|
2013-12-03 13:37:39 +00:00
|
|
|
|
2018-03-26 08:59:01 +00:00
|
|
|
"github.com/gomodule/redigo/redis"
|
2014-04-04 00:31:22 +00:00
|
|
|
|
|
|
|
"github.com/astaxie/beego/cache"
|
2018-09-26 16:05:09 +00:00
|
|
|
"strings"
|
2013-04-22 10:56:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-09-08 16:15:03 +00:00
|
|
|
// DefaultKey the collection name of redis for cache adapter.
|
|
|
|
DefaultKey = "beecacheRedis"
|
2013-04-22 10:56:30 +00:00
|
|
|
)
|
|
|
|
|
2015-09-08 16:15:03 +00:00
|
|
|
// Cache is Redis cache adapter.
|
|
|
|
type Cache struct {
|
2014-01-10 10:31:15 +00:00
|
|
|
p *redis.Pool // redis connection pool
|
2013-04-22 10:56:30 +00:00
|
|
|
conninfo string
|
2015-02-16 13:56:32 +00:00
|
|
|
dbNum int
|
2013-04-22 10:56:30 +00:00
|
|
|
key string
|
2015-06-07 08:26:23 +00:00
|
|
|
password string
|
2017-12-26 10:21:39 +00:00
|
|
|
maxIdle int
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 16:15:03 +00:00
|
|
|
// NewRedisCache create new redis cache with default collection name.
|
2016-01-04 02:50:04 +00:00
|
|
|
func NewRedisCache() cache.Cache {
|
2015-09-08 16:15:03 +00:00
|
|
|
return &Cache{key: DefaultKey}
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 11:56:38 +00:00
|
|
|
// actually do the redis cmds, args[0] must be the key name.
|
2015-09-08 16:15:03 +00:00
|
|
|
func (rc *Cache) do(commandName string, args ...interface{}) (reply interface{}, err error) {
|
2017-05-20 11:56:38 +00:00
|
|
|
if len(args) < 1 {
|
|
|
|
return nil, errors.New("missing required arguments")
|
|
|
|
}
|
|
|
|
args[0] = rc.associate(args[0])
|
2014-01-10 10:31:15 +00:00
|
|
|
c := rc.p.Get()
|
|
|
|
defer c.Close()
|
2014-01-09 10:49:18 +00:00
|
|
|
|
2014-01-10 10:31:15 +00:00
|
|
|
return c.Do(commandName, args...)
|
|
|
|
}
|
2014-01-09 10:49:18 +00:00
|
|
|
|
2017-05-20 11:56:38 +00:00
|
|
|
// associate with config key.
|
|
|
|
func (rc *Cache) associate(originKey interface{}) string {
|
|
|
|
return fmt.Sprintf("%s:%s", rc.key, originKey)
|
|
|
|
}
|
|
|
|
|
2014-01-10 10:31:15 +00:00
|
|
|
// Get cache from redis.
|
2015-09-08 16:15:03 +00:00
|
|
|
func (rc *Cache) Get(key string) interface{} {
|
2014-07-12 07:51:47 +00:00
|
|
|
if v, err := rc.do("GET", key); err == nil {
|
|
|
|
return v
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
2014-07-12 07:51:47 +00:00
|
|
|
return nil
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
|
|
|
|
2015-06-07 13:33:01 +00:00
|
|
|
// GetMulti get cache from redis.
|
2015-09-08 16:15:03 +00:00
|
|
|
func (rc *Cache) GetMulti(keys []string) []interface{} {
|
2015-06-07 13:33:01 +00:00
|
|
|
c := rc.p.Get()
|
|
|
|
defer c.Close()
|
2017-05-20 11:56:38 +00:00
|
|
|
var args []interface{}
|
2015-06-07 13:33:01 +00:00
|
|
|
for _, key := range keys {
|
2017-05-20 11:56:38 +00:00
|
|
|
args = append(args, rc.associate(key))
|
2015-06-07 13:33:01 +00:00
|
|
|
}
|
2017-05-20 11:56:38 +00:00
|
|
|
values, err := redis.Values(c.Do("MGET", args...))
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
2015-06-07 13:33:01 +00:00
|
|
|
}
|
2017-05-20 11:56:38 +00:00
|
|
|
return values
|
2015-06-07 13:33:01 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 16:15:03 +00:00
|
|
|
// Put put cache to redis.
|
2016-01-08 05:47:14 +00:00
|
|
|
func (rc *Cache) Put(key string, val interface{}, timeout time.Duration) error {
|
2017-05-20 11:56:38 +00:00
|
|
|
_, err := rc.do("SETEX", key, int64(timeout/time.Second), val)
|
2013-04-22 10:56:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-08 16:15:03 +00:00
|
|
|
// Delete delete cache in redis.
|
|
|
|
func (rc *Cache) Delete(key string) error {
|
2017-05-20 11:56:38 +00:00
|
|
|
_, err := rc.do("DEL", key)
|
2013-04-22 10:56:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-08 16:15:03 +00:00
|
|
|
// IsExist check cache's existence in redis.
|
|
|
|
func (rc *Cache) IsExist(key string) bool {
|
2014-07-05 14:27:31 +00:00
|
|
|
v, err := redis.Bool(rc.do("EXISTS", key))
|
2013-04-22 10:56:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2015-09-08 16:15:03 +00:00
|
|
|
// Incr increase counter in redis.
|
|
|
|
func (rc *Cache) Incr(key string) error {
|
2014-07-05 14:27:31 +00:00
|
|
|
_, err := redis.Bool(rc.do("INCRBY", key, 1))
|
2014-01-09 10:49:18 +00:00
|
|
|
return err
|
2013-07-16 11:05:44 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 16:15:03 +00:00
|
|
|
// Decr decrease counter in redis.
|
|
|
|
func (rc *Cache) Decr(key string) error {
|
2014-07-05 14:27:31 +00:00
|
|
|
_, err := redis.Bool(rc.do("INCRBY", key, -1))
|
2014-01-09 10:49:18 +00:00
|
|
|
return err
|
2013-07-16 11:05:44 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 16:15:03 +00:00
|
|
|
// ClearAll clean all cache in redis. delete this redis collection.
|
|
|
|
func (rc *Cache) ClearAll() error {
|
2017-05-20 11:56:38 +00:00
|
|
|
c := rc.p.Get()
|
|
|
|
defer c.Close()
|
|
|
|
cachedKeys, err := redis.Strings(c.Do("KEYS", rc.key+":*"))
|
2014-07-12 07:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-07-05 14:27:31 +00:00
|
|
|
for _, str := range cachedKeys {
|
2017-05-20 11:56:38 +00:00
|
|
|
if _, err = c.Do("DEL", str); err != nil {
|
2014-07-12 07:51:47 +00:00
|
|
|
return err
|
2014-07-05 14:27:31 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-22 10:56:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-08 16:15:03 +00:00
|
|
|
// StartAndGC start redis cache adapter.
|
2015-02-16 13:56:32 +00:00
|
|
|
// config is like {"key":"collection key","conn":"connection info","dbNum":"0"}
|
2013-12-22 05:35:02 +00:00
|
|
|
// the cache item in redis are stored forever,
|
|
|
|
// so no gc operation.
|
2015-09-08 16:15:03 +00:00
|
|
|
func (rc *Cache) StartAndGC(config string) error {
|
2013-04-22 10:56:30 +00:00
|
|
|
var cf map[string]string
|
|
|
|
json.Unmarshal([]byte(config), &cf)
|
2014-01-10 10:31:15 +00:00
|
|
|
|
2013-04-22 10:56:30 +00:00
|
|
|
if _, ok := cf["key"]; !ok {
|
|
|
|
cf["key"] = DefaultKey
|
|
|
|
}
|
|
|
|
if _, ok := cf["conn"]; !ok {
|
|
|
|
return errors.New("config has no conn key")
|
|
|
|
}
|
2018-09-26 16:05:09 +00:00
|
|
|
|
|
|
|
// Format redis://<password>@<host>:<port>
|
|
|
|
cf["conn"] = strings.Replace(cf["conn"], "redis://", "", 1)
|
|
|
|
if i := strings.Index(cf["conn"], "@"); i > -1 {
|
|
|
|
cf["password"] = cf["conn"][0:i]
|
|
|
|
cf["conn"] = cf["conn"][i+1:]
|
|
|
|
}
|
|
|
|
|
2015-02-16 13:56:32 +00:00
|
|
|
if _, ok := cf["dbNum"]; !ok {
|
|
|
|
cf["dbNum"] = "0"
|
|
|
|
}
|
2015-06-07 08:26:23 +00:00
|
|
|
if _, ok := cf["password"]; !ok {
|
|
|
|
cf["password"] = ""
|
|
|
|
}
|
2017-12-26 10:21:39 +00:00
|
|
|
if _, ok := cf["maxIdle"]; !ok {
|
2017-12-26 10:43:04 +00:00
|
|
|
cf["maxIdle"] = "3"
|
2017-12-26 10:21:39 +00:00
|
|
|
}
|
2013-04-22 10:56:30 +00:00
|
|
|
rc.key = cf["key"]
|
|
|
|
rc.conninfo = cf["conn"]
|
2015-02-16 13:56:32 +00:00
|
|
|
rc.dbNum, _ = strconv.Atoi(cf["dbNum"])
|
2015-06-07 08:26:23 +00:00
|
|
|
rc.password = cf["password"]
|
2017-12-26 10:21:39 +00:00
|
|
|
rc.maxIdle, _ = strconv.Atoi(cf["maxIdle"])
|
2015-06-07 08:26:23 +00:00
|
|
|
|
2014-01-10 10:31:15 +00:00
|
|
|
rc.connectInit()
|
|
|
|
|
|
|
|
c := rc.p.Get()
|
|
|
|
defer c.Close()
|
|
|
|
|
2014-07-12 07:51:47 +00:00
|
|
|
return c.Err()
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
|
|
|
|
2013-12-22 05:35:02 +00:00
|
|
|
// connect to redis.
|
2015-09-08 16:15:03 +00:00
|
|
|
func (rc *Cache) connectInit() {
|
2014-07-12 07:51:47 +00:00
|
|
|
dialFunc := func() (c redis.Conn, err error) {
|
|
|
|
c, err = redis.Dial("tcp", rc.conninfo)
|
2015-06-07 08:26:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if rc.password != "" {
|
|
|
|
if _, err := c.Do("AUTH", rc.password); err != nil {
|
|
|
|
c.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-16 13:56:32 +00:00
|
|
|
_, selecterr := c.Do("SELECT", rc.dbNum)
|
|
|
|
if selecterr != nil {
|
|
|
|
c.Close()
|
|
|
|
return nil, selecterr
|
|
|
|
}
|
2014-07-12 07:51:47 +00:00
|
|
|
return
|
|
|
|
}
|
2014-01-10 10:31:15 +00:00
|
|
|
// initialize a new pool
|
|
|
|
rc.p = &redis.Pool{
|
2017-12-26 10:21:39 +00:00
|
|
|
MaxIdle: rc.maxIdle,
|
2014-01-10 10:31:15 +00:00
|
|
|
IdleTimeout: 180 * time.Second,
|
2014-07-12 07:51:47 +00:00
|
|
|
Dial: dialFunc,
|
2014-01-10 10:31:15 +00:00
|
|
|
}
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-04 02:50:04 +00:00
|
|
|
cache.Register("redis", NewRedisCache)
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|