Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
2773fda8
B
beego
项目概览
二次元的罪恶王冠
/
beego
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
beego
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2773fda8
编写于
8月 08, 2014
作者:
A
astaxie
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
session:change the driver from beego to bradfitz
上级
99c03a2b
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
48 addition
and
46 deletion
+48
-46
session/memcache/sess_memcache.go
session/memcache/sess_memcache.go
+48
-46
未找到文件。
session/memcache/sess_memcache.go
浏览文件 @
2773fda8
...
...
@@ -11,18 +11,19 @@ package session
import
(
"net/http"
"strings"
"sync"
"github.com/astaxie/beego/session"
"github.com/b
eego
/memcache"
"github.com/b
radfitz/gomemcache
/memcache"
)
var
mempder
=
&
MemProvider
{}
var
client
*
memcache
.
Client
// memcache session store
type
MemcacheSessionStore
struct
{
c
*
memcache
.
Connection
sid
string
lock
sync
.
RWMutex
values
map
[
interface
{}]
interface
{}
...
...
@@ -71,19 +72,18 @@ func (rs *MemcacheSessionStore) SessionID() string {
// save session values to redis
func
(
rs
*
MemcacheSessionStore
)
SessionRelease
(
w
http
.
ResponseWriter
)
{
defer
rs
.
c
.
Close
()
b
,
err
:=
session
.
EncodeGob
(
rs
.
values
)
if
err
!=
nil
{
return
}
rs
.
c
.
Set
(
rs
.
sid
,
0
,
uint64
(
rs
.
maxlifetime
),
b
)
item
:=
memcache
.
Item
{
Key
:
rs
.
sid
,
Value
:
b
,
Expiration
:
int32
(
rs
.
maxlifetime
)}
client
.
Set
(
&
item
)
}
// redis session provider
type
MemProvider
struct
{
maxlifetime
int64
savePath
string
conninfo
[]
string
poolsize
int
password
string
}
...
...
@@ -93,46 +93,44 @@ type MemProvider struct {
// e.g. 127.0.0.1:9090
func
(
rp
*
MemProvider
)
SessionInit
(
maxlifetime
int64
,
savePath
string
)
error
{
rp
.
maxlifetime
=
maxlifetime
rp
.
savePath
=
savePath
rp
.
conninfo
=
strings
.
Split
(
savePath
,
";"
)
client
=
memcache
.
New
(
rp
.
conninfo
...
)
return
nil
}
// read redis session by sid
func
(
rp
*
MemProvider
)
SessionRead
(
sid
string
)
(
session
.
SessionStore
,
error
)
{
conn
,
err
:=
rp
.
connectInit
()
if
err
!=
nil
{
return
nil
,
err
if
client
==
nil
{
if
err
:=
rp
.
connectInit
();
err
!=
nil
{
return
nil
,
err
}
}
kvs
,
err
:=
conn
.
Get
(
sid
)
item
,
err
:=
client
.
Get
(
sid
)
if
err
!=
nil
{
return
nil
,
err
}
var
contain
[]
byte
if
len
(
kvs
)
>
0
{
contain
=
kvs
[
0
]
.
Value
}
var
kv
map
[
interface
{}]
interface
{}
if
len
(
contain
)
==
0
{
if
len
(
item
.
Value
)
==
0
{
kv
=
make
(
map
[
interface
{}]
interface
{})
}
else
{
kv
,
err
=
session
.
DecodeGob
(
contain
)
kv
,
err
=
session
.
DecodeGob
(
item
.
Value
)
if
err
!=
nil
{
return
nil
,
err
}
}
rs
:=
&
MemcacheSessionStore
{
c
:
conn
,
sid
:
sid
,
values
:
kv
,
maxlifetime
:
rp
.
maxlifetime
}
rs
:=
&
MemcacheSessionStore
{
sid
:
sid
,
values
:
kv
,
maxlifetime
:
rp
.
maxlifetime
}
return
rs
,
nil
}
// check redis session exist by sid
func
(
rp
*
MemProvider
)
SessionExist
(
sid
string
)
bool
{
conn
,
err
:=
rp
.
connectInit
()
if
err
!=
nil
{
return
false
if
client
==
nil
{
if
err
:=
rp
.
connectInit
();
err
!=
nil
{
return
false
}
}
defer
conn
.
Close
()
if
kvs
,
err
:=
conn
.
Get
(
sid
);
err
!=
nil
||
len
(
kvs
)
==
0
{
if
item
,
err
:=
client
.
Get
(
sid
);
err
!=
nil
||
len
(
item
.
Value
)
==
0
{
return
false
}
else
{
return
true
...
...
@@ -141,51 +139,64 @@ func (rp *MemProvider) SessionExist(sid string) bool {
// generate new sid for redis session
func
(
rp
*
MemProvider
)
SessionRegenerate
(
oldsid
,
sid
string
)
(
session
.
SessionStore
,
error
)
{
conn
,
err
:=
rp
.
connectInit
()
if
err
!=
nil
{
return
nil
,
err
if
client
==
nil
{
if
err
:=
rp
.
connectInit
();
err
!=
nil
{
return
nil
,
err
}
}
var
contain
[]
byte
if
kvs
,
err
:=
conn
.
Get
(
sid
);
err
!=
nil
||
len
(
kvs
)
==
0
{
if
item
,
err
:=
client
.
Get
(
sid
);
err
!=
nil
||
len
(
item
.
Value
)
==
0
{
// oldsid doesn't exists, set the new sid directly
// ignore error here, since if it return error
// the existed value will be 0
conn
.
Set
(
sid
,
0
,
uint64
(
rp
.
maxlifetime
),
[]
byte
(
""
))
item
.
Key
=
sid
item
.
Value
=
[]
byte
(
""
)
item
.
Expiration
=
int32
(
rp
.
maxlifetime
)
client
.
Set
(
item
)
}
else
{
conn
.
Delete
(
oldsid
)
conn
.
Set
(
sid
,
0
,
uint64
(
rp
.
maxlifetime
),
kvs
[
0
]
.
Value
)
contain
=
kvs
[
0
]
.
Value
client
.
Delete
(
oldsid
)
item
.
Key
=
sid
item
.
Value
=
item
.
Value
item
.
Expiration
=
int32
(
rp
.
maxlifetime
)
client
.
Set
(
item
)
contain
=
item
.
Value
}
var
kv
map
[
interface
{}]
interface
{}
if
len
(
contain
)
==
0
{
kv
=
make
(
map
[
interface
{}]
interface
{})
}
else
{
var
err
error
kv
,
err
=
session
.
DecodeGob
(
contain
)
if
err
!=
nil
{
return
nil
,
err
}
}
rs
:=
&
MemcacheSessionStore
{
c
:
conn
,
sid
:
sid
,
values
:
kv
,
maxlifetime
:
rp
.
maxlifetime
}
rs
:=
&
MemcacheSessionStore
{
sid
:
sid
,
values
:
kv
,
maxlifetime
:
rp
.
maxlifetime
}
return
rs
,
nil
}
// delete redis session by id
func
(
rp
*
MemProvider
)
SessionDestroy
(
sid
string
)
error
{
conn
,
err
:=
rp
.
connectInit
()
if
err
!=
nil
{
return
err
if
client
==
nil
{
if
err
:=
rp
.
connectInit
();
err
!=
nil
{
return
err
}
}
defer
conn
.
Close
()
_
,
err
=
conn
.
Delete
(
sid
)
err
:=
client
.
Delete
(
sid
)
if
err
!=
nil
{
return
err
}
return
nil
}
func
(
rp
*
MemProvider
)
connectInit
()
error
{
client
=
memcache
.
New
(
rp
.
conninfo
...
)
return
nil
}
// Impelment method, no used.
func
(
rp
*
MemProvider
)
SessionGC
()
{
return
...
...
@@ -196,15 +207,6 @@ func (rp *MemProvider) SessionAll() int {
return
0
}
// connect to memcache and keep the connection.
func
(
rp
*
MemProvider
)
connectInit
()
(
*
memcache
.
Connection
,
error
)
{
c
,
err
:=
memcache
.
Connect
(
rp
.
savePath
)
if
err
!=
nil
{
return
nil
,
err
}
return
c
,
nil
}
func
init
()
{
session
.
Register
(
"memcache"
,
mempder
)
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录