1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-22 22:10:48 +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"
"github.com/beego/memcache"
"github.com/astaxie/beego/cache"
)
// Memcache adapter.
@ -147,5 +149,5 @@ func (rc *MemcacheCache) connectInit() (*memcache.Connection, error) {
}
func init() {
Register("memcache", NewMemCache())
cache.Register("memcache", NewMemCache())
}

View File

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

View File

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

View File

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

View File

@ -34,6 +34,9 @@ import (
"net/http"
"sync"
"time"
"github.com/astaxie/beego/session"
_ "github.com/lib/pq"
)
@ -93,7 +96,7 @@ func (st *PostgresqlSessionStore) SessionID() string {
// must call this method to save values to database.
func (st *PostgresqlSessionStore) SessionRelease(w http.ResponseWriter) {
defer st.c.Close()
b, err := encodeGob(st.values)
b, err := session.EncodeGob(st.values)
if err != nil {
return
}
@ -126,7 +129,7 @@ func (mp *PostgresqlProvider) SessionInit(maxlifetime int64, savePath string) er
}
// 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()
row := c.QueryRow("select session_data from session where session_key=$1", sid)
var sessiondata []byte
@ -138,7 +141,7 @@ func (mp *PostgresqlProvider) SessionRead(sid string) (SessionStore, error) {
if err != nil {
return nil, err
}
} else if err != nil {
} else if err != nil {
return nil, err
}
@ -146,7 +149,7 @@ func (mp *PostgresqlProvider) SessionRead(sid string) (SessionStore, error) {
if len(sessiondata) == 0 {
kv = make(map[interface{}]interface{})
} else {
kv, err = decodeGob(sessiondata)
kv, err = session.DecodeGob(sessiondata)
if err != nil {
return nil, err
}
@ -162,7 +165,7 @@ func (mp *PostgresqlProvider) SessionExist(sid string) bool {
row := c.QueryRow("select session_data from session where session_key=$1", sid)
var sessiondata []byte
err := row.Scan(&sessiondata)
if err == sql.ErrNoRows {
return false
} else {
@ -171,21 +174,21 @@ func (mp *PostgresqlProvider) SessionExist(sid string) bool {
}
// 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()
row := c.QueryRow("select session_data from session where session_key=$1", oldsid)
var sessiondata []byte
err := row.Scan(&sessiondata)
if err == sql.ErrNoRows {
c.Exec("insert into session(session_key,session_data,session_expiry) values($1,$2,$3)",
oldsid, "", time.Now().Format(time.RFC3339))
oldsid, "", time.Now().Format(time.RFC3339))
}
c.Exec("update session set session_key=$1 where session_key=$2", sid, oldsid)
var kv map[interface{}]interface{}
if len(sessiondata) == 0 {
kv = make(map[interface{}]interface{})
} else {
kv, err = decodeGob(sessiondata)
kv, err = session.DecodeGob(sessiondata)
if err != nil {
return nil, err
}
@ -223,5 +226,5 @@ func (mp *PostgresqlProvider) SessionAll() int {
}
func init() {
Register("postgresql", postgresqlpder)
session.Register("postgresql", postgresqlpder)
}

View File

@ -6,6 +6,8 @@ import (
"strings"
"sync"
"github.com/astaxie/beego/session"
"github.com/beego/redigo/redis"
)
@ -77,7 +79,7 @@ func (rs *RedisSessionStore) SessionRelease(w http.ResponseWriter) {
return
}
b, err := encodeGob(rs.values)
b, err := session.EncodeGob(rs.values)
if err != nil {
return
}
@ -134,7 +136,7 @@ func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
}
// 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()
defer c.Close()
@ -143,7 +145,7 @@ func (rp *RedisProvider) SessionRead(sid string) (SessionStore, error) {
if len(kvs) == 0 {
kv = make(map[interface{}]interface{})
} else {
kv, err = decodeGob([]byte(kvs))
kv, err = session.DecodeGob([]byte(kvs))
if err != nil {
return nil, err
}
@ -166,7 +168,7 @@ func (rp *RedisProvider) SessionExist(sid string) bool {
}
// 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()
defer c.Close()
@ -185,7 +187,7 @@ func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (SessionStore, er
if len(kvs) == 0 {
kv = make(map[interface{}]interface{})
} else {
kv, err = decodeGob([]byte(kvs))
kv, err = session.DecodeGob([]byte(kvs))
if err != nil {
return nil, err
}
@ -215,5 +217,5 @@ func (rp *RedisProvider) SessionAll() int {
}
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
func (fs *FileSessionStore) SessionRelease(w http.ResponseWriter) {
defer fs.f.Close()
b, err := encodeGob(fs.values)
b, err := EncodeGob(fs.values)
if err != nil {
return
}
@ -123,7 +123,7 @@ func (fp *FileProvider) SessionRead(sid string) (SessionStore, error) {
if len(b) == 0 {
kv = make(map[interface{}]interface{})
} else {
kv, err = decodeGob(b)
kv, err = DecodeGob(b)
if err != nil {
return nil, err
}
@ -222,7 +222,7 @@ func (fp *FileProvider) SessionRegenerate(oldsid, sid string) (SessionStore, err
if len(b) == 0 {
kv = make(map[interface{}]interface{})
} else {
kv, err = decodeGob(b)
kv, err = DecodeGob(b)
if err != nil {
return nil, err
}

View File

@ -27,7 +27,7 @@ func init() {
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)
enc := gob.NewEncoder(buf)
err := enc.Encode(obj)
@ -37,7 +37,7 @@ func encodeGob(obj map[interface{}]interface{}) ([]byte, error) {
return buf.Bytes(), nil
}
func decodeGob(encoded []byte) (map[interface{}]interface{}, error) {
func DecodeGob(encoded []byte) (map[interface{}]interface{}, error) {
buf := bytes.NewBuffer(encoded)
dec := gob.NewDecoder(buf)
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) {
var err error
var b []byte
// 1. encodeGob.
if b, err = encodeGob(value); err != nil {
// 1. EncodeGob.
if b, err = EncodeGob(value); err != nil {
return "", err
}
// 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 {
return nil, err
}
// 5. decodeGob.
if dst, err := decodeGob(b); err != nil {
// 5. DecodeGob.
if dst, err := DecodeGob(b); err != nil {
return nil, err
} else {
return dst, nil