1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 09:40:56 +00:00

beego: fix dependency of cache / session sub package

This commit is contained in:
slene 2014-04-04 08:31:22 +08:00
parent acbdeb62e8
commit f4d62d3193
8 changed files with 53 additions and 39 deletions

View File

@ -5,6 +5,8 @@ import (
"errors" "errors"
"github.com/beego/memcache" "github.com/beego/memcache"
"github.com/astaxie/beego/cache"
) )
// Memcache adapter. // Memcache adapter.
@ -147,5 +149,5 @@ func (rc *MemcacheCache) connectInit() (*memcache.Connection, error) {
} }
func init() { func init() {
Register("memcache", NewMemCache()) cache.Register("memcache", NewMemCache())
} }

View File

@ -6,6 +6,8 @@ import (
"time" "time"
"github.com/beego/redigo/redis" "github.com/beego/redigo/redis"
"github.com/astaxie/beego/cache"
) )
var ( var (
@ -130,5 +132,5 @@ func (rc *RedisCache) connectInit() {
} }
func init() { func init() {
Register("redis", NewRedisCache()) cache.Register("redis", NewRedisCache())
} }

View File

@ -1,10 +1,13 @@
package session package session
import ( import (
"github.com/couchbaselabs/go-couchbase"
"net/http" "net/http"
"strings" "strings"
"sync" "sync"
"github.com/couchbaselabs/go-couchbase"
"github.com/astaxie/beego/session"
) )
var couchbpder = &CouchbaseProvider{} var couchbpder = &CouchbaseProvider{}
@ -70,7 +73,7 @@ func (cs *CouchbaseSessionStore) SessionRelease(w http.ResponseWriter) {
return return
} }
bo, err := encodeGob(cs.values) bo, err := session.EncodeGob(cs.values)
if err != nil { if err != nil {
return return
} }
@ -117,7 +120,7 @@ func (cp *CouchbaseProvider) SessionInit(maxlifetime int64, savePath string) err
} }
// read couchbase session by sid // read couchbase session by sid
func (cp *CouchbaseProvider) SessionRead(sid string) (SessionStore, error) { func (cp *CouchbaseProvider) SessionRead(sid string) (session.SessionStore, error) {
cp.b = cp.getBucket() cp.b = cp.getBucket()
var doc []byte var doc []byte
@ -127,7 +130,7 @@ func (cp *CouchbaseProvider) SessionRead(sid string) (SessionStore, error) {
if doc == nil { if doc == nil {
kv = make(map[interface{}]interface{}) kv = make(map[interface{}]interface{})
} else { } else {
kv, err = decodeGob(doc) kv, err = session.DecodeGob(doc)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -150,7 +153,7 @@ func (cp *CouchbaseProvider) SessionExist(sid string) bool {
} }
} }
func (cp *CouchbaseProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) { func (cp *CouchbaseProvider) SessionRegenerate(oldsid, sid string) (session.SessionStore, error) {
cp.b = cp.getBucket() cp.b = cp.getBucket()
var doc []byte var doc []byte
@ -172,7 +175,7 @@ func (cp *CouchbaseProvider) SessionRegenerate(oldsid, sid string) (SessionStore
if doc == nil { if doc == nil {
kv = make(map[interface{}]interface{}) kv = make(map[interface{}]interface{})
} else { } else {
kv, err = decodeGob(doc) kv, err = session.DecodeGob(doc)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -199,5 +202,5 @@ func (cp *CouchbaseProvider) SessionAll() int {
} }
func init() { func init() {
Register("couchbase", couchbpder) session.Register("couchbase", couchbpder)
} }

View File

@ -14,6 +14,8 @@ import (
"sync" "sync"
"time" "time"
"github.com/astaxie/beego/session"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
) )
@ -73,7 +75,7 @@ func (st *MysqlSessionStore) SessionID() string {
// must call this method to save values to database. // must call this method to save values to database.
func (st *MysqlSessionStore) SessionRelease(w http.ResponseWriter) { func (st *MysqlSessionStore) SessionRelease(w http.ResponseWriter) {
defer st.c.Close() defer st.c.Close()
b, err := encodeGob(st.values) b, err := session.EncodeGob(st.values)
if err != nil { if err != nil {
return return
} }
@ -106,7 +108,7 @@ func (mp *MysqlProvider) SessionInit(maxlifetime int64, savePath string) error {
} }
// get mysql session by sid // get mysql session by sid
func (mp *MysqlProvider) SessionRead(sid string) (SessionStore, error) { func (mp *MysqlProvider) SessionRead(sid string) (session.SessionStore, error) {
c := mp.connectInit() c := mp.connectInit()
row := c.QueryRow("select session_data from session where session_key=?", sid) row := c.QueryRow("select session_data from session where session_key=?", sid)
var sessiondata []byte var sessiondata []byte
@ -119,7 +121,7 @@ func (mp *MysqlProvider) SessionRead(sid string) (SessionStore, error) {
if len(sessiondata) == 0 { if len(sessiondata) == 0 {
kv = make(map[interface{}]interface{}) kv = make(map[interface{}]interface{})
} else { } else {
kv, err = decodeGob(sessiondata) kv, err = session.DecodeGob(sessiondata)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -143,7 +145,7 @@ func (mp *MysqlProvider) SessionExist(sid string) bool {
} }
// generate new sid for mysql session // generate new sid for mysql session
func (mp *MysqlProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) { func (mp *MysqlProvider) SessionRegenerate(oldsid, sid string) (session.SessionStore, error) {
c := mp.connectInit() c := mp.connectInit()
row := c.QueryRow("select session_data from session where session_key=?", oldsid) row := c.QueryRow("select session_data from session where session_key=?", oldsid)
var sessiondata []byte var sessiondata []byte
@ -156,7 +158,7 @@ func (mp *MysqlProvider) SessionRegenerate(oldsid, sid string) (SessionStore, er
if len(sessiondata) == 0 { if len(sessiondata) == 0 {
kv = make(map[interface{}]interface{}) kv = make(map[interface{}]interface{})
} else { } else {
kv, err = decodeGob(sessiondata) kv, err = session.DecodeGob(sessiondata)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -194,5 +196,5 @@ func (mp *MysqlProvider) SessionAll() int {
} }
func init() { func init() {
Register("mysql", mysqlpder) session.Register("mysql", mysqlpder)
} }

View File

@ -34,6 +34,9 @@ import (
"net/http" "net/http"
"sync" "sync"
"time" "time"
"github.com/astaxie/beego/session"
_ "github.com/lib/pq" _ "github.com/lib/pq"
) )
@ -93,7 +96,7 @@ func (st *PostgresqlSessionStore) SessionID() string {
// must call this method to save values to database. // must call this method to save values to database.
func (st *PostgresqlSessionStore) SessionRelease(w http.ResponseWriter) { func (st *PostgresqlSessionStore) SessionRelease(w http.ResponseWriter) {
defer st.c.Close() defer st.c.Close()
b, err := encodeGob(st.values) b, err := session.EncodeGob(st.values)
if err != nil { if err != nil {
return return
} }
@ -126,7 +129,7 @@ func (mp *PostgresqlProvider) SessionInit(maxlifetime int64, savePath string) er
} }
// get postgresql session by sid // get postgresql session by sid
func (mp *PostgresqlProvider) SessionRead(sid string) (SessionStore, error) { func (mp *PostgresqlProvider) SessionRead(sid string) (session.SessionStore, error) {
c := mp.connectInit() c := mp.connectInit()
row := c.QueryRow("select session_data from session where session_key=$1", sid) row := c.QueryRow("select session_data from session where session_key=$1", sid)
var sessiondata []byte var sessiondata []byte
@ -146,7 +149,7 @@ func (mp *PostgresqlProvider) SessionRead(sid string) (SessionStore, error) {
if len(sessiondata) == 0 { if len(sessiondata) == 0 {
kv = make(map[interface{}]interface{}) kv = make(map[interface{}]interface{})
} else { } else {
kv, err = decodeGob(sessiondata) kv, err = session.DecodeGob(sessiondata)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -171,7 +174,7 @@ func (mp *PostgresqlProvider) SessionExist(sid string) bool {
} }
// generate new sid for postgresql session // generate new sid for postgresql session
func (mp *PostgresqlProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) { func (mp *PostgresqlProvider) SessionRegenerate(oldsid, sid string) (session.SessionStore, error) {
c := mp.connectInit() c := mp.connectInit()
row := c.QueryRow("select session_data from session where session_key=$1", oldsid) row := c.QueryRow("select session_data from session where session_key=$1", oldsid)
var sessiondata []byte var sessiondata []byte
@ -185,7 +188,7 @@ func (mp *PostgresqlProvider) SessionRegenerate(oldsid, sid string) (SessionStor
if len(sessiondata) == 0 { if len(sessiondata) == 0 {
kv = make(map[interface{}]interface{}) kv = make(map[interface{}]interface{})
} else { } else {
kv, err = decodeGob(sessiondata) kv, err = session.DecodeGob(sessiondata)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -223,5 +226,5 @@ func (mp *PostgresqlProvider) SessionAll() int {
} }
func init() { func init() {
Register("postgresql", postgresqlpder) session.Register("postgresql", postgresqlpder)
} }

View File

@ -6,6 +6,8 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/astaxie/beego/session"
"github.com/beego/redigo/redis" "github.com/beego/redigo/redis"
) )
@ -77,7 +79,7 @@ func (rs *RedisSessionStore) SessionRelease(w http.ResponseWriter) {
return return
} }
b, err := encodeGob(rs.values) b, err := session.EncodeGob(rs.values)
if err != nil { if err != nil {
return return
} }
@ -134,7 +136,7 @@ func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
} }
// read redis session by sid // read redis session by sid
func (rp *RedisProvider) SessionRead(sid string) (SessionStore, error) { func (rp *RedisProvider) SessionRead(sid string) (session.SessionStore, error) {
c := rp.poollist.Get() c := rp.poollist.Get()
defer c.Close() defer c.Close()
@ -143,7 +145,7 @@ func (rp *RedisProvider) SessionRead(sid string) (SessionStore, error) {
if len(kvs) == 0 { if len(kvs) == 0 {
kv = make(map[interface{}]interface{}) kv = make(map[interface{}]interface{})
} else { } else {
kv, err = decodeGob([]byte(kvs)) kv, err = session.DecodeGob([]byte(kvs))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -166,7 +168,7 @@ func (rp *RedisProvider) SessionExist(sid string) bool {
} }
// generate new sid for redis session // generate new sid for redis session
func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (SessionStore, error) { func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (session.SessionStore, error) {
c := rp.poollist.Get() c := rp.poollist.Get()
defer c.Close() defer c.Close()
@ -185,7 +187,7 @@ func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (SessionStore, er
if len(kvs) == 0 { if len(kvs) == 0 {
kv = make(map[interface{}]interface{}) kv = make(map[interface{}]interface{})
} else { } else {
kv, err = decodeGob([]byte(kvs)) kv, err = session.DecodeGob([]byte(kvs))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -215,5 +217,5 @@ func (rp *RedisProvider) SessionAll() int {
} }
func init() { func init() {
Register("redis", redispder) session.Register("redis", redispder)
} }

View File

@ -70,7 +70,7 @@ func (fs *FileSessionStore) SessionID() string {
// Write file session to local file with Gob string // Write file session to local file with Gob string
func (fs *FileSessionStore) SessionRelease(w http.ResponseWriter) { func (fs *FileSessionStore) SessionRelease(w http.ResponseWriter) {
defer fs.f.Close() defer fs.f.Close()
b, err := encodeGob(fs.values) b, err := EncodeGob(fs.values)
if err != nil { if err != nil {
return return
} }
@ -123,7 +123,7 @@ func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) {
if len(b) == 0 { if len(b) == 0 {
kv = make(map[interface{}]interface{}) kv = make(map[interface{}]interface{})
} else { } else {
kv, err = decodeGob(b) kv, err = DecodeGob(b)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -222,7 +222,7 @@ func (fp *FileProvider) SessionRegenerate(oldsid, sid string) (SessionStore, err
if len(b) == 0 { if len(b) == 0 {
kv = make(map[interface{}]interface{}) kv = make(map[interface{}]interface{})
} else { } else {
kv, err = decodeGob(b) kv, err = DecodeGob(b)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -27,7 +27,7 @@ func init() {
gob.Register(map[int]int64{}) gob.Register(map[int]int64{})
} }
func encodeGob(obj map[interface{}]interface{}) ([]byte, error) { func EncodeGob(obj map[interface{}]interface{}) ([]byte, error) {
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
enc := gob.NewEncoder(buf) enc := gob.NewEncoder(buf)
err := enc.Encode(obj) err := enc.Encode(obj)
@ -37,7 +37,7 @@ func encodeGob(obj map[interface{}]interface{}) ([]byte, error) {
return buf.Bytes(), nil return buf.Bytes(), nil
} }
func decodeGob(encoded []byte) (map[interface{}]interface{}, error) { func DecodeGob(encoded []byte) (map[interface{}]interface{}, error) {
buf := bytes.NewBuffer(encoded) buf := bytes.NewBuffer(encoded)
dec := gob.NewDecoder(buf) dec := gob.NewDecoder(buf)
var out map[interface{}]interface{} var out map[interface{}]interface{}
@ -97,8 +97,8 @@ func decrypt(block cipher.Block, value []byte) ([]byte, error) {
func encodeCookie(block cipher.Block, hashKey, name string, value map[interface{}]interface{}) (string, error) { func encodeCookie(block cipher.Block, hashKey, name string, value map[interface{}]interface{}) (string, error) {
var err error var err error
var b []byte var b []byte
// 1. encodeGob. // 1. EncodeGob.
if b, err = encodeGob(value); err != nil { if b, err = EncodeGob(value); err != nil {
return "", err return "", err
} }
// 2. Encrypt (optional). // 2. Encrypt (optional).
@ -158,8 +158,8 @@ func decodeCookie(block cipher.Block, hashKey, name, value string, gcmaxlifetime
if b, err = decrypt(block, b); err != nil { if b, err = decrypt(block, b); err != nil {
return nil, err return nil, err
} }
// 5. decodeGob. // 5. DecodeGob.
if dst, err := decodeGob(b); err != nil { if dst, err := DecodeGob(b); err != nil {
return nil, err return nil, err
} else { } else {
return dst, nil return dst, nil