Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
yii2_fecshop
提交
77593d8d
Y
yii2_fecshop
项目概览
OpenDocCN
/
yii2_fecshop
通知
17
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Y
yii2_fecshop
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
77593d8d
编写于
1月 18, 2019
作者:
T
Terry
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
appserver--> redisSession Refactor
上级
9606f675
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
23 addition
and
137 deletion
+23
-137
services/session/SessionMysqldb.php
services/session/SessionMysqldb.php
+7
-73
services/session/SessionRedis.php
services/session/SessionRedis.php
+16
-64
未找到文件。
services/session/SessionMysqldb.php
浏览文件 @
77593d8d
...
...
@@ -22,66 +22,24 @@ use fecshop\services\Service;
*/
class
SessionMysqldb
extends
Service
implements
SessionInterface
{
protected
$_sessionModelName
=
'\fecshop\models\mysqldb\SessionStorage'
;
protected
$_sessionModel
;
public
function
init
()
{
parent
::
init
();
list
(
$this
->
_sessionModelName
,
$this
->
_sessionModel
)
=
\
Yii
::
mapGet
(
$this
->
_sessionModelName
);
}
public
function
set
(
$key
,
$val
,
$timeout
)
{
$uuid
=
Yii
::
$service
->
session
->
getUUID
();
$one
=
$this
->
_sessionModel
->
find
()
->
where
([
'uuid'
=>
$uuid
,
'key'
=>
$key
,
])
->
one
();
if
(
!
$one
[
'id'
])
{
$one
=
new
$this
->
_sessionModelName
();
$one
[
'uuid'
]
=
$uuid
;
$one
[
'key'
]
=
$key
;
}
$one
[
'value'
]
=
$val
;
$one
[
'timeout'
]
=
$timeout
;
$one
[
'updated_at'
]
=
time
();
$one
->
save
();
return
true
;
}
public
function
get
(
$key
,
$reflush
)
{
$uuid
=
Yii
::
$service
->
session
->
getUUID
();
$one
=
$this
->
_sessionModel
->
find
()
->
where
([
'uuid'
=>
$uuid
,
'key'
=>
$key
,
])
->
one
();
if
(
$one
[
'id'
])
{
$timeout
=
$one
[
'timeout'
];
$updated_at
=
$one
[
'updated_at'
];
if
(
$updated_at
+
$timeout
>
time
())
{
if
(
$reflush
)
{
$one
[
'updated_at'
]
=
time
();
$one
->
save
();
}
return
$one
[
'value'
];
}
}
}
public
function
remove
(
$key
)
{
$uuid
=
Yii
::
$service
->
session
->
getUUID
();
$one
=
$this
->
_sessionModel
->
find
()
->
where
([
'uuid'
=>
$uuid
,
'key'
=>
$key
,
])
->
one
();
if
(
$one
[
'id'
])
{
$one
->
delete
();
return
true
;
}
}
/**
...
...
@@ -89,40 +47,16 @@ class SessionMysqldb extends Service implements SessionInterface
*/
public
function
destroy
()
{
if
(
!
Yii
::
$app
->
user
->
isGuest
)
{
$identity
=
Yii
::
$app
->
user
->
identity
;
$identity
->
access_token
=
''
;
$identity
->
access_token_created_at
=
null
;
$identity
->
save
();
}
$uuid
=
Yii
::
$service
->
session
->
getUUID
();
$result
=
$this
->
_sessionModel
->
deleteAll
([
'uuid'
=>
$uuid
,
]);
return
true
;
}
public
function
setFlash
(
$key
,
$val
,
$timeout
)
{
return
$this
->
set
(
$key
,
$val
,
$timeout
);
}
public
function
getFlash
(
$key
)
{
$uuid
=
Yii
::
$service
->
session
->
getUUID
();
$one
=
$this
->
_sessionModel
->
find
()
->
where
([
'uuid'
=>
$uuid
,
'key'
=>
$key
,
])
->
one
();
if
(
$one
[
'id'
])
{
$timeout
=
$one
[
'timeout'
];
$updated_at
=
$one
[
'updated_at'
];
if
(
$updated_at
+
$timeout
>
time
())
{
$val
=
$one
[
'value'
];
$one
->
delete
();
return
$val
;
}
}
}
}
services/session/SessionRedis.php
浏览文件 @
77593d8d
...
...
@@ -22,67 +22,37 @@ use fecshop\services\Service;
*/
class
SessionRedis
extends
Service
implements
SessionInterface
{
protected
$_sessionModelName
=
'\fecshop\models\redis\SessionStorage'
;
protected
$_sessionModel
;
protected
$storageSession
;
public
function
init
()
{
parent
::
init
();
list
(
$this
->
_sessionModelName
,
$this
->
_sessionModel
)
=
\
Yii
::
mapGet
(
$this
->
_sessionModelName
);
// SessionRedis 是单例模式,因此init只能执行一次,下面的也是只会执行一次。
$this
->
storageSession
=
new
\
yii\redis\Session
;
}
public
function
set
(
$key
,
$val
,
$timeout
)
{
$uuid
=
Yii
::
$service
->
session
->
getUUID
();
$r_id
=
$this
->
getUuidKey
(
$uuid
,
$key
);
$one
=
$this
->
_sessionModel
->
find
()
->
where
([
'id'
=>
$r_id
,
])
->
one
();
if
(
!
$one
[
'id'
])
{
$one
=
new
$this
->
_sessionModelName
();
$one
[
'session_uuid'
]
=
$uuid
;
$one
[
'session_key'
]
=
$key
;
$one
[
'id'
]
=
$r_id
;
}
$one
[
'session_value'
]
=
$val
;
$one
[
'session_timeout'
]
=
$timeout
;
$one
[
'session_updated_at'
]
=
time
();
$one
->
save
();
return
true
;
return
$this
->
storageSession
->
set
(
$r_id
,
$val
,
$timeout
);
}
public
function
get
(
$key
,
$reflush
)
{
$uuid
=
Yii
::
$service
->
session
->
getUUID
();
$r_id
=
$this
->
getUuidKey
(
$uuid
,
$key
);
$one
=
$this
->
_sessionModel
->
find
()
->
where
([
'id'
=>
$r_id
,
])
->
one
();
if
(
$one
[
'id'
])
{
$timeout
=
$one
[
'session_timeout'
];
$updated_at
=
$one
[
'session_updated_at'
];
if
(
$updated_at
+
$timeout
>
time
())
{
if
(
$reflush
)
{
$one
[
'session_updated_at'
]
=
time
();
$one
->
save
();
}
return
$one
[
'session_value'
];
}
}
return
$this
->
storageSession
->
get
(
$r_id
);
}
public
function
remove
(
$key
)
{
$uuid
=
Yii
::
$service
->
session
->
getUUID
();
$r_id
=
$this
->
getUuidKey
(
$uuid
,
$key
);
$one
=
$this
->
_sessionModel
->
find
()
->
where
([
'id'
=>
$r_id
,
])
->
one
();
if
(
$one
[
'id'
])
{
$one
->
delete
();
return
true
;
}
return
$this
->
storageSession
->
remove
(
$r_id
);
}
public
function
getUuidKey
(
$uuid
,
$key
)
...
...
@@ -95,40 +65,22 @@ class SessionRedis extends Service implements SessionInterface
*/
public
function
destroy
()
{
if
(
!
Yii
::
$app
->
user
->
isGuest
)
{
$identity
=
Yii
::
$app
->
user
->
identity
;
$identity
->
access_token
=
''
;
$identity
->
access_token_created_at
=
null
;
$identity
->
save
();
}
$uuid
=
Yii
::
$service
->
session
->
getUUID
();
$result
=
$this
->
_sessionModel
->
deleteAll
([
'session_uuid'
=>
$uuid
,
]);
return
true
;
return
$this
->
storageSession
->
destroy
();
}
public
function
setFlash
(
$key
,
$val
,
$timeout
)
{
return
$this
->
set
(
$key
,
$val
,
$timeout
);
$uuid
=
Yii
::
$service
->
session
->
getUUID
();
$r_id
=
$this
->
getUuidKey
(
$uuid
,
$key
);
return
$this
->
storageSession
->
setFlash
(
$r_id
,
$val
,
$timeout
);
}
public
function
getFlash
(
$key
)
{
$uuid
=
Yii
::
$service
->
session
->
getUUID
();
$r_id
=
$this
->
getUuidKey
(
$uuid
,
$key
);
$one
=
$this
->
_sessionModel
->
find
()
->
where
([
'id'
=>
$r_id
,
])
->
one
();
if
(
$one
[
'id'
])
{
$timeout
=
$one
[
'session_timeout'
];
$updated_at
=
$one
[
'session_updated_at'
];
if
(
$updated_at
+
$timeout
>
time
())
{
$val
=
$one
[
'session_value'
];
$one
->
delete
();
return
$val
;
}
}
return
$this
->
storageSession
->
getFlash
(
$r_id
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录