mirror of
https://github.com/astaxie/beego.git
synced 2025-06-11 04:40:39 +00:00
add golint check and fix all golints
This commit is contained in:
@ -11,16 +11,17 @@ import (
|
||||
"github.com/ssdb/gossdb/ssdb"
|
||||
)
|
||||
|
||||
var ssdbProvider = &SsdbProvider{}
|
||||
var ssdbProvider = &Provider{}
|
||||
|
||||
type SsdbProvider struct {
|
||||
// Provider holds ssdb client and configs
|
||||
type Provider struct {
|
||||
client *ssdb.Client
|
||||
host string
|
||||
port int
|
||||
maxLifetime int64
|
||||
}
|
||||
|
||||
func (p *SsdbProvider) connectInit() error {
|
||||
func (p *Provider) connectInit() error {
|
||||
var err error
|
||||
if p.host == "" || p.port == 0 {
|
||||
return errors.New("SessionInit First")
|
||||
@ -29,7 +30,8 @@ func (p *SsdbProvider) connectInit() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *SsdbProvider) SessionInit(maxLifetime int64, savePath string) error {
|
||||
// SessionInit init the ssdb with the config
|
||||
func (p *Provider) SessionInit(maxLifetime int64, savePath string) error {
|
||||
p.maxLifetime = maxLifetime
|
||||
address := strings.Split(savePath, ":")
|
||||
p.host = address[0]
|
||||
@ -41,7 +43,8 @@ func (p *SsdbProvider) SessionInit(maxLifetime int64, savePath string) error {
|
||||
return p.connectInit()
|
||||
}
|
||||
|
||||
func (p *SsdbProvider) SessionRead(sid string) (session.Store, error) {
|
||||
// SessionRead return a ssdb client session Store
|
||||
func (p *Provider) SessionRead(sid string) (session.Store, error) {
|
||||
if p.client == nil {
|
||||
if err := p.connectInit(); err != nil {
|
||||
return nil, err
|
||||
@ -64,7 +67,8 @@ func (p *SsdbProvider) SessionRead(sid string) (session.Store, error) {
|
||||
return rs, nil
|
||||
}
|
||||
|
||||
func (p *SsdbProvider) SessionExist(sid string) bool {
|
||||
// SessionExist judged whether sid is exist in session
|
||||
func (p *Provider) SessionExist(sid string) bool {
|
||||
if p.client == nil {
|
||||
if err := p.connectInit(); err != nil {
|
||||
panic(err)
|
||||
@ -80,7 +84,8 @@ func (p *SsdbProvider) SessionExist(sid string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (p *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
|
||||
// SessionRegenerate regenerate session with new sid and delete oldsid
|
||||
func (p *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
|
||||
//conn.Do("setx", key, v, ttl)
|
||||
if p.client == nil {
|
||||
if err := p.connectInit(); err != nil {
|
||||
@ -112,7 +117,8 @@ func (p *SsdbProvider) SessionRegenerate(oldsid, sid string) (session.Store, err
|
||||
return rs, nil
|
||||
}
|
||||
|
||||
func (p *SsdbProvider) SessionDestroy(sid string) error {
|
||||
// SessionDestroy destroy the sid
|
||||
func (p *Provider) SessionDestroy(sid string) error {
|
||||
if p.client == nil {
|
||||
if err := p.connectInit(); err != nil {
|
||||
return err
|
||||
@ -122,13 +128,16 @@ func (p *SsdbProvider) SessionDestroy(sid string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *SsdbProvider) SessionGC() {
|
||||
// SessionGC not implemented
|
||||
func (p *Provider) SessionGC() {
|
||||
}
|
||||
|
||||
func (p *SsdbProvider) SessionAll() int {
|
||||
// SessionAll not implemented
|
||||
func (p *Provider) SessionAll() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// SessionStore holds the session information which stored in ssdb
|
||||
type SessionStore struct {
|
||||
sid string
|
||||
lock sync.RWMutex
|
||||
@ -137,12 +146,15 @@ type SessionStore struct {
|
||||
client *ssdb.Client
|
||||
}
|
||||
|
||||
// Set the key and value
|
||||
func (s *SessionStore) Set(key, value interface{}) error {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
s.values[key] = value
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get return the value by the key
|
||||
func (s *SessionStore) Get(key interface{}) interface{} {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
@ -152,30 +164,36 @@ func (s *SessionStore) Get(key interface{}) interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete the key in session store
|
||||
func (s *SessionStore) Delete(key interface{}) error {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
delete(s.values, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Flush delete all keys and values
|
||||
func (s *SessionStore) Flush() error {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
s.values = make(map[interface{}]interface{})
|
||||
return nil
|
||||
}
|
||||
|
||||
// SessionID return the sessionID
|
||||
func (s *SessionStore) SessionID() string {
|
||||
return s.sid
|
||||
}
|
||||
|
||||
// SessionRelease Store the keyvalues into ssdb
|
||||
func (s *SessionStore) SessionRelease(w http.ResponseWriter) {
|
||||
b, err := session.EncodeGob(s.values)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
s.client.Do("setx", s.sid, string(b), s.maxLifetime)
|
||||
|
||||
}
|
||||
|
||||
func init() {
|
||||
session.Register("ssdb", ssdbProvider)
|
||||
}
|
||||
|
Reference in New Issue
Block a user