Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
a2dd859e
Commit
a2dd859e
authored
May 14, 2013
by
yecrane
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update sess_redis.go
parent
4d06fec5
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
5 deletions
+40
-5
sess_redis.go
session/sess_redis.go
+40
-5
No files found.
session/sess_redis.go
View file @
a2dd859e
...
...
@@ -6,18 +6,24 @@ import (
var
redispder
=
&
RedisProvider
{}
var
MAX_POOL_SIZE
=
20
var
redisPool
chan
redis
.
Conn
type
RedisSessionStore
struct
{
c
redis
.
Conn
sid
string
}
func
(
rs
*
RedisSessionStore
)
Set
(
key
,
value
interface
{})
error
{
//_, err := rs.c.Do("HSET", rs.sid, key, value)
_
,
err
:=
rs
.
c
.
Do
(
"HSET"
,
rs
.
sid
,
key
,
value
)
return
err
}
func
(
rs
*
RedisSessionStore
)
Get
(
key
interface
{})
interface
{}
{
v
,
err
:=
rs
.
c
.
Do
(
"GET"
,
rs
.
sid
,
key
)
//v, err := rs.c.Do("GET", rs.sid, key)
v
,
err
:=
redis
.
String
(
rs
.
c
.
Do
(
"HGET"
,
rs
.
sid
,
key
))
if
err
!=
nil
{
return
nil
}
...
...
@@ -25,6 +31,7 @@ func (rs *RedisSessionStore) Get(key interface{}) interface{} {
}
func
(
rs
*
RedisSessionStore
)
Delete
(
key
interface
{})
error
{
//_, err := rs.c.Do("HDEL", rs.sid, key)
_
,
err
:=
rs
.
c
.
Do
(
"HDEL"
,
rs
.
sid
,
key
)
return
err
}
...
...
@@ -43,11 +50,37 @@ type RedisProvider struct {
}
func
(
rp
*
RedisProvider
)
connectInit
()
redis
.
Conn
{
c
,
err
:=
redis
.
Dial
(
"tcp"
,
rp
.
savePath
)
/*
c, err := redis.Dial("tcp", rp.savePath)
if err != nil {
return nil
}
return
c
return c*/
if
redisPool
==
nil
{
redisPool
=
make
(
chan
redis
.
Conn
,
MAX_POOL_SIZE
)
}
if
len
(
redisPool
)
==
0
{
go
func
()
{
for
i
:=
0
;
i
<
MAX_POOL_SIZE
/
2
;
i
++
{
c
,
err
:=
redis
.
Dial
(
"tcp"
,
rp
.
savePath
)
if
err
!=
nil
{
panic
(
err
)
}
putRedis
(
c
)
}
}()
}
return
<-
redisPool
}
func
putRedis
(
conn
redis
.
Conn
)
{
if
redisPool
==
nil
{
redisPool
=
make
(
chan
redis
.
Conn
,
MAX_POOL_SIZE
)
}
if
len
(
redisPool
)
==
MAX_POOL_SIZE
{
conn
.
Close
()
return
}
redisPool
<-
conn
}
func
(
rp
*
RedisProvider
)
SessionInit
(
maxlifetime
int64
,
savePath
string
)
error
{
...
...
@@ -58,8 +91,10 @@ func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
func
(
rp
*
RedisProvider
)
SessionRead
(
sid
string
)
(
SessionStore
,
error
)
{
c
:=
rp
.
connectInit
()
if
str
,
err
:=
redis
.
String
(
c
.
Do
(
"GET"
,
sid
));
err
!=
nil
||
str
==
""
{
c
.
Do
(
"SET"
,
sid
,
sid
,
rp
.
maxlifetime
)
//if str, err := redis.String(c.Do("GET", sid)); err != nil || str == "" {
if
str
,
err
:=
redis
.
String
(
c
.
Do
(
"HGET"
,
sid
,
sid
));
err
!=
nil
||
str
==
""
{
//c.Do("SET", sid, sid, rp.maxlifetime)
c
.
Do
(
"HSET"
,
sid
,
sid
,
rp
.
maxlifetime
)
}
rs
:=
&
RedisSessionStore
{
c
:
c
,
sid
:
sid
}
return
rs
,
nil
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment