1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 04:20:55 +00:00
Beego/session/redis/sess_redis.go

262 lines
6.0 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 redis for session provider
2014-08-18 08:41:43 +00:00
//
// depend on github.com/gomodule/redigo/redis
2014-08-18 08:41:43 +00:00
//
// go install github.com/gomodule/redigo/redis
2014-08-18 08:41:43 +00:00
//
// Usage:
// import(
// _ "github.com/astaxie/beego/session/redis"
// "github.com/astaxie/beego/session"
// )
//
// func init() {
// globalSessions, _ = session.NewManager("redis", ``{"cookieName":"gosessionid","gclifetime":3600,"ProviderConfig":"127.0.0.1:7070"}``)
// 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 redis
import (
2014-01-05 06:48:36 +00:00
"net/http"
2013-10-28 14:19:37 +00:00
"strconv"
"strings"
2013-11-05 13:59:35 +00:00
"sync"
2018-07-12 02:48:50 +00:00
"time"
2013-12-03 13:37:39 +00:00
"github.com/astaxie/beego/session"
"github.com/gomodule/redigo/redis"
)
2015-09-12 14:53:55 +00:00
var redispder = &Provider{}
2016-01-17 16:18:21 +00:00
// MaxPoolSize redis max pool size
2015-09-12 14:53:55 +00:00
var MaxPoolSize = 100
2013-05-14 15:55:50 +00:00
2015-09-12 14:53:55 +00:00
// SessionStore redis session store
type SessionStore struct {
2014-01-21 10:48:16 +00:00
p *redis.Pool
2013-12-14 15:29:17 +00:00
sid string
lock sync.RWMutex
values map[interface{}]interface{}
maxlifetime int64
}
2015-09-12 14:53:55 +00:00
// Set value in redis session
func (rs *SessionStore) Set(key, value interface{}) error {
2013-11-05 13:59:35 +00:00
rs.lock.Lock()
defer rs.lock.Unlock()
rs.values[key] = value
return nil
}
2015-09-12 14:53:55 +00:00
// Get value in redis session
func (rs *SessionStore) Get(key interface{}) interface{} {
2013-11-05 13:59:35 +00:00
rs.lock.RLock()
defer rs.lock.RUnlock()
if v, ok := rs.values[key]; ok {
return v
}
2015-09-12 14:53:55 +00:00
return nil
}
2015-09-12 14:53:55 +00:00
// Delete value in redis session
func (rs *SessionStore) Delete(key interface{}) error {
2013-11-05 13:59:35 +00:00
rs.lock.Lock()
defer rs.lock.Unlock()
delete(rs.values, key)
return nil
}
2015-09-12 14:53:55 +00:00
// Flush clear all values in redis session
func (rs *SessionStore) Flush() error {
2013-11-05 13:59:35 +00:00
rs.lock.Lock()
defer rs.lock.Unlock()
rs.values = make(map[interface{}]interface{})
return nil
2013-09-26 10:07:00 +00:00
}
2015-09-12 14:53:55 +00:00
// SessionID get redis session id
func (rs *SessionStore) SessionID() string {
return rs.sid
}
2015-09-12 14:53:55 +00:00
// SessionRelease save session values to redis
func (rs *SessionStore) SessionRelease(w http.ResponseWriter) {
b, err := session.EncodeGob(rs.values)
if err != nil {
return
2013-11-05 13:59:35 +00:00
}
c := rs.p.Get()
defer c.Close()
2016-01-18 08:11:27 +00:00
c.Do("SETEX", rs.sid, rs.maxlifetime, string(b))
}
2015-09-12 14:53:55 +00:00
// Provider redis session provider
type Provider struct {
maxlifetime int64
savePath string
2013-10-28 14:19:37 +00:00
poolsize int
password string
dbNum int
2013-10-28 14:19:37 +00:00
poollist *redis.Pool
}
2015-09-12 14:53:55 +00:00
// SessionInit init redis session
2018-07-12 02:48:50 +00:00
// savepath like redis server addr,pool size,password,dbnum,IdleTimeout second
// e.g. 127.0.0.1:6379,100,astaxie,0,30
2015-09-12 14:53:55 +00:00
func (rp *Provider) SessionInit(maxlifetime int64, savePath string) error {
2013-10-28 14:19:37 +00:00
rp.maxlifetime = maxlifetime
configs := strings.Split(savePath, ",")
if len(configs) > 0 {
rp.savePath = configs[0]
}
2013-10-28 14:19:37 +00:00
if len(configs) > 1 {
poolsize, err := strconv.Atoi(configs[1])
if err != nil || poolsize < 0 {
2015-09-12 14:53:55 +00:00
rp.poolsize = MaxPoolSize
2013-10-28 14:19:37 +00:00
} else {
rp.poolsize = poolsize
}
} else {
2015-09-12 14:53:55 +00:00
rp.poolsize = MaxPoolSize
2013-05-14 15:55:50 +00:00
}
2013-10-28 14:19:37 +00:00
if len(configs) > 2 {
rp.password = configs[2]
2013-05-14 15:55:50 +00:00
}
if len(configs) > 3 {
2015-05-13 13:20:50 +00:00
dbnum, err := strconv.Atoi(configs[3])
if err != nil || dbnum < 0 {
rp.dbNum = 0
} else {
rp.dbNum = dbnum
}
} else {
rp.dbNum = 0
}
2018-07-12 02:48:50 +00:00
var idleTimeout time.Duration = 0
if len(configs) > 4 {
timeout, err := strconv.Atoi(configs[4])
if err == nil && timeout > 0 {
idleTimeout = time.Duration(timeout) * time.Second
}
}
rp.poollist = &redis.Pool{
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", rp.savePath)
if err != nil {
return nil, err
}
if rp.password != "" {
if _, err = c.Do("AUTH", rp.password); err != nil {
c.Close()
return nil, err
}
}
// some redis proxy such as twemproxy is not support select command
if rp.dbNum > 0 {
_, err = c.Do("SELECT", rp.dbNum)
if err != nil {
c.Close()
return nil, err
}
}
return c, err
},
2018-07-12 02:48:50 +00:00
MaxIdle: rp.poolsize,
}
2018-07-12 02:48:50 +00:00
rp.poollist.IdleTimeout = idleTimeout
return rp.poollist.Get().Err()
}
2015-09-12 14:53:55 +00:00
// SessionRead read redis session by sid
func (rp *Provider) SessionRead(sid string) (session.Store, error) {
2013-10-28 14:19:37 +00:00
c := rp.poollist.Get()
2014-01-21 10:48:16 +00:00
defer c.Close()
2017-04-30 15:38:48 +00:00
var kv map[interface{}]interface{}
2017-04-30 15:38:48 +00:00
kvs, err := redis.String(c.Do("GET", sid))
2017-05-02 04:47:15 +00:00
if err != nil && err != redis.ErrNil {
return nil, err
}
2013-11-05 15:41:01 +00:00
if len(kvs) == 0 {
kv = make(map[interface{}]interface{})
} else {
if kv, err = session.DecodeGob([]byte(kvs)); err != nil {
2013-11-05 15:41:01 +00:00
return nil, err
2013-11-05 13:59:35 +00:00
}
}
2014-01-21 10:48:16 +00:00
2015-09-12 14:53:55 +00:00
rs := &SessionStore{p: rp.poollist, sid: sid, values: kv, maxlifetime: rp.maxlifetime}
return rs, nil
}
2015-09-12 14:53:55 +00:00
// SessionExist check redis session exist by sid
func (rp *Provider) SessionExist(sid string) bool {
2013-11-05 14:23:48 +00:00
c := rp.poollist.Get()
2013-12-14 15:29:17 +00:00
defer c.Close()
2014-01-21 10:48:16 +00:00
2013-11-05 15:41:01 +00:00
if existed, err := redis.Int(c.Do("EXISTS", sid)); err != nil || existed == 0 {
2013-11-05 14:23:48 +00:00
return false
}
2015-09-12 14:53:55 +00:00
return true
2013-11-05 14:23:48 +00:00
}
2015-09-12 14:53:55 +00:00
// SessionRegenerate generate new sid for redis session
func (rp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
2013-10-28 14:19:37 +00:00
c := rp.poollist.Get()
2014-01-21 10:48:16 +00:00
defer c.Close()
if existed, _ := redis.Int(c.Do("EXISTS", oldsid)); existed == 0 {
// oldsid doesn't exists, set the new sid directly
// ignore error here, since if it return error
// the existed value will be 0
c.Do("SET", sid, "", "EX", rp.maxlifetime)
} else {
c.Do("RENAME", oldsid, sid)
c.Do("EXPIRE", sid, rp.maxlifetime)
}
return rp.SessionRead(sid)
2013-09-26 10:07:00 +00:00
}
2015-09-12 14:53:55 +00:00
// SessionDestroy delete redis session by id
func (rp *Provider) SessionDestroy(sid string) error {
2013-10-28 14:19:37 +00:00
c := rp.poollist.Get()
2013-12-14 15:29:17 +00:00
defer c.Close()
2014-01-21 10:48:16 +00:00
c.Do("DEL", sid)
return nil
}
2015-09-12 14:53:55 +00:00
// SessionGC Impelment method, no used.
func (rp *Provider) SessionGC() {
}
2015-09-12 14:53:55 +00:00
// SessionAll return all activeSession
func (rp *Provider) SessionAll() int {
2013-11-01 16:16:10 +00:00
return 0
}
func init() {
session.Register("redis", redispder)
}