1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-24 02:30:49 +00:00
Beego/session/couchbase/sess_couchbase.go

244 lines
5.3 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 couchbase for session provider
2014-08-18 08:41:43 +00:00
//
// depend on github.com/couchbaselabs/go-couchbasee
//
// go install github.com/couchbaselabs/go-couchbase
//
// Usage:
// import(
// _ "github.com/astaxie/beego/session/couchbase"
// "github.com/astaxie/beego/session"
// )
//
// func init() {
// globalSessions, _ = session.NewManager("couchbase", ``{"cookieName":"gosessionid","gclifetime":3600,"ProviderConfig":"http://host:port/, Pool, Bucket"}``)
// 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 couchbase
2014-02-19 11:54:16 +00:00
import (
"net/http"
"strings"
"sync"
2016-03-01 13:41:44 +00:00
couchbase "github.com/couchbase/go-couchbase"
"github.com/astaxie/beego/session"
2014-02-19 11:54:16 +00:00
)
2015-09-12 14:53:55 +00:00
var couchbpder = &Provider{}
2014-02-19 11:54:16 +00:00
2015-09-12 14:53:55 +00:00
// SessionStore store each session
type SessionStore struct {
2014-02-19 11:54:16 +00:00
b *couchbase.Bucket
sid string
lock sync.RWMutex
values map[interface{}]interface{}
maxlifetime int64
}
2015-09-12 14:53:55 +00:00
// Provider couchabse provided
type Provider struct {
2014-02-19 11:54:16 +00:00
maxlifetime int64
savePath string
pool string
bucket string
b *couchbase.Bucket
}
2015-09-12 14:53:55 +00:00
// Set value to couchabse session
func (cs *SessionStore) Set(key, value interface{}) error {
2014-02-19 11:54:16 +00:00
cs.lock.Lock()
defer cs.lock.Unlock()
cs.values[key] = value
return nil
}
2015-09-12 14:53:55 +00:00
// Get value from couchabse session
func (cs *SessionStore) Get(key interface{}) interface{} {
2014-02-19 11:54:16 +00:00
cs.lock.RLock()
defer cs.lock.RUnlock()
if v, ok := cs.values[key]; ok {
return v
}
2015-09-12 14:53:55 +00:00
return nil
2014-02-19 11:54:16 +00:00
}
2015-09-12 14:53:55 +00:00
// Delete value in couchbase session by given key
func (cs *SessionStore) Delete(key interface{}) error {
2014-02-19 11:54:16 +00:00
cs.lock.Lock()
defer cs.lock.Unlock()
delete(cs.values, key)
return nil
}
2015-09-12 14:53:55 +00:00
// Flush Clean all values in couchbase session
func (cs *SessionStore) Flush() error {
2014-02-19 11:54:16 +00:00
cs.lock.Lock()
defer cs.lock.Unlock()
cs.values = make(map[interface{}]interface{})
return nil
}
2015-09-12 14:53:55 +00:00
// SessionID Get couchbase session store id
func (cs *SessionStore) SessionID() string {
2014-02-19 11:54:16 +00:00
return cs.sid
}
2015-09-12 14:53:55 +00:00
// SessionRelease Write couchbase session with Gob string
func (cs *SessionStore) SessionRelease(w http.ResponseWriter) {
2014-02-19 11:54:16 +00:00
defer cs.b.Close()
bo, err := session.EncodeGob(cs.values)
2014-02-19 11:54:16 +00:00
if err != nil {
return
}
cs.b.Set(cs.sid, int(cs.maxlifetime), bo)
}
2015-09-12 14:53:55 +00:00
func (cp *Provider) getBucket() *couchbase.Bucket {
2014-02-19 11:54:16 +00:00
c, err := couchbase.Connect(cp.savePath)
if err != nil {
return nil
}
pool, err := c.GetPool(cp.pool)
if err != nil {
return nil
}
bucket, err := pool.GetBucket(cp.bucket)
if err != nil {
return nil
}
return bucket
}
2015-09-12 14:53:55 +00:00
// SessionInit init couchbase session
2014-02-19 11:54:16 +00:00
// savepath like couchbase server REST/JSON URL
// e.g. http://host:port/, Pool, Bucket
2015-09-12 14:53:55 +00:00
func (cp *Provider) SessionInit(maxlifetime int64, savePath string) error {
2014-02-19 11:54:16 +00:00
cp.maxlifetime = maxlifetime
configs := strings.Split(savePath, ",")
if len(configs) > 0 {
cp.savePath = configs[0]
}
if len(configs) > 1 {
cp.pool = configs[1]
}
if len(configs) > 2 {
cp.bucket = configs[2]
}
return nil
}
2015-09-12 14:53:55 +00:00
// SessionRead read couchbase session by sid
func (cp *Provider) SessionRead(sid string) (session.Store, error) {
2014-02-19 11:54:16 +00:00
cp.b = cp.getBucket()
var doc []byte
err := cp.b.Get(sid, &doc)
var kv map[interface{}]interface{}
if doc == nil {
kv = make(map[interface{}]interface{})
} else {
kv, err = session.DecodeGob(doc)
2014-02-19 11:54:16 +00:00
if err != nil {
return nil, err
}
}
2015-09-12 14:53:55 +00:00
cs := &SessionStore{b: cp.b, sid: sid, values: kv, maxlifetime: cp.maxlifetime}
2014-02-19 11:54:16 +00:00
return cs, nil
}
2015-09-12 14:53:55 +00:00
// SessionExist Check couchbase session exist.
// it checkes sid exist or not.
func (cp *Provider) SessionExist(sid string) bool {
2014-02-19 11:54:16 +00:00
cp.b = cp.getBucket()
defer cp.b.Close()
var doc []byte
if err := cp.b.Get(sid, &doc); err != nil || doc == nil {
return false
}
2015-09-12 14:53:55 +00:00
return true
2014-02-19 11:54:16 +00:00
}
2015-09-12 14:53:55 +00:00
// SessionRegenerate remove oldsid and use sid to generate new session
func (cp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
2014-02-19 11:54:16 +00:00
cp.b = cp.getBucket()
var doc []byte
if err := cp.b.Get(oldsid, &doc); err != nil || doc == nil {
cp.b.Set(sid, int(cp.maxlifetime), "")
} else {
err := cp.b.Delete(oldsid)
if err != nil {
return nil, err
}
_, _ = cp.b.Add(sid, int(cp.maxlifetime), doc)
}
err := cp.b.Get(sid, &doc)
if err != nil {
return nil, err
}
var kv map[interface{}]interface{}
if doc == nil {
kv = make(map[interface{}]interface{})
} else {
kv, err = session.DecodeGob(doc)
2014-02-19 11:54:16 +00:00
if err != nil {
return nil, err
}
}
2015-09-12 14:53:55 +00:00
cs := &SessionStore{b: cp.b, sid: sid, values: kv, maxlifetime: cp.maxlifetime}
2014-02-19 11:54:16 +00:00
return cs, nil
}
2015-09-12 14:53:55 +00:00
// SessionDestroy Remove bucket in this couchbase
func (cp *Provider) SessionDestroy(sid string) error {
2014-02-19 11:54:16 +00:00
cp.b = cp.getBucket()
defer cp.b.Close()
cp.b.Delete(sid)
return nil
}
2015-09-12 14:53:55 +00:00
// SessionGC Recycle
func (cp *Provider) SessionGC() {
2014-02-19 11:54:16 +00:00
return
}
2015-09-12 14:53:55 +00:00
// SessionAll return all active session
func (cp *Provider) SessionAll() int {
2014-02-19 11:54:16 +00:00
return 0
}
func init() {
session.Register("couchbase", couchbpder)
2014-02-19 11:54:16 +00:00
}