Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
f988f035
authored
2015-02-16 21:56:32 +0800
by
astaxie
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
redis provider for session and cache support select db
1 parent
1b4158c1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
4 deletions
cache/redis/redis.go
session/redis/sess_redis.go
cache/redis/redis.go
View file @
f988f03
...
...
@@ -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 @
f988f03
...
...
@@ -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
Styling with
Markdown
is supported
Attach a file
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 post a comment