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
060631e9
authored
2013-10-28 22:19:37 +0800
by
astaxie
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
fix #241#192
1 parent
57165f2f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
39 deletions
session/README.md
session/sess_redis.go
session/README.md
View file @
060631e
...
...
@@ -39,10 +39,10 @@ Then in you web app init the global session manager
go globalSessions.GC()
}
*
Use
**Redis**
as provider, the last param is the Redis conn address:
*
Use
**Redis**
as provider, the last param is the Redis conn address
,poolsize,password
:
func init() {
globalSessions, _ = session.NewManager("redis", "gosessionid", 3600, "127.0.0.1:6379")
globalSessions, _ = session.NewManager("redis", "gosessionid", 3600, "127.0.0.1:6379
,100,astaxie
")
go globalSessions.GC()
}
...
...
session/sess_redis.go
View file @
060631e
package
session
import
(
"fmt"
"github.com/garyburd/redigo/redis"
"strconv"
"strings"
)
var
redispder
=
&
RedisProvider
{}
var
MAX_POOL_SIZE
=
2
0
var
MAX_POOL_SIZE
=
10
0
var
redisPool
chan
redis
.
Conn
...
...
@@ -52,72 +53,73 @@ func (rs *RedisSessionStore) SessionRelease() {
type
RedisProvider
struct
{
maxlifetime
int64
savePath
string
poolsize
int
password
string
poollist
*
redis
.
Pool
}
func
(
rp
*
RedisProvider
)
connectInit
()
redis
.
Conn
{
/*c, err := redis.Dial("tcp", rp.savePath)
if err != nil {
return nil
//savepath like redisserveraddr,poolsize,password
//127.0.0.1:6379,100,astaxie
func
(
rp
*
RedisProvider
)
SessionInit
(
maxlifetime
int64
,
savePath
string
)
error
{
rp
.
maxlifetime
=
maxlifetime
configs
:=
strings
.
Split
(
savePath
,
","
)
if
len
(
configs
)
>
0
{
rp
.
savePath
=
configs
[
0
]
}
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
{
fmt
.
Println
(
err
)
return
if
len
(
configs
)
>
1
{
poolsize
,
err
:=
strconv
.
Atoi
(
configs
[
1
])
if
err
!=
nil
||
poolsize
<=
0
{
rp
.
poolsize
=
MAX_POOL_SIZE
}
else
{
rp
.
poolsize
=
poolsize
}
putRedis
(
c
)
}
else
{
rp
.
poolsize
=
MAX_POOL_SIZE
}
}()
if
len
(
configs
)
>
2
{
rp
.
password
=
configs
[
2
]
}
return
<-
redisPool
}
func
putRedis
(
conn
redis
.
Conn
)
{
if
redisPool
==
nil
{
redisPool
=
make
(
chan
redis
.
Conn
,
MAX_POOL_SIZE
)
rp
.
poollist
=
redis
.
NewPool
(
func
()
(
redis
.
Conn
,
error
)
{
c
,
err
:=
redis
.
Dial
(
"tcp"
,
rp
.
savePath
)
if
err
!=
nil
{
return
nil
,
err
}
if
len
(
redisPool
)
>=
MAX_POOL_SIZE
{
conn
.
Close
()
return
if
rp
.
password
!=
""
{
if
_
,
err
:=
c
.
Do
(
"AUTH"
,
rp
.
password
);
err
!=
nil
{
c
.
Close
()
return
nil
,
err
}
redisPool
<-
conn
}
func
(
rp
*
RedisProvider
)
SessionInit
(
maxlifetime
int64
,
savePath
string
)
error
{
rp
.
maxlifetime
=
maxlifetime
rp
.
savePath
=
savePath
}
return
c
,
err
},
rp
.
poolsize
)
return
nil
}
func
(
rp
*
RedisProvider
)
SessionRead
(
sid
string
)
(
SessionStore
,
error
)
{
c
:=
rp
.
connectIni
t
()
c
:=
rp
.
poollist
.
Ge
t
()
//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
)
}
c
.
Do
(
"EXPIRE"
,
sid
,
rp
.
maxlifetime
)
rs
:=
&
RedisSessionStore
{
c
:
c
,
sid
:
sid
}
return
rs
,
nil
}
func
(
rp
*
RedisProvider
)
SessionRegenerate
(
oldsid
,
sid
string
)
(
SessionStore
,
error
)
{
c
:=
rp
.
connectIni
t
()
c
:=
rp
.
poollist
.
Ge
t
()
if
str
,
err
:=
redis
.
String
(
c
.
Do
(
"HGET"
,
oldsid
,
oldsid
));
err
!=
nil
||
str
==
""
{
c
.
Do
(
"HSET"
,
oldsid
,
oldsid
,
rp
.
maxlifetime
)
}
c
.
Do
(
"RENAME"
,
oldsid
,
sid
)
c
.
Do
(
"EXPIRE"
,
sid
,
rp
.
maxlifetime
)
rs
:=
&
RedisSessionStore
{
c
:
c
,
sid
:
sid
}
return
rs
,
nil
}
func
(
rp
*
RedisProvider
)
SessionDestroy
(
sid
string
)
error
{
c
:=
rp
.
connectIni
t
()
c
:=
rp
.
poollist
.
Ge
t
()
c
.
Do
(
"DEL"
,
sid
)
return
nil
}
...
...
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