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

233 lines
5.5 KiB
Go
Raw Normal View History

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-12 14:53:55 +00:00
// Package memcache for session provider
2014-08-18 08:41:43 +00:00
//
// depend on github.com/bradfitz/gomemcache/memcache
//
// go install github.com/bradfitz/gomemcache/memcache
//
// Usage:
// import(
// _ "github.com/astaxie/beego/session/memcache"
// "github.com/astaxie/beego/session"
// )
//
// func init() {
// globalSessions, _ = session.NewManager("memcache", ``{"cookieName":"gosessionid","gclifetime":3600,"ProviderConfig":"127.0.0.1:11211"}``)
// go globalSessions.GC()
// }
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// more docs: http://beego.me/docs/module/session.md
2015-09-12 14:53:55 +00:00
package memcache
2014-05-16 17:19:47 +00:00
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
2015-09-12 14:53:55 +00:00
// SessionStore memcache session store
type SessionStore struct {
2014-05-16 17:19:47 +00:00
sid string
lock sync.RWMutex
values map[interface{}]interface{}
maxlifetime int64
}
2015-09-12 14:53:55 +00:00
// Set value in memcache session
func (rs *SessionStore) Set(key, value interface{}) error {
2014-05-16 17:19:47 +00:00
rs.lock.Lock()
defer rs.lock.Unlock()
rs.values[key] = value
return nil
}
2015-09-12 14:53:55 +00:00
// Get value in memcache session
func (rs *SessionStore) Get(key interface{}) interface{} {
2014-05-16 17:19:47 +00:00
rs.lock.RLock()
defer rs.lock.RUnlock()
if v, ok := rs.values[key]; ok {
return v
}
2015-09-12 14:53:55 +00:00
return nil
2014-05-16 17:19:47 +00:00
}
2015-09-12 14:53:55 +00:00
// Delete value in memcache session
func (rs *SessionStore) Delete(key interface{}) error {
2014-05-16 17:19:47 +00:00
rs.lock.Lock()
defer rs.lock.Unlock()
delete(rs.values, key)
return nil
}
2015-09-12 14:53:55 +00:00
// Flush clear all values in memcache session
func (rs *SessionStore) Flush() error {
2014-05-16 17:19:47 +00:00
rs.lock.Lock()
defer rs.lock.Unlock()
rs.values = make(map[interface{}]interface{})
return nil
}
2015-09-12 14:53:55 +00:00
// SessionID get memcache session id
func (rs *SessionStore) SessionID() string {
2014-05-16 17:19:47 +00:00
return rs.sid
}
2015-09-12 14:53:55 +00:00
// SessionRelease save session values to memcache
func (rs *SessionStore) SessionRelease(w http.ResponseWriter) {
2014-05-16 17:19:47 +00:00
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
}
2015-09-12 14:53:55 +00:00
// MemProvider memcache session provider
2014-05-16 17:19:47 +00:00
type MemProvider struct {
maxlifetime int64
conninfo []string
2014-05-16 17:19:47 +00:00
poolsize int
password string
}
2015-09-12 14:53:55 +00:00
// SessionInit init memcache session
2014-05-16 17:19:47 +00:00
// 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
}
2015-09-12 14:53:55 +00:00
// SessionRead read memcache session by sid
func (rp *MemProvider) SessionRead(sid string) (session.Store, 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)
2015-05-28 04:04:19 +00:00
if err != nil && err == memcache.ErrCacheMiss {
2015-09-12 14:53:55 +00:00
rs := &SessionStore{sid: sid, values: make(map[interface{}]interface{}), maxlifetime: rp.maxlifetime}
2015-05-28 04:04:19 +00:00
return rs, nil
2014-05-16 17:19:47 +00:00
}
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
}
}
2015-09-12 14:53:55 +00:00
rs := &SessionStore{sid: sid, values: kv, maxlifetime: rp.maxlifetime}
2014-05-16 17:19:47 +00:00
return rs, nil
}
2015-09-12 14:53:55 +00:00
// SessionExist check memcache session exist by sid
2014-05-16 17:19:47 +00:00
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
}
2015-09-12 14:53:55 +00:00
return true
2014-05-16 17:19:47 +00:00
}
2015-09-12 14:53:55 +00:00
// SessionRegenerate generate new sid for memcache session
func (rp *MemProvider) SessionRegenerate(oldsid, sid string) (session.Store, 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.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
}
}
2015-09-12 14:53:55 +00:00
rs := &SessionStore{sid: sid, values: kv, maxlifetime: rp.maxlifetime}
2014-05-16 17:19:47 +00:00
return rs, nil
}
2015-09-12 14:53:55 +00:00
// SessionDestroy delete memcache session by id
2014-05-16 17:19:47 +00:00
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
}
2015-09-12 14:53:55 +00:00
// SessionGC Impelment method, no used.
2014-05-16 17:19:47 +00:00
func (rp *MemProvider) SessionGC() {
return
}
2015-09-12 14:53:55 +00:00
// SessionAll return all activeSession
2014-05-16 17:19:47 +00:00
func (rp *MemProvider) SessionAll() int {
return 0
}
func init() {
session.Register("memcache", mempder)
}