1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-01 09:30:57 +00:00
Beego/server/web/session/couchbase/sess_couchbase.go

249 lines
5.7 KiB
Go
Raw Normal View History

2020-07-22 14:50:08 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
//
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.
// Package couchbase for session provider
//
// 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()
// }
//
// more docs: http://beego.me/docs/module/session.md
package couchbase
import (
2020-08-30 15:39:07 +00:00
"context"
2020-07-22 14:50:08 +00:00
"net/http"
"strings"
"sync"
couchbase "github.com/couchbase/go-couchbase"
"github.com/astaxie/beego/server/web/session"
2020-07-22 14:50:08 +00:00
)
var couchbpder = &Provider{}
// SessionStore store each session
type SessionStore struct {
b *couchbase.Bucket
sid string
lock sync.RWMutex
values map[interface{}]interface{}
maxlifetime int64
}
// Provider couchabse provided
type Provider struct {
maxlifetime int64
savePath string
pool string
bucket string
b *couchbase.Bucket
}
// Set value to couchabse session
2020-08-30 15:39:07 +00:00
func (cs *SessionStore) Set(ctx context.Context, key, value interface{}) error {
2020-07-22 14:50:08 +00:00
cs.lock.Lock()
defer cs.lock.Unlock()
cs.values[key] = value
return nil
}
// Get value from couchabse session
2020-08-30 15:39:07 +00:00
func (cs *SessionStore) Get(ctx context.Context, key interface{}) interface{} {
2020-07-22 14:50:08 +00:00
cs.lock.RLock()
defer cs.lock.RUnlock()
if v, ok := cs.values[key]; ok {
return v
}
return nil
}
// Delete value in couchbase session by given key
2020-08-30 15:39:07 +00:00
func (cs *SessionStore) Delete(ctx context.Context, key interface{}) error {
2020-07-22 14:50:08 +00:00
cs.lock.Lock()
defer cs.lock.Unlock()
delete(cs.values, key)
return nil
}
// Flush Clean all values in couchbase session
2020-08-30 15:39:07 +00:00
func (cs *SessionStore) Flush(context.Context) error {
2020-07-22 14:50:08 +00:00
cs.lock.Lock()
defer cs.lock.Unlock()
cs.values = make(map[interface{}]interface{})
return nil
}
// SessionID Get couchbase session store id
2020-08-30 15:39:07 +00:00
func (cs *SessionStore) SessionID(context.Context) string {
2020-07-22 14:50:08 +00:00
return cs.sid
}
// SessionRelease Write couchbase session with Gob string
2020-08-30 15:39:07 +00:00
func (cs *SessionStore) SessionRelease(ctx context.Context, w http.ResponseWriter) {
2020-07-22 14:50:08 +00:00
defer cs.b.Close()
bo, err := session.EncodeGob(cs.values)
if err != nil {
return
}
cs.b.Set(cs.sid, int(cs.maxlifetime), bo)
}
func (cp *Provider) getBucket() *couchbase.Bucket {
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
}
// SessionInit init couchbase session
// savepath like couchbase server REST/JSON URL
// e.g. http://host:port/, Pool, Bucket
2020-08-30 15:39:07 +00:00
func (cp *Provider) SessionInit(ctx context.Context, maxlifetime int64, savePath string) error {
2020-07-22 14:50:08 +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
}
// SessionRead read couchbase session by sid
2020-08-30 15:39:07 +00:00
func (cp *Provider) SessionRead(ctx context.Context, sid string) (session.Store, error) {
2020-07-22 14:50:08 +00:00
cp.b = cp.getBucket()
var (
kv map[interface{}]interface{}
err error
doc []byte
)
err = cp.b.Get(sid, &doc)
if err != nil {
return nil, err
} else if doc == nil {
kv = make(map[interface{}]interface{})
} else {
kv, err = session.DecodeGob(doc)
if err != nil {
return nil, err
}
}
cs := &SessionStore{b: cp.b, sid: sid, values: kv, maxlifetime: cp.maxlifetime}
return cs, nil
}
// SessionExist Check couchbase session exist.
// it checkes sid exist or not.
2020-08-30 15:39:07 +00:00
func (cp *Provider) SessionExist(ctx context.Context, sid string) (bool, error) {
2020-07-22 14:50:08 +00:00
cp.b = cp.getBucket()
defer cp.b.Close()
var doc []byte
if err := cp.b.Get(sid, &doc); err != nil || doc == nil {
2020-08-05 16:29:22 +00:00
return false, err
2020-07-22 14:50:08 +00:00
}
2020-08-05 16:29:22 +00:00
return true, nil
2020-07-22 14:50:08 +00:00
}
// SessionRegenerate remove oldsid and use sid to generate new session
2020-08-30 15:39:07 +00:00
func (cp *Provider) SessionRegenerate(ctx context.Context, oldsid, sid string) (session.Store, error) {
2020-07-22 14:50:08 +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)
if err != nil {
return nil, err
}
}
cs := &SessionStore{b: cp.b, sid: sid, values: kv, maxlifetime: cp.maxlifetime}
return cs, nil
}
// SessionDestroy Remove bucket in this couchbase
2020-08-30 15:39:07 +00:00
func (cp *Provider) SessionDestroy(ctx context.Context, sid string) error {
2020-07-22 14:50:08 +00:00
cp.b = cp.getBucket()
defer cp.b.Close()
cp.b.Delete(sid)
return nil
}
// SessionGC Recycle
2020-08-30 15:39:07 +00:00
func (cp *Provider) SessionGC(context.Context) {
2020-07-22 14:50:08 +00:00
}
// SessionAll return all active session
2020-08-30 15:39:07 +00:00
func (cp *Provider) SessionAll(context.Context) int {
2020-07-22 14:50:08 +00:00
return 0
}
func init() {
session.Register("couchbase", couchbpder)
}