mirror of
https://github.com/astaxie/beego.git
synced 2024-11-21 22:20:55 +00:00
Merge pull request #4137 from phiphi282/change_redis_provider
Add additional options to redis session prov
This commit is contained in:
commit
d6a2621b3c
2
go.mod
2
go.mod
@ -12,8 +12,8 @@ require (
|
|||||||
github.com/couchbase/goutils v0.0.0-20180530154633-e865a1461c8a // indirect
|
github.com/couchbase/goutils v0.0.0-20180530154633-e865a1461c8a // indirect
|
||||||
github.com/elastic/go-elasticsearch/v6 v6.8.5
|
github.com/elastic/go-elasticsearch/v6 v6.8.5
|
||||||
github.com/elazarl/go-bindata-assetfs v1.0.0
|
github.com/elazarl/go-bindata-assetfs v1.0.0
|
||||||
github.com/go-kit/kit v0.9.0
|
|
||||||
github.com/go-redis/redis v6.14.2+incompatible
|
github.com/go-redis/redis v6.14.2+incompatible
|
||||||
|
github.com/go-redis/redis/v7 v7.4.0
|
||||||
github.com/go-sql-driver/mysql v1.5.0
|
github.com/go-sql-driver/mysql v1.5.0
|
||||||
github.com/gogo/protobuf v1.1.1
|
github.com/gogo/protobuf v1.1.1
|
||||||
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
|
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
|
||||||
|
11
go.sum
11
go.sum
@ -59,6 +59,8 @@ github.com/go-logfmt/logfmt v0.4.0 h1:MP4Eh7ZCb31lleYCFuwm0oe4/YGak+5l1vA2NOE80n
|
|||||||
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
|
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
|
||||||
github.com/go-redis/redis v6.14.2+incompatible h1:UE9pLhzmWf+xHNmZsoccjXosPicuiNaInPgym8nzfg0=
|
github.com/go-redis/redis v6.14.2+incompatible h1:UE9pLhzmWf+xHNmZsoccjXosPicuiNaInPgym8nzfg0=
|
||||||
github.com/go-redis/redis v6.14.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
|
github.com/go-redis/redis v6.14.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
|
||||||
|
github.com/go-redis/redis/v7 v7.4.0 h1:7obg6wUoj05T0EpY0o8B59S9w5yeMWql7sw2kwNW1x4=
|
||||||
|
github.com/go-redis/redis/v7 v7.4.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg=
|
||||||
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
|
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
|
||||||
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||||
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
|
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
|
||||||
@ -121,8 +123,10 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
|
|||||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
|
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
|
||||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||||
|
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||||
github.com/onsi/ginkgo v1.12.0 h1:Iw5WCbBcaAAd0fpRb1c9r5YCylv4XDoCSigm1zLevwU=
|
github.com/onsi/ginkgo v1.12.0 h1:Iw5WCbBcaAAd0fpRb1c9r5YCylv4XDoCSigm1zLevwU=
|
||||||
github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0HfGg=
|
github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0HfGg=
|
||||||
|
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
||||||
github.com/onsi/gomega v1.7.1 h1:K0jcRCwNQM3vFGh1ppMtDh/+7ApJrjldlX8fA0jDTLQ=
|
github.com/onsi/gomega v1.7.1 h1:K0jcRCwNQM3vFGh1ppMtDh/+7ApJrjldlX8fA0jDTLQ=
|
||||||
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
|
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
|
||||||
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
|
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
|
||||||
@ -195,8 +199,9 @@ golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73r
|
|||||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
|
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g=
|
||||||
|
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
@ -210,12 +215,16 @@ golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5h
|
|||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 h1:ogLJMz+qpzav7lGMh10LMvAkM/fAoGlaiiHYiFYdm80=
|
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 h1:ogLJMz+qpzav7lGMh10LMvAkM/fAoGlaiiHYiFYdm80=
|
||||||
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
|
||||||
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||||
|
@ -41,7 +41,7 @@ import (
|
|||||||
|
|
||||||
"github.com/astaxie/beego/pkg/session"
|
"github.com/astaxie/beego/pkg/session"
|
||||||
|
|
||||||
"github.com/gomodule/redigo/redis"
|
"github.com/go-redis/redis/v7"
|
||||||
)
|
)
|
||||||
|
|
||||||
var redispder = &Provider{}
|
var redispder = &Provider{}
|
||||||
@ -51,7 +51,7 @@ var MaxPoolSize = 100
|
|||||||
|
|
||||||
// SessionStore redis session store
|
// SessionStore redis session store
|
||||||
type SessionStore struct {
|
type SessionStore struct {
|
||||||
p *redis.Pool
|
p *redis.Client
|
||||||
sid string
|
sid string
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
values map[interface{}]interface{}
|
values map[interface{}]interface{}
|
||||||
@ -103,19 +103,21 @@ func (rs *SessionStore) SessionRelease(w http.ResponseWriter) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c := rs.p.Get()
|
c := rs.p
|
||||||
defer c.Close()
|
c.Set(rs.sid, string(b), time.Duration(rs.maxlifetime)*time.Second)
|
||||||
c.Do("SETEX", rs.sid, rs.maxlifetime, string(b))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provider redis session provider
|
// Provider redis session provider
|
||||||
type Provider struct {
|
type Provider struct {
|
||||||
maxlifetime int64
|
maxlifetime int64
|
||||||
savePath string
|
savePath string
|
||||||
poolsize int
|
poolsize int
|
||||||
password string
|
password string
|
||||||
dbNum int
|
dbNum int
|
||||||
poollist *redis.Pool
|
idleTimeout time.Duration
|
||||||
|
idleCheckFrequency time.Duration
|
||||||
|
maxRetries int
|
||||||
|
poollist *redis.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
// SessionInit init redis session
|
// SessionInit init redis session
|
||||||
@ -150,52 +152,44 @@ func (rp *Provider) SessionInit(maxlifetime int64, savePath string) error {
|
|||||||
} else {
|
} else {
|
||||||
rp.dbNum = 0
|
rp.dbNum = 0
|
||||||
}
|
}
|
||||||
var idleTimeout time.Duration = 0
|
|
||||||
if len(configs) > 4 {
|
if len(configs) > 4 {
|
||||||
timeout, err := strconv.Atoi(configs[4])
|
timeout, err := strconv.Atoi(configs[4])
|
||||||
if err == nil && timeout > 0 {
|
if err == nil && timeout > 0 {
|
||||||
idleTimeout = time.Duration(timeout) * time.Second
|
rp.idleTimeout = time.Duration(timeout) * time.Second
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rp.poollist = &redis.Pool{
|
if len(configs) > 5 {
|
||||||
Dial: func() (redis.Conn, error) {
|
checkFrequency, err := strconv.Atoi(configs[5])
|
||||||
c, err := redis.Dial("tcp", rp.savePath)
|
if err == nil && checkFrequency > 0 {
|
||||||
if err != nil {
|
rp.idleCheckFrequency = time.Duration(checkFrequency) * time.Second
|
||||||
return nil, err
|
}
|
||||||
}
|
}
|
||||||
if rp.password != "" {
|
if len(configs) > 6 {
|
||||||
if _, err = c.Do("AUTH", rp.password); err != nil {
|
retries, err := strconv.Atoi(configs[6])
|
||||||
c.Close()
|
if err == nil && retries > 0 {
|
||||||
return nil, err
|
rp.maxRetries = retries
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// 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
|
|
||||||
},
|
|
||||||
MaxIdle: rp.poolsize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rp.poollist.IdleTimeout = idleTimeout
|
rp.poollist = redis.NewClient(&redis.Options{
|
||||||
|
Addr: rp.savePath,
|
||||||
|
Password: rp.password,
|
||||||
|
PoolSize: rp.poolsize,
|
||||||
|
DB: rp.dbNum,
|
||||||
|
IdleTimeout: rp.idleTimeout,
|
||||||
|
IdleCheckFrequency: rp.idleCheckFrequency,
|
||||||
|
MaxRetries: rp.maxRetries,
|
||||||
|
})
|
||||||
|
|
||||||
return rp.poollist.Get().Err()
|
return rp.poollist.Ping().Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
// SessionRead read redis session by sid
|
// SessionRead read redis session by sid
|
||||||
func (rp *Provider) SessionRead(sid string) (session.Store, error) {
|
func (rp *Provider) SessionRead(sid string) (session.Store, error) {
|
||||||
c := rp.poollist.Get()
|
|
||||||
defer c.Close()
|
|
||||||
|
|
||||||
var kv map[interface{}]interface{}
|
var kv map[interface{}]interface{}
|
||||||
|
|
||||||
kvs, err := redis.String(c.Do("GET", sid))
|
kvs, err := rp.poollist.Get(sid).Result()
|
||||||
if err != nil && err != redis.ErrNil {
|
if err != nil && err != redis.Nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(kvs) == 0 {
|
if len(kvs) == 0 {
|
||||||
@ -212,10 +206,9 @@ func (rp *Provider) SessionRead(sid string) (session.Store, error) {
|
|||||||
|
|
||||||
// SessionExist check redis session exist by sid
|
// SessionExist check redis session exist by sid
|
||||||
func (rp *Provider) SessionExist(sid string) (bool, error) {
|
func (rp *Provider) SessionExist(sid string) (bool, error) {
|
||||||
c := rp.poollist.Get()
|
c := rp.poollist
|
||||||
defer c.Close()
|
|
||||||
|
|
||||||
if existed, err := redis.Int(c.Do("EXISTS", sid)); err != nil || existed == 0 {
|
if existed, err := c.Exists(sid).Result(); err != nil || existed == 0 {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
return true, nil
|
return true, nil
|
||||||
@ -223,27 +216,24 @@ func (rp *Provider) SessionExist(sid string) (bool, error) {
|
|||||||
|
|
||||||
// SessionRegenerate generate new sid for redis session
|
// SessionRegenerate generate new sid for redis session
|
||||||
func (rp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
|
func (rp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
|
||||||
c := rp.poollist.Get()
|
c := rp.poollist
|
||||||
defer c.Close()
|
if existed, _ := c.Exists(oldsid).Result(); existed == 0 {
|
||||||
|
|
||||||
if existed, _ := redis.Int(c.Do("EXISTS", oldsid)); existed == 0 {
|
|
||||||
// oldsid doesn't exists, set the new sid directly
|
// oldsid doesn't exists, set the new sid directly
|
||||||
// ignore error here, since if it return error
|
// ignore error here, since if it return error
|
||||||
// the existed value will be 0
|
// the existed value will be 0
|
||||||
c.Do("SET", sid, "", "EX", rp.maxlifetime)
|
c.Do(c.Context(), "SET", sid, "", "EX", rp.maxlifetime)
|
||||||
} else {
|
} else {
|
||||||
c.Do("RENAME", oldsid, sid)
|
c.Rename(oldsid, sid)
|
||||||
c.Do("EXPIRE", sid, rp.maxlifetime)
|
c.Expire(sid, time.Duration(rp.maxlifetime) * time.Second)
|
||||||
}
|
}
|
||||||
return rp.SessionRead(sid)
|
return rp.SessionRead(sid)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SessionDestroy delete redis session by id
|
// SessionDestroy delete redis session by id
|
||||||
func (rp *Provider) SessionDestroy(sid string) error {
|
func (rp *Provider) SessionDestroy(sid string) error {
|
||||||
c := rp.poollist.Get()
|
c := rp.poollist
|
||||||
defer c.Close()
|
|
||||||
|
|
||||||
c.Do("DEL", sid)
|
c.Del(sid)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
88
pkg/session/redis/sess_redis_test.go
Normal file
88
pkg/session/redis/sess_redis_test.go
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
package redis
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/astaxie/beego/pkg/session"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRedis(t *testing.T) {
|
||||||
|
sessionConfig := &session.ManagerConfig{
|
||||||
|
CookieName: "gosessionid",
|
||||||
|
EnableSetCookie: true,
|
||||||
|
Gclifetime: 3600,
|
||||||
|
Maxlifetime: 3600,
|
||||||
|
Secure: false,
|
||||||
|
CookieLifeTime: 3600,
|
||||||
|
ProviderConfig: "127.0.0.1:6379,100,,0,30",
|
||||||
|
}
|
||||||
|
globalSession, err := session.NewManager("redis", sessionConfig)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("could not create manager:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
go globalSession.GC()
|
||||||
|
|
||||||
|
r, _ := http.NewRequest("GET", "/", nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
sess, err := globalSession.SessionStart(w, r)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("session start failed:", err)
|
||||||
|
}
|
||||||
|
defer sess.SessionRelease(w)
|
||||||
|
|
||||||
|
// SET AND GET
|
||||||
|
err = sess.Set("username", "astaxie")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("set username failed:", err)
|
||||||
|
}
|
||||||
|
username := sess.Get("username")
|
||||||
|
if username != "astaxie" {
|
||||||
|
t.Fatal("get username failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// DELETE
|
||||||
|
err = sess.Delete("username")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("delete username failed:", err)
|
||||||
|
}
|
||||||
|
username = sess.Get("username")
|
||||||
|
if username != nil {
|
||||||
|
t.Fatal("delete username failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
// FLUSH
|
||||||
|
err = sess.Set("username", "astaxie")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("set failed:", err)
|
||||||
|
}
|
||||||
|
err = sess.Set("password", "1qaz2wsx")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("set failed:", err)
|
||||||
|
}
|
||||||
|
username = sess.Get("username")
|
||||||
|
if username != "astaxie" {
|
||||||
|
t.Fatal("get username failed")
|
||||||
|
}
|
||||||
|
password := sess.Get("password")
|
||||||
|
if password != "1qaz2wsx" {
|
||||||
|
t.Fatal("get password failed")
|
||||||
|
}
|
||||||
|
err = sess.Flush()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("flush failed:", err)
|
||||||
|
}
|
||||||
|
username = sess.Get("username")
|
||||||
|
if username != nil {
|
||||||
|
t.Fatal("flush failed")
|
||||||
|
}
|
||||||
|
password = sess.Get("password")
|
||||||
|
if password != nil {
|
||||||
|
t.Fatal("flush failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
sess.SessionRelease(w)
|
||||||
|
}
|
@ -34,7 +34,7 @@ package redis_cluster
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/astaxie/beego/pkg/session"
|
"github.com/astaxie/beego/pkg/session"
|
||||||
rediss "github.com/go-redis/redis"
|
rediss "github.com/go-redis/redis/v7"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -107,12 +107,15 @@ func (rs *SessionStore) SessionRelease(w http.ResponseWriter) {
|
|||||||
|
|
||||||
// Provider redis_cluster session provider
|
// Provider redis_cluster session provider
|
||||||
type Provider struct {
|
type Provider struct {
|
||||||
maxlifetime int64
|
maxlifetime int64
|
||||||
savePath string
|
savePath string
|
||||||
poolsize int
|
poolsize int
|
||||||
password string
|
password string
|
||||||
dbNum int
|
dbNum int
|
||||||
poollist *rediss.ClusterClient
|
idleTimeout time.Duration
|
||||||
|
idleCheckFrequency time.Duration
|
||||||
|
maxRetries int
|
||||||
|
poollist *rediss.ClusterClient
|
||||||
}
|
}
|
||||||
|
|
||||||
// SessionInit init redis_cluster session
|
// SessionInit init redis_cluster session
|
||||||
@ -147,11 +150,32 @@ func (rp *Provider) SessionInit(maxlifetime int64, savePath string) error {
|
|||||||
} else {
|
} else {
|
||||||
rp.dbNum = 0
|
rp.dbNum = 0
|
||||||
}
|
}
|
||||||
|
if len(configs) > 4 {
|
||||||
|
timeout, err := strconv.Atoi(configs[4])
|
||||||
|
if err == nil && timeout > 0 {
|
||||||
|
rp.idleTimeout = time.Duration(timeout) * time.Second
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(configs) > 5 {
|
||||||
|
checkFrequency, err := strconv.Atoi(configs[5])
|
||||||
|
if err == nil && checkFrequency > 0 {
|
||||||
|
rp.idleCheckFrequency = time.Duration(checkFrequency) * time.Second
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(configs) > 6 {
|
||||||
|
retries, err := strconv.Atoi(configs[6])
|
||||||
|
if err == nil && retries > 0 {
|
||||||
|
rp.maxRetries = retries
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
rp.poollist = rediss.NewClusterClient(&rediss.ClusterOptions{
|
rp.poollist = rediss.NewClusterClient(&rediss.ClusterOptions{
|
||||||
Addrs: strings.Split(rp.savePath, ";"),
|
Addrs: strings.Split(rp.savePath, ";"),
|
||||||
Password: rp.password,
|
Password: rp.password,
|
||||||
PoolSize: rp.poolsize,
|
PoolSize: rp.poolsize,
|
||||||
|
IdleTimeout: rp.idleTimeout,
|
||||||
|
IdleCheckFrequency: rp.idleCheckFrequency,
|
||||||
|
MaxRetries: rp.maxRetries,
|
||||||
})
|
})
|
||||||
return rp.poollist.Ping().Err()
|
return rp.poollist.Ping().Err()
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ package redis_sentinel
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/astaxie/beego/pkg/session"
|
"github.com/astaxie/beego/pkg/session"
|
||||||
"github.com/go-redis/redis"
|
"github.com/go-redis/redis/v7"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -107,13 +107,16 @@ func (rs *SessionStore) SessionRelease(w http.ResponseWriter) {
|
|||||||
|
|
||||||
// Provider redis_sentinel session provider
|
// Provider redis_sentinel session provider
|
||||||
type Provider struct {
|
type Provider struct {
|
||||||
maxlifetime int64
|
maxlifetime int64
|
||||||
savePath string
|
savePath string
|
||||||
poolsize int
|
poolsize int
|
||||||
password string
|
password string
|
||||||
dbNum int
|
dbNum int
|
||||||
poollist *redis.Client
|
idleTimeout time.Duration
|
||||||
masterName string
|
idleCheckFrequency time.Duration
|
||||||
|
maxRetries int
|
||||||
|
poollist *redis.Client
|
||||||
|
masterName string
|
||||||
}
|
}
|
||||||
|
|
||||||
// SessionInit init redis_sentinel session
|
// SessionInit init redis_sentinel session
|
||||||
@ -157,13 +160,34 @@ func (rp *Provider) SessionInit(maxlifetime int64, savePath string) error {
|
|||||||
} else {
|
} else {
|
||||||
rp.masterName = "mymaster"
|
rp.masterName = "mymaster"
|
||||||
}
|
}
|
||||||
|
if len(configs) > 5 {
|
||||||
|
timeout, err := strconv.Atoi(configs[4])
|
||||||
|
if err == nil && timeout > 0 {
|
||||||
|
rp.idleTimeout = time.Duration(timeout) * time.Second
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(configs) > 6 {
|
||||||
|
checkFrequency, err := strconv.Atoi(configs[5])
|
||||||
|
if err == nil && checkFrequency > 0 {
|
||||||
|
rp.idleCheckFrequency = time.Duration(checkFrequency) * time.Second
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(configs) > 7 {
|
||||||
|
retries, err := strconv.Atoi(configs[6])
|
||||||
|
if err == nil && retries > 0 {
|
||||||
|
rp.maxRetries = retries
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
rp.poollist = redis.NewFailoverClient(&redis.FailoverOptions{
|
rp.poollist = redis.NewFailoverClient(&redis.FailoverOptions{
|
||||||
SentinelAddrs: strings.Split(rp.savePath, ";"),
|
SentinelAddrs: strings.Split(rp.savePath, ";"),
|
||||||
Password: rp.password,
|
Password: rp.password,
|
||||||
PoolSize: rp.poolsize,
|
PoolSize: rp.poolsize,
|
||||||
DB: rp.dbNum,
|
DB: rp.dbNum,
|
||||||
MasterName: rp.masterName,
|
MasterName: rp.masterName,
|
||||||
|
IdleTimeout: rp.idleTimeout,
|
||||||
|
IdleCheckFrequency: rp.idleCheckFrequency,
|
||||||
|
MaxRetries: rp.maxRetries,
|
||||||
})
|
})
|
||||||
|
|
||||||
return rp.poollist.Ping().Err()
|
return rp.poollist.Ping().Err()
|
||||||
|
Loading…
Reference in New Issue
Block a user