-
Notifications
You must be signed in to change notification settings - Fork 0
/
sess_redis.go
120 lines (101 loc) · 3.9 KB
/
sess_redis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// 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 redis for session provider
//
// depend on github.com/gomodule/redigo/redis
//
// go install github.com/gomodule/redigo/redis
//
// Usage:
// import(
// _ "github.com/W3-Partha/Beego/server/web/session/redis"
// "github.com/W3-Partha/Beego/server/web/session"
// )
//
// func init() {
// globalSessions, _ = session.NewManager("redis", ``{"cookieName":"gosessionid","gclifetime":3600,"ProviderConfig":"127.0.0.1:7070"}``)
// go globalSessions.GC()
// }
//
// more docs: http://beego.me/docs/module/session.md
package redis
import (
"context"
"net/http"
"github.com/W3-Partha/Beego/adapter/session"
beeRedis "github.com/W3-Partha/Beego/server/web/session/redis"
)
// MaxPoolSize redis max pool size
var MaxPoolSize = beeRedis.MaxPoolSize
// SessionStore redis session store
type SessionStore beeRedis.SessionStore
// Set value in redis session
func (rs *SessionStore) Set(key, value interface{}) error {
return (*beeRedis.SessionStore)(rs).Set(context.Background(), key, value)
}
// Get value in redis session
func (rs *SessionStore) Get(key interface{}) interface{} {
return (*beeRedis.SessionStore)(rs).Get(context.Background(), key)
}
// Delete value in redis session
func (rs *SessionStore) Delete(key interface{}) error {
return (*beeRedis.SessionStore)(rs).Delete(context.Background(), key)
}
// Flush clear all values in redis session
func (rs *SessionStore) Flush() error {
return (*beeRedis.SessionStore)(rs).Flush(context.Background())
}
// SessionID get redis session id
func (rs *SessionStore) SessionID() string {
return (*beeRedis.SessionStore)(rs).SessionID(context.Background())
}
// SessionRelease save session values to redis
func (rs *SessionStore) SessionRelease(w http.ResponseWriter) {
(*beeRedis.SessionStore)(rs).SessionRelease(context.Background(), w)
}
// Provider redis session provider
type Provider beeRedis.Provider
// SessionInit init redis session
// savepath like redis server addr,pool size,password,dbnum,IdleTimeout second
// e.g. 127.0.0.1:6379,100,astaxie,0,30
func (rp *Provider) SessionInit(maxlifetime int64, savePath string) error {
return (*beeRedis.Provider)(rp).SessionInit(context.Background(), maxlifetime, savePath)
}
// SessionRead read redis session by sid
func (rp *Provider) SessionRead(sid string) (session.Store, error) {
s, err := (*beeRedis.Provider)(rp).SessionRead(context.Background(), sid)
return session.CreateNewToOldStoreAdapter(s), err
}
// SessionExist check redis session exist by sid
func (rp *Provider) SessionExist(sid string) bool {
res, _ := (*beeRedis.Provider)(rp).SessionExist(context.Background(), sid)
return res
}
// SessionRegenerate generate new sid for redis session
func (rp *Provider) SessionRegenerate(oldsid, sid string) (session.Store, error) {
s, err := (*beeRedis.Provider)(rp).SessionRegenerate(context.Background(), oldsid, sid)
return session.CreateNewToOldStoreAdapter(s), err
}
// SessionDestroy delete redis session by id
func (rp *Provider) SessionDestroy(sid string) error {
return (*beeRedis.Provider)(rp).SessionDestroy(context.Background(), sid)
}
// SessionGC Impelment method, no used.
func (rp *Provider) SessionGC() {
(*beeRedis.Provider)(rp).SessionGC(context.Background())
}
// SessionAll return all activeSession
func (rp *Provider) SessionAll() int {
return (*beeRedis.Provider)(rp).SessionAll(context.Background())
}