File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ package session
2+
3+ import (
4+ "encoding/json"
5+ "github.com/gomodule/redigo/redis"
6+ "time"
7+ )
8+
9+ type RedisHandler struct {
10+ pool * redis.Pool // redis connection pool
11+ prefix string
12+ lifetime time.Duration
13+ }
14+
15+ // NewRedisHandler Create a redis session handler
16+ func NewRedisHandler (pool * redis.Pool , prefix string , lifetime time.Duration ) * RedisHandler {
17+ return & RedisHandler {pool , prefix , lifetime }
18+ }
19+
20+ func (rh * RedisHandler ) Read (id string ) string {
21+ c := rh .pool .Get ()
22+ defer c .Close ()
23+
24+ b , err := redis .Bytes (c .Do ("GET" , rh .prefix + ":" + id ))
25+ if err != nil {
26+ return ""
27+ }
28+
29+ var value string
30+
31+ json .Unmarshal (b , value )
32+
33+ return value
34+ }
35+
36+ func (rh * RedisHandler ) Write (id string , data string ) {
37+ c := rh .pool .Get ()
38+ defer c .Close ()
39+
40+ c .Do ("SETEX" , rh .prefix + ":" + id , int64 (rh .lifetime ), data )
41+ }
You can’t perform that action at this time.
0 commit comments