1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-19 10:40:58 +00:00
Beego/session/memcache/sess_memcache.go

213 lines
4.5 KiB
Go
Raw Normal View History

2014-05-16 17:19:47 +00:00
// Beego (http://beego.me/)
2014-07-03 15:40:21 +00:00
//
2014-05-16 17:19:47 +00:00
// @description beego is an open-source, high-performance web framework for the Go programming language.
2014-07-03 15:40:21 +00:00
//
2014-05-16 17:19:47 +00:00
// @link http://github.com/astaxie/beego for the canonical source repository
2014-07-03 15:40:21 +00:00
//
2014-05-16 17:19:47 +00:00
// @license http://github.com/astaxie/beego/blob/master/LICENSE
2014-07-03 15:40:21 +00:00
//
2014-05-16 17:19:47 +00:00
// @authors astaxie
package session
import (
"net/http"
"strings"
2014-05-16 17:19:47 +00:00
"sync"
"github.com/astaxie/beego/session"
"github.com/bradfitz/gomemcache/memcache"
2014-05-16 17:19:47 +00:00
)
var mempder = &MemProvider{}
var client *memcache.Client
2014-05-16 17:19:47 +00:00
// memcache session store
type MemcacheSessionStore struct {
sid string
lock sync.RWMutex
values map[interface{}]interface{}
maxlifetime int64
}
// set value in memcache session
func (rs *MemcacheSessionStore) Set(key, value interface{}) error {
rs.lock.Lock()
defer rs.lock.Unlock()
rs.values[key] = value
return nil
}
// get value in memcache session
func (rs *MemcacheSessionStore) Get(key interface{}) interface{} {
rs.lock.RLock()
defer rs.lock.RUnlock()
if v, ok := rs.values[key]; ok {
return v
} else {
return nil
}
}
// delete value in memcache session
func (rs *MemcacheSessionStore) Delete(key interface{}) error {
rs.lock.Lock()
defer rs.lock.Unlock()
delete(rs.values, key)
return nil
}
// clear all values in memcache session
func (rs *MemcacheSessionStore) Flush() error {
rs.lock.Lock()
defer rs.lock.Unlock()
rs.values = make(map[interface{}]interface{})
return nil
}
// get redis session id
func (rs *MemcacheSessionStore) SessionID() string {
return rs.sid
}
// save session values to redis
func (rs *MemcacheSessionStore) SessionRelease(w http.ResponseWriter) {
b, err := session.EncodeGob(rs.values)
if err != nil {
return
}
item := memcache.Item{Key: rs.sid, Value: b, Expiration: int32(rs.maxlifetime)}
client.Set(&item)
2014-05-16 17:19:47 +00:00
}
// redis session provider
type MemProvider struct {
maxlifetime int64
conninfo []string
2014-05-16 17:19:47 +00:00
poolsize int
password string
}
// init redis session
// savepath like
// e.g. 127.0.0.1:9090
func (rp *MemProvider) SessionInit(maxlifetime int64, savePath string) error {
rp.maxlifetime = maxlifetime
rp.conninfo = strings.Split(savePath, ";")
client = memcache.New(rp.conninfo...)
2014-05-16 17:19:47 +00:00
return nil
}
// read redis session by sid
func (rp *MemProvider) SessionRead(sid string) (session.SessionStore, error) {
if client == nil {
if err := rp.connectInit(); err != nil {
return nil, err
}
2014-05-16 17:19:47 +00:00
}
item, err := client.Get(sid)
2014-05-16 17:19:47 +00:00
if err != nil {
return nil, err
}
var kv map[interface{}]interface{}
if len(item.Value) == 0 {
2014-05-16 17:19:47 +00:00
kv = make(map[interface{}]interface{})
} else {
kv, err = session.DecodeGob(item.Value)
2014-05-16 17:19:47 +00:00
if err != nil {
return nil, err
}
}
rs := &MemcacheSessionStore{sid: sid, values: kv, maxlifetime: rp.maxlifetime}
2014-05-16 17:19:47 +00:00
return rs, nil
}
// check redis session exist by sid
func (rp *MemProvider) SessionExist(sid string) bool {
if client == nil {
if err := rp.connectInit(); err != nil {
return false
}
2014-05-16 17:19:47 +00:00
}
if item, err := client.Get(sid); err != nil || len(item.Value) == 0 {
2014-05-16 17:19:47 +00:00
return false
} else {
return true
}
}
// generate new sid for redis session
func (rp *MemProvider) SessionRegenerate(oldsid, sid string) (session.SessionStore, error) {
if client == nil {
if err := rp.connectInit(); err != nil {
return nil, err
}
2014-05-16 17:19:47 +00:00
}
var contain []byte
if item, err := client.Get(sid); err != nil || len(item.Value) == 0 {
2014-05-16 17:19:47 +00:00
// oldsid doesn't exists, set the new sid directly
// ignore error here, since if it return error
// the existed value will be 0
item.Key = sid
item.Value = []byte("")
item.Expiration = int32(rp.maxlifetime)
client.Set(item)
2014-05-16 17:19:47 +00:00
} else {
client.Delete(oldsid)
item.Key = sid
item.Value = item.Value
item.Expiration = int32(rp.maxlifetime)
client.Set(item)
contain = item.Value
2014-05-16 17:19:47 +00:00
}
var kv map[interface{}]interface{}
if len(contain) == 0 {
kv = make(map[interface{}]interface{})
} else {
var err error
2014-05-16 17:19:47 +00:00
kv, err = session.DecodeGob(contain)
if err != nil {
return nil, err
}
}
rs := &MemcacheSessionStore{sid: sid, values: kv, maxlifetime: rp.maxlifetime}
2014-05-16 17:19:47 +00:00
return rs, nil
}
// delete redis session by id
func (rp *MemProvider) SessionDestroy(sid string) error {
if client == nil {
if err := rp.connectInit(); err != nil {
return err
}
2014-05-16 17:19:47 +00:00
}
err := client.Delete(sid)
2014-05-16 17:19:47 +00:00
if err != nil {
return err
}
return nil
}
func (rp *MemProvider) connectInit() error {
client = memcache.New(rp.conninfo...)
return nil
}
2014-05-16 17:19:47 +00:00
// Impelment method, no used.
func (rp *MemProvider) SessionGC() {
return
}
// @todo
func (rp *MemProvider) SessionAll() int {
return 0
}
func init() {
session.Register("memcache", mempder)
}