initial commit
This commit is contained in:
28
vendor/github.com/go-xorm/xorm-redis-cache/LICENSE
generated
vendored
Normal file
28
vendor/github.com/go-xorm/xorm-redis-cache/LICENSE
generated
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
Copyright (c) 2014, go-xorm
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
* Neither the name of the {organization} nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
9
vendor/github.com/go-xorm/xorm-redis-cache/README.md
generated
vendored
Normal file
9
vendor/github.com/go-xorm/xorm-redis-cache/README.md
generated
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
xorm-redis-cache
|
||||
================
|
||||
|
||||
XORM Redis Cache
|
||||
|
||||
|
||||
[](http://gowalker.org/github.com/go-xorm/xorm-redis-cache)
|
||||
|
||||
|
302
vendor/github.com/go-xorm/xorm-redis-cache/redis_cacher.go
generated
vendored
Normal file
302
vendor/github.com/go-xorm/xorm-redis-cache/redis_cacher.go
generated
vendored
Normal file
@ -0,0 +1,302 @@
|
||||
package xormrediscache
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"github.com/garyburd/redigo/redis"
|
||||
"github.com/go-xorm/core"
|
||||
"hash/crc32"
|
||||
// "log"
|
||||
"reflect"
|
||||
// "strconv"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
DEFAULT_EXPIRATION = time.Duration(0)
|
||||
FOREVER_EXPIRATION = time.Duration(-1)
|
||||
|
||||
LOGGING_PREFIX = "[redis_cacher]"
|
||||
)
|
||||
|
||||
// Wraps the Redis client to meet the Cache interface.
|
||||
type RedisCacher struct {
|
||||
pool *redis.Pool
|
||||
defaultExpiration time.Duration
|
||||
|
||||
Logger core.ILogger
|
||||
}
|
||||
|
||||
// New a Redis Cacher, host as IP endpoint, i.e., localhost:6379, provide empty string or nil if Redis server doesn't
|
||||
// require AUTH command, defaultExpiration sets the expire duration for a key to live. Until redigo supports
|
||||
// sharding/clustering, only one host will be in hostList
|
||||
//
|
||||
// engine.SetDefaultCacher(xormrediscache.NewRedisCacher("localhost:6379", "", xormrediscache.DEFAULT_EXPIRATION, engine.Logger))
|
||||
//
|
||||
// or set MapCacher
|
||||
//
|
||||
// engine.MapCacher(&user, xormrediscache.NewRedisCacher("localhost:6379", "", xormrediscache.DEFAULT_EXPIRATION, engine.Logger))
|
||||
//
|
||||
func NewRedisCacher(host string, password string, defaultExpiration time.Duration, logger core.ILogger) *RedisCacher {
|
||||
var pool = &redis.Pool{
|
||||
MaxIdle: 5,
|
||||
IdleTimeout: 240 * time.Second,
|
||||
Dial: func() (redis.Conn, error) {
|
||||
// the redis protocol should probably be made sett-able
|
||||
c, err := redis.Dial("tcp", host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(password) > 0 {
|
||||
if _, err := c.Do("AUTH", password); err != nil {
|
||||
c.Close()
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
// check with PING
|
||||
if _, err := c.Do("PING"); err != nil {
|
||||
c.Close()
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return c, err
|
||||
},
|
||||
// custom connection test method
|
||||
TestOnBorrow: func(c redis.Conn, t time.Time) error {
|
||||
if _, err := c.Do("PING"); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
return &RedisCacher{pool: pool, defaultExpiration: defaultExpiration, Logger: logger}
|
||||
}
|
||||
|
||||
func exists(conn redis.Conn, key string) bool {
|
||||
existed, _ := redis.Bool(conn.Do("EXISTS", key))
|
||||
return existed
|
||||
}
|
||||
|
||||
func (c *RedisCacher) logErrf(format string, contents ...interface{}) {
|
||||
if c.Logger != nil {
|
||||
c.Logger.Errorf(fmt.Sprintf("%s %s", LOGGING_PREFIX, format), contents...)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *RedisCacher) logDebugf(format string, contents ...interface{}) {
|
||||
if c.Logger != nil {
|
||||
c.Logger.Debugf(fmt.Sprintf("%s %s", LOGGING_PREFIX, format), contents...)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *RedisCacher) getBeanKey(tableName string, id string) string {
|
||||
return fmt.Sprintf("xorm:bean:%s:%s", tableName, id)
|
||||
}
|
||||
|
||||
func (c *RedisCacher) getSqlKey(tableName string, sql string) string {
|
||||
// hash sql to minimize key length
|
||||
crc := crc32.ChecksumIEEE([]byte(sql))
|
||||
return fmt.Sprintf("xorm:sql:%s:%d", tableName, crc)
|
||||
}
|
||||
|
||||
// Delete all xorm cached objects
|
||||
func (c *RedisCacher) Flush() error {
|
||||
// conn := c.pool.Get()
|
||||
// defer conn.Close()
|
||||
// _, err := conn.Do("FLUSHALL")
|
||||
// return err
|
||||
return c.delObject("xorm:*")
|
||||
}
|
||||
|
||||
func (c *RedisCacher) getObject(key string) interface{} {
|
||||
conn := c.pool.Get()
|
||||
defer conn.Close()
|
||||
raw, err := conn.Do("GET", key)
|
||||
if raw == nil {
|
||||
return nil
|
||||
}
|
||||
item, err := redis.Bytes(raw, err)
|
||||
if err != nil {
|
||||
c.logErrf("redis.Bytes failed: %s", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
value, err := c.deserialize(item)
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func (c *RedisCacher) GetIds(tableName, sql string) interface{} {
|
||||
sqlKey := c.getSqlKey(tableName, sql)
|
||||
c.logDebugf(" GetIds|tableName:%s|sql:%s|key:%s", tableName, sql, sqlKey)
|
||||
return c.getObject(sqlKey)
|
||||
}
|
||||
|
||||
func (c *RedisCacher) GetBean(tableName string, id string) interface{} {
|
||||
beanKey := c.getBeanKey(tableName, id)
|
||||
c.logDebugf("[xorm/redis_cacher] GetBean|tableName:%s|id:%s|key:%s", tableName, id, beanKey)
|
||||
return c.getObject(beanKey)
|
||||
}
|
||||
|
||||
func (c *RedisCacher) putObject(key string, value interface{}) {
|
||||
c.invoke(c.pool.Get().Do, key, value, c.defaultExpiration)
|
||||
}
|
||||
|
||||
func (c *RedisCacher) PutIds(tableName, sql string, ids interface{}) {
|
||||
sqlKey := c.getSqlKey(tableName, sql)
|
||||
c.logDebugf("PutIds|tableName:%s|sql:%s|key:%s|obj:%s|type:%v", tableName, sql, sqlKey, ids, reflect.TypeOf(ids))
|
||||
c.putObject(sqlKey, ids)
|
||||
}
|
||||
|
||||
func (c *RedisCacher) PutBean(tableName string, id string, obj interface{}) {
|
||||
beanKey := c.getBeanKey(tableName, id)
|
||||
c.logDebugf("PutBean|tableName:%s|id:%s|key:%s|type:%v", tableName, id, beanKey, reflect.TypeOf(obj))
|
||||
c.putObject(beanKey, obj)
|
||||
}
|
||||
|
||||
func (c *RedisCacher) delObject(key string) error {
|
||||
c.logDebugf("delObject key:[%s]", key)
|
||||
|
||||
conn := c.pool.Get()
|
||||
defer conn.Close()
|
||||
if !exists(conn, key) {
|
||||
c.logErrf("delObject key:[%s] err: %v", key, core.ErrCacheMiss)
|
||||
return core.ErrCacheMiss
|
||||
}
|
||||
_, err := conn.Do("DEL", key)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *RedisCacher) delObjects(key string) error {
|
||||
|
||||
c.logDebugf("delObjects key:[%s]", key)
|
||||
|
||||
conn := c.pool.Get()
|
||||
defer conn.Close()
|
||||
|
||||
keys, err := conn.Do("KEYS", key)
|
||||
c.logDebugf("delObjects keys: %v", keys)
|
||||
|
||||
if err == nil {
|
||||
for _, key := range keys.([]interface{}) {
|
||||
conn.Do("DEL", key)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *RedisCacher) DelIds(tableName, sql string) {
|
||||
c.delObject(c.getSqlKey(tableName, sql))
|
||||
}
|
||||
|
||||
func (c *RedisCacher) DelBean(tableName string, id string) {
|
||||
c.delObject(c.getBeanKey(tableName, id))
|
||||
}
|
||||
|
||||
func (c *RedisCacher) ClearIds(tableName string) {
|
||||
c.delObjects(c.getSqlKey(tableName, "*"))
|
||||
}
|
||||
|
||||
func (c *RedisCacher) ClearBeans(tableName string) {
|
||||
c.delObjects(c.getBeanKey(tableName, "*"))
|
||||
}
|
||||
|
||||
func (c *RedisCacher) invoke(f func(string, ...interface{}) (interface{}, error),
|
||||
key string, value interface{}, expires time.Duration) error {
|
||||
|
||||
switch expires {
|
||||
case DEFAULT_EXPIRATION:
|
||||
expires = c.defaultExpiration
|
||||
case FOREVER_EXPIRATION:
|
||||
expires = time.Duration(0)
|
||||
}
|
||||
|
||||
b, err := c.serialize(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conn := c.pool.Get()
|
||||
defer conn.Close()
|
||||
if expires > 0 {
|
||||
_, err := f("SETEX", key, int32(expires/time.Second), b)
|
||||
return err
|
||||
} else {
|
||||
_, err := f("SET", key, b)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (c *RedisCacher) serialize(value interface{}) ([]byte, error) {
|
||||
|
||||
err := c.registerGobConcreteType(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if reflect.TypeOf(value).Kind() == reflect.Struct {
|
||||
return nil, fmt.Errorf("serialize func only take pointer of a struct")
|
||||
}
|
||||
|
||||
var b bytes.Buffer
|
||||
encoder := gob.NewEncoder(&b)
|
||||
|
||||
c.logDebugf("serialize type:%v", reflect.TypeOf(value))
|
||||
err = encoder.Encode(&value)
|
||||
if err != nil {
|
||||
c.logErrf("gob encoding '%s' failed: %s|value:%v", value, err, value)
|
||||
return nil, err
|
||||
}
|
||||
return b.Bytes(), nil
|
||||
}
|
||||
|
||||
func (c *RedisCacher) deserialize(byt []byte) (ptr interface{}, err error) {
|
||||
b := bytes.NewBuffer(byt)
|
||||
decoder := gob.NewDecoder(b)
|
||||
|
||||
var p interface{}
|
||||
err = decoder.Decode(&p)
|
||||
if err != nil {
|
||||
c.logErrf("decode failed: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
v := reflect.ValueOf(p)
|
||||
c.logDebugf("deserialize type:%v", v.Type())
|
||||
if v.Kind() == reflect.Struct {
|
||||
|
||||
var pp interface{} = &p
|
||||
datas := reflect.ValueOf(pp).Elem().InterfaceData()
|
||||
|
||||
sp := reflect.NewAt(v.Type(),
|
||||
unsafe.Pointer(datas[1])).Interface()
|
||||
ptr = sp
|
||||
vv := reflect.ValueOf(ptr)
|
||||
c.logDebugf("deserialize convert ptr type:%v | CanAddr:%t", vv.Type(), vv.CanAddr())
|
||||
} else {
|
||||
ptr = p
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *RedisCacher) registerGobConcreteType(value interface{}) error {
|
||||
|
||||
t := reflect.TypeOf(value)
|
||||
|
||||
c.logDebugf("registerGobConcreteType:%v", t)
|
||||
|
||||
switch t.Kind() {
|
||||
case reflect.Ptr:
|
||||
v := reflect.ValueOf(value)
|
||||
i := v.Elem().Interface()
|
||||
gob.Register(i)
|
||||
case reflect.Struct, reflect.Map, reflect.Slice:
|
||||
gob.Register(value)
|
||||
case reflect.String, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Bool, reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
|
||||
// do nothing since already registered known type
|
||||
default:
|
||||
return fmt.Errorf("unhandled type: %v", t)
|
||||
}
|
||||
return nil
|
||||
}
|
6
vendor/github.com/go-xorm/xorm-redis-cache/run_test.sh
generated
vendored
Executable file
6
vendor/github.com/go-xorm/xorm-redis-cache/run_test.sh
generated
vendored
Executable file
@ -0,0 +1,6 @@
|
||||
redis-cli FLUSHALL
|
||||
if [ $? == "0" ];then
|
||||
go test -v -run=TestMysqlWithCache
|
||||
else
|
||||
echo "no redis-server running on localhost"
|
||||
fi
|
Reference in New Issue
Block a user