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
f988f035
Commit
f988f035
authored
Feb 16, 2015
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
redis provider for session and cache support select db
parent
1b4158c1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
4 deletions
+30
-4
redis.go
cache/redis/redis.go
+12
-2
sess_redis.go
session/redis/sess_redis.go
+18
-2
No files found.
cache/redis/redis.go
View file @
f988f035
...
...
@@ -32,6 +32,7 @@ package redis
import
(
"encoding/json"
"errors"
"strconv"
"time"
"github.com/garyburd/redigo/redis"
...
...
@@ -48,6 +49,7 @@ var (
type
RedisCache
struct
{
p
*
redis
.
Pool
// redis connection pool
conninfo
string
dbNum
int
key
string
}
...
...
@@ -137,7 +139,7 @@ func (rc *RedisCache) ClearAll() error {
}
// start redis cache adapter.
// config is like {"key":"collection key","conn":"connection info"}
// config is like {"key":"collection key","conn":"connection info"
,"dbNum":"0"
}
// the cache item in redis are stored forever,
// so no gc operation.
func
(
rc
*
RedisCache
)
StartAndGC
(
config
string
)
error
{
...
...
@@ -151,9 +153,12 @@ func (rc *RedisCache) StartAndGC(config string) error {
if
_
,
ok
:=
cf
[
"conn"
];
!
ok
{
return
errors
.
New
(
"config has no conn key"
)
}
if
_
,
ok
:=
cf
[
"dbNum"
];
!
ok
{
cf
[
"dbNum"
]
=
"0"
}
rc
.
key
=
cf
[
"key"
]
rc
.
conninfo
=
cf
[
"conn"
]
rc
.
dbNum
,
_
=
strconv
.
Atoi
(
cf
[
"dbNum"
])
rc
.
connectInit
()
c
:=
rc
.
p
.
Get
()
...
...
@@ -166,6 +171,11 @@ func (rc *RedisCache) StartAndGC(config string) error {
func
(
rc
*
RedisCache
)
connectInit
()
{
dialFunc
:=
func
()
(
c
redis
.
Conn
,
err
error
)
{
c
,
err
=
redis
.
Dial
(
"tcp"
,
rc
.
conninfo
)
_
,
selecterr
:=
c
.
Do
(
"SELECT"
,
rc
.
dbNum
)
if
selecterr
!=
nil
{
c
.
Close
()
return
nil
,
selecterr
}
return
}
// initialize a new pool
...
...
session/redis/sess_redis.go
View file @
f988f035
...
...
@@ -118,12 +118,13 @@ type RedisProvider struct {
savePath
string
poolsize
int
password
string
dbNum
int
poollist
*
redis
.
Pool
}
// init redis session
// savepath like redis server addr,pool size,password
// e.g. 127.0.0.1:6379,100,astaxie
// savepath like redis server addr,pool size,password
,dbnum
// e.g. 127.0.0.1:6379,100,astaxie
,0
func
(
rp
*
RedisProvider
)
SessionInit
(
maxlifetime
int64
,
savePath
string
)
error
{
rp
.
maxlifetime
=
maxlifetime
configs
:=
strings
.
Split
(
savePath
,
","
)
...
...
@@ -143,6 +144,16 @@ func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
if
len
(
configs
)
>
2
{
rp
.
password
=
configs
[
2
]
}
if
len
(
configs
)
>
3
{
dbnum
,
err
:=
strconv
.
Atoi
(
configs
[
1
])
if
err
!=
nil
||
dbnum
<
0
{
rp
.
dbNum
=
0
}
else
{
rp
.
dbNum
=
dbnum
}
}
else
{
rp
.
dbNum
=
0
}
rp
.
poollist
=
redis
.
NewPool
(
func
()
(
redis
.
Conn
,
error
)
{
c
,
err
:=
redis
.
Dial
(
"tcp"
,
rp
.
savePath
)
if
err
!=
nil
{
...
...
@@ -154,6 +165,11 @@ func (rp *RedisProvider) SessionInit(maxlifetime int64, savePath string) error {
return
nil
,
err
}
}
_
,
err
=
c
.
Do
(
"SELECT"
,
rp
.
dbNum
)
if
err
!=
nil
{
c
.
Close
()
return
nil
,
err
}
return
c
,
err
},
rp
.
poolsize
)
...
...
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