Commit d7090689 authored by astaxie's avatar astaxie

cache: change the memcache &redis driver

change the memcache to the newest
parent 52d153da
...@@ -12,16 +12,17 @@ package memcache ...@@ -12,16 +12,17 @@ package memcache
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"strings"
"github.com/beego/memcache" "github.com/bradfitz/gomemcache/memcache"
"github.com/astaxie/beego/cache" "github.com/astaxie/beego/cache"
) )
// Memcache adapter. // Memcache adapter.
type MemcacheCache struct { type MemcacheCache struct {
conn *memcache.Connection conn *memcache.Client
conninfo string conninfo []string
} }
// create new memcache adapter. // create new memcache adapter.
...@@ -36,10 +37,8 @@ func (rc *MemcacheCache) Get(key string) interface{} { ...@@ -36,10 +37,8 @@ func (rc *MemcacheCache) Get(key string) interface{} {
return err return err
} }
} }
if v, err := rc.conn.Get(key); err == nil { if item, err := rc.conn.Get(key); err == nil {
if len(v) > 0 { return string(item.Value)
return string(v[0].Value)
}
} }
return nil return nil
} }
...@@ -55,11 +54,8 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { ...@@ -55,11 +54,8 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error {
if !ok { if !ok {
return errors.New("val must string") return errors.New("val must string")
} }
stored, err := rc.conn.Set(key, 0, uint64(timeout), []byte(v)) item := memcache.Item{Key: key, Value: []byte(v), Expiration: int32(timeout)}
if err == nil && stored == false { return rc.conn.Set(&item)
return errors.New("stored fail")
}
return err
} }
// delete value in memcache. // delete value in memcache.
...@@ -69,20 +65,29 @@ func (rc *MemcacheCache) Delete(key string) error { ...@@ -69,20 +65,29 @@ func (rc *MemcacheCache) Delete(key string) error {
return err return err
} }
} }
_, err := rc.conn.Delete(key) return rc.conn.Delete(key)
return err
} }
// [Not Support]
// increase counter. // increase counter.
func (rc *MemcacheCache) Incr(_ string) error { func (rc *MemcacheCache) Incr(key string) error {
return errors.New("not support in memcache") if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
_, err := rc.conn.Increment(key, 1)
return err
} }
// [Not Support]
// decrease counter. // decrease counter.
func (rc *MemcacheCache) Decr(_ string) error { func (rc *MemcacheCache) Decr(key string) error {
return errors.New("not support in memcache") if rc.conn == nil {
if err := rc.connectInit(); err != nil {
return err
}
}
_, err := rc.conn.Decrement(key, 1)
return err
} }
// check value exists in memcache. // check value exists in memcache.
...@@ -92,13 +97,10 @@ func (rc *MemcacheCache) IsExist(key string) bool { ...@@ -92,13 +97,10 @@ func (rc *MemcacheCache) IsExist(key string) bool {
return false return false
} }
} }
v, err := rc.conn.Get(key) _, err := rc.conn.Get(key)
if err != nil { if err != nil {
return false return false
} }
if len(v) == 0 {
return false
}
return true return true
} }
...@@ -121,7 +123,7 @@ func (rc *MemcacheCache) StartAndGC(config string) error { ...@@ -121,7 +123,7 @@ func (rc *MemcacheCache) StartAndGC(config string) error {
if _, ok := cf["conn"]; !ok { if _, ok := cf["conn"]; !ok {
return errors.New("config has no conn key") return errors.New("config has no conn key")
} }
rc.conninfo = cf["conn"] rc.conninfo = strings.Split(cf["conn"], ";")
if rc.conn == nil { if rc.conn == nil {
if err := rc.connectInit(); err != nil { if err := rc.connectInit(); err != nil {
return err return err
...@@ -132,11 +134,7 @@ func (rc *MemcacheCache) StartAndGC(config string) error { ...@@ -132,11 +134,7 @@ func (rc *MemcacheCache) StartAndGC(config string) error {
// connect to memcache and keep the connection. // connect to memcache and keep the connection.
func (rc *MemcacheCache) connectInit() error { func (rc *MemcacheCache) connectInit() error {
c, err := memcache.Connect(rc.conninfo) rc.conn = memcache.New(rc.conninfo...)
if err != nil {
return err
}
rc.conn = c
return nil return nil
} }
......
...@@ -14,7 +14,7 @@ import ( ...@@ -14,7 +14,7 @@ import (
"errors" "errors"
"time" "time"
"github.com/beego/redigo/redis" "github.com/garyburd/redigo/redis"
"github.com/astaxie/beego/cache" "github.com/astaxie/beego/cache"
) )
......
...@@ -14,7 +14,7 @@ import ( ...@@ -14,7 +14,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/beego/redigo/redis" "github.com/garyburd/redigo/redis"
"github.com/astaxie/beego/cache" "github.com/astaxie/beego/cache"
) )
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment