1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-23 02:20:55 +00:00

Merge pull request #692 from fuxiaohei/develop

code style simplify
This commit is contained in:
astaxie 2014-07-12 14:18:30 +08:00
commit 15759f60ed
2 changed files with 41 additions and 63 deletions

View File

@ -7,7 +7,7 @@
// @license http://github.com/astaxie/beego/blob/master/LICENSE // @license http://github.com/astaxie/beego/blob/master/LICENSE
// //
// @authors astaxie // @authors astaxie
package cache package memcache
import ( import (
"encoding/json" "encoding/json"
@ -20,7 +20,7 @@ import (
// Memcache adapter. // Memcache adapter.
type MemcacheCache struct { type MemcacheCache struct {
c *memcache.Connection conn *memcache.Connection
conninfo string conninfo string
} }
@ -31,32 +31,23 @@ func NewMemCache() *MemcacheCache {
// get value from memcache. // get value from memcache.
func (rc *MemcacheCache) Get(key string) interface{} { func (rc *MemcacheCache) Get(key string) interface{} {
if rc.c == nil { if rc.conn == nil {
var err error if err := rc.connectInit(); err != nil {
rc.c, err = rc.connectInit()
if err != nil {
return err return err
} }
} }
v, err := rc.c.Get(key) if v, err := rc.conn.Get(key); err == nil {
if err != nil { if len(v) > 0 {
return nil return string(v[0].Value)
}
} }
var contain interface{} return nil
if len(v) > 0 {
contain = string(v[0].Value)
} else {
contain = nil
}
return contain
} }
// put value to memcache. only support string. // put value to memcache. only support string.
func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error {
if rc.c == nil { if rc.conn == nil {
var err error if err := rc.connectInit(); err != nil {
rc.c, err = rc.connectInit()
if err != nil {
return err return err
} }
} }
@ -64,7 +55,7 @@ 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.c.Set(key, 0, uint64(timeout), []byte(v)) stored, err := rc.conn.Set(key, 0, uint64(timeout), []byte(v))
if err == nil && stored == false { if err == nil && stored == false {
return errors.New("stored fail") return errors.New("stored fail")
} }
@ -73,60 +64,52 @@ func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error {
// delete value in memcache. // delete value in memcache.
func (rc *MemcacheCache) Delete(key string) error { func (rc *MemcacheCache) Delete(key string) error {
if rc.c == nil { if rc.conn == nil {
var err error if err := rc.connectInit(); err != nil {
rc.c, err = rc.connectInit()
if err != nil {
return err return err
} }
} }
_, err := rc.c.Delete(key) _, err := rc.conn.Delete(key)
return err return err
} }
// [Not Support] // [Not Support]
// increase counter. // increase counter.
func (rc *MemcacheCache) Incr(key string) error { func (rc *MemcacheCache) Incr(_ string) error {
return errors.New("not support in memcache") return errors.New("not support in memcache")
} }
// [Not Support] // [Not Support]
// decrease counter. // decrease counter.
func (rc *MemcacheCache) Decr(key string) error { func (rc *MemcacheCache) Decr(_ string) error {
return errors.New("not support in memcache") return errors.New("not support in memcache")
} }
// check value exists in memcache. // check value exists in memcache.
func (rc *MemcacheCache) IsExist(key string) bool { func (rc *MemcacheCache) IsExist(key string) bool {
if rc.c == nil { if rc.conn == nil {
var err error if err := rc.connectInit(); err != nil {
rc.c, err = rc.connectInit()
if err != nil {
return false return false
} }
} }
v, err := rc.c.Get(key) v, err := rc.conn.Get(key)
if err != nil { if err != nil {
return false return false
} }
if len(v) == 0 { if len(v) == 0 {
return false return false
} else {
return true
} }
return true
} }
// clear all cached in memcache. // clear all cached in memcache.
func (rc *MemcacheCache) ClearAll() error { func (rc *MemcacheCache) ClearAll() error {
if rc.c == nil { if rc.conn == nil {
var err error if err := rc.connectInit(); err != nil {
rc.c, err = rc.connectInit()
if err != nil {
return err return err
} }
} }
err := rc.c.FlushAll() return rc.conn.FlushAll()
return err
} }
// start memcache adapter. // start memcache adapter.
@ -139,23 +122,22 @@ func (rc *MemcacheCache) StartAndGC(config string) error {
return errors.New("config has no conn key") return errors.New("config has no conn key")
} }
rc.conninfo = cf["conn"] rc.conninfo = cf["conn"]
var err error if rc.conn == nil {
if rc.c != nil { if err := rc.connectInit(); err != nil {
rc.c, err = rc.connectInit() return err
if err != nil {
return errors.New("dial tcp conn error")
} }
} }
return nil return nil
} }
// connect to memcache and keep the connection. // connect to memcache and keep the connection.
func (rc *MemcacheCache) connectInit() (*memcache.Connection, error) { func (rc *MemcacheCache) connectInit() error {
c, err := memcache.Connect(rc.conninfo) c, err := memcache.Connect(rc.conninfo)
if err != nil { if err != nil {
return nil, err return err
} }
return c, nil rc.conn = c
return nil
} }
func init() { func init() {

24
cache/memory.go vendored
View File

@ -49,15 +49,14 @@ func NewMemoryCache() *MemoryCache {
func (bc *MemoryCache) Get(name string) interface{} { func (bc *MemoryCache) Get(name string) interface{} {
bc.lock.RLock() bc.lock.RLock()
defer bc.lock.RUnlock() defer bc.lock.RUnlock()
itm, ok := bc.items[name] if itm, ok := bc.items[name]; ok {
if !ok { if (time.Now().Unix() - itm.Lastaccess.Unix()) > itm.expired {
return nil go bc.Delete(name)
return nil
}
return itm.val
} }
if (time.Now().Unix() - itm.Lastaccess.Unix()) > itm.expired { return nil
go bc.Delete(name)
return nil
}
return itm.val
} }
// Put cache to memory. // Put cache to memory.
@ -65,12 +64,11 @@ func (bc *MemoryCache) Get(name string) interface{} {
func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error { func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error {
bc.lock.Lock() bc.lock.Lock()
defer bc.lock.Unlock() defer bc.lock.Unlock()
t := MemoryItem{ bc.items[name] = &MemoryItem{
val: value, val: value,
Lastaccess: time.Now(), Lastaccess: time.Now(),
expired: expired, expired: expired,
} }
bc.items[name] = &t
return nil return nil
} }
@ -82,8 +80,7 @@ func (bc *MemoryCache) Delete(name string) error {
return errors.New("key not exist") return errors.New("key not exist")
} }
delete(bc.items, name) delete(bc.items, name)
_, valid := bc.items[name] if _, ok := bc.items[name]; ok {
if valid {
return errors.New("delete key error") return errors.New("delete key error")
} }
return nil return nil
@ -214,8 +211,7 @@ func (bc *MemoryCache) item_expired(name string) bool {
if !ok { if !ok {
return true return true
} }
sec := time.Now().Unix() - itm.Lastaccess.Unix() if time.Now().Unix()-itm.Lastaccess.Unix() >= itm.expired {
if sec >= itm.expired {
delete(bc.items, name) delete(bc.items, name)
return true return true
} }