>  기사  >  백엔드 개발  >  PHP는 Redis 스토리지 세션을 구현합니다.

PHP는 Redis 스토리지 세션을 구현합니다.

WBOY
WBOY원래의
2016-07-29 09:15:181097검색

一、首先实现SessionHandlerInterface(此接口PHP>5.4.0),如下

<pre font-size:9.0pt><span><?php
</span><span><em>/**
</em></span><span><em> * 以db的方式存储<strong>Session</strong>
</em></span><span><em> */
</em></span><span>namespace </span><span>OC\<strong>Session</strong></span><span>;
</span><span></span><span>class </span><span>redis<strong>Session</strong> </span><span>implements </span><span>\<strong>Session</strong>HandlerInterface{
</span><span></span><span><em>/**
</em></span><span><em>     * 保存<strong>Session</strong>的数据库表的信息
</em></span><span><em>     */
</em></span><span><em></em></span><span>private </span><span>$_options </span><span>= </span><span>array</span><span>(
</span><span></span><span>'handler' </span><span>=> </span><span>null</span><span>, </span><span>//数据库连接句柄
</span><span></span><span>'host' </span><span>=> </span><span>null</span><span>,
</span><span></span><span>'port' </span><span>=> </span><span>null</span><span>,
</span><span></span><span>'lifeTime' </span><span>=> </span><span>null</span><span>,
</span><span></span><span>)</span><span>;
</span><span></span><span><em>/**
</em></span><span><em>     * 构造函数
</em></span><span><em>     * </em></span><span><em>@param </em></span><span><em>$options 设置信息数组
</em></span><span><em>     */
</em></span><span><em></em></span><span>public function </span><span>__construct</span><span>(</span><span>$options</span><span>=</span><span>array</span><span>()){
</span><span></span><span>if</span><span>(!class_exists(</span><span>"redis"</span><span>, </span><span>false</span><span>)){
</span><span></span><span>die</span><span>(</span><span>"必须安装redis扩展"</span><span>)</span><span>;
</span><span></span><span>}
</span><span></span><span>if</span><span>(!</span><span>isset</span><span>(</span><span>$options</span><span>[</span><span>'lifeTime'</span><span>]) || </span><span>$options</span><span>[</span><span>'lifeTime'</span><span>] <= </span><span>0</span><span>){
</span><span></span><span>$options</span><span>[</span><span>'lifeTime'</span><span>] = ini_get(</span><span>'<strong>Session</strong>.gc_maxlifetime'</span><span>)</span><span>;
</span><span></span><span>}
</span><span></span><span>$this</span><span>-></span><span>_options </span><span>= array_merge(</span><span>$this</span><span>-></span><span>_options</span><span>, </span><span>$options</span><span>)</span><span>;
</span><span></span><span>}
</span><span></span><span><em>/**
</em></span><span><em>     * 开始使用该驱动的<strong>Session</strong>
</em></span><span><em>     */
</em></span><span><em></em></span><span>public function </span><span>begin</span><span>(){
</span><span></span><span>if</span><span>(</span><span>$this</span><span>-></span><span>_options</span><span>[</span><span>'host'</span><span>] === </span><span>null </span><span>||
</span><span></span><span>$this</span><span>-></span><span>_options</span><span>[</span><span>'port'</span><span>] === </span><span>null </span><span>||
</span><span></span><span>$this</span><span>-></span><span>_options</span><span>[</span><span>'lifeTime'</span><span>] === </span><span>null
</span><span></span><span>){
</span><span></span><span>return false</span><span>;
</span><span></span><span>}
</span><span></span><span>//设置<strong>Session</strong>处理函数
</span><span></span><span><strong>Session</strong>_set_save_handler(
</span><span></span><span>array</span><span>(</span><span>$this</span><span>, </span><span>'open'</span><span>)</span><span>,
</span><span></span><span>array</span><span>(</span><span>$this</span><span>, </span><span>'close'</span><span>)</span><span>,
</span><span></span><span>array</span><span>(</span><span>$this</span><span>, </span><span>'read'</span><span>)</span><span>,
</span><span></span><span>array</span><span>(</span><span>$this</span><span>, </span><span>'write'</span><span>)</span><span>,
</span><span></span><span>array</span><span>(</span><span>$this</span><span>, </span><span>'destory'</span><span>)</span><span>,
</span><span></span><span>array</span><span>(</span><span>$this</span><span>, </span><span>'gc'</span><span>)
</span><span>        )</span><span>;
</span><span></span><span>}
</span><span></span><span><em>/**
</em></span><span><em>     * 自动开始回话或者<strong>Session</strong>_start()开始回话后第一个调用的函数
</em></span><span><em>     * 类似于构造函数的作用
</em></span><span><em>     * </em></span><span><em>@param </em></span><span><em>$savePath 默认的保存路径
</em></span><span><em>     * </em></span><span><em>@param </em></span><span><em>$<strong>Session</strong>Name 默认的参数名,PHPSESSID
</em></span><span><em>     */
</em></span><span><em></em></span><span>public function </span><span>open</span><span>(</span><span>$savePath</span><span>, </span><span>$<strong>Session</strong>Name</span><span>){
</span><span></span><span>if</span><span>(is_resource(</span><span>$this</span><span>-></span><span>_options</span><span>[</span><span>'handler'</span><span>])) </span><span>return true</span><span>;
</span><span></span><span>//连接redis
</span><span></span><span>$redisHandle </span><span>= </span><span>new </span><span>\Redis()</span><span>;
</span><span></span><span>$redisHandle</span><span>-></span><span>connect</span><span>(</span><span>$this</span><span>-></span><span>_options</span><span>[</span><span>'host'</span><span>]</span><span>, </span><span>$this</span><span>-></span><span>_options</span><span>[</span><span>'port'</span><span>])</span><span>;
</span><span></span><span>if</span><span>(!</span><span>$redisHandle</span><span>){
</span><span></span><span>return false</span><span>;
</span><span></span><span>}
</span><span></span><span>$this</span><span>-></span><span>_options</span><span>[</span><span>'handler'</span><span>] = </span><span>$redisHandle</span><span>;
</span><span></span><span>$this</span><span>-></span><span>gc</span><span>(</span><span>null</span><span>)</span><span>;
</span><span></span><span>return true</span><span>;
</span><span></span><span>}
</span><span></span><span><em>/**
</em></span><span><em>     * 类似于析构函数,在write之后调用或者<strong>Session</strong>_write_close()函数之后调用
</em></span><span><em>     */
</em></span><span><em></em></span><span>public function </span><span>close</span><span>(){
</span><span></span><span>return </span><span>$this</span><span>-></span><span>_options</span><span>[</span><span>'handler'</span><span>]-></span><span>close</span><span>()</span><span>;
</span><span></span><span>}
</span><span></span><span><em>/**
</em></span><span><em>     * 读取<strong>Session</strong>信息
</em></span><span><em>     * </em></span><span><em>@param </em></span><span><em>$<strong>Session</strong>Id 通过该Id唯一确定对应的<strong>Session</strong>数据
</em></span><span><em>     * </em></span><span><em>@return </em></span><span><em><strong>Session</strong>信息/空串
</em></span><span><em>     */
</em></span><span><em></em></span><span>public function </span><span>read</span><span>(</span><span>$<strong>Session</strong>Id</span><span>){
</span><span></span><span>return </span><span>$this</span><span>-></span><span>_options</span><span>[</span><span>'handler'</span><span>]-></span><span>get</span><span>(</span><span>$<strong>Session</strong>Id</span><span>)</span><span>;
</span><span></span><span>}
</span><span></span><span><em>/**
</em></span><span><em>     * 写入或者修改<strong>Session</strong>数据
</em></span><span><em>     * </em></span><span><em>@param </em></span><span><em>$<strong>Session</strong>Id 要写入数据的<strong>Session</strong>对应的id
</em></span><span><em>     * </em></span><span><em>@param </em></span><span><em>$<strong>Session</strong>Data 要写入的数据,已经序列化过了
</em></span><span><em>     */
</em></span><span><em></em></span><span>public function </span><span>write</span><span>(</span><span>$<strong>Session</strong>Id</span><span>, </span><span>$<strong>Session</strong>Data</span><span>){
</span><span></span><span>return </span><span>$this</span><span>-></span><span>_options</span><span>[</span><span>'handler'</span><span>]-></span><span>setex</span><span>(</span><span>$<strong>Session</strong>Id</span><span>, </span><span>$this</span><span>-></span><span>_options</span><span>[</span><span>'lifeTime'</span><span>]</span><span>, </span><span>$<strong>Session</strong>Data</span><span>)</span><span>;
</span><span></span><span>}
</span><span></span><span><em>/**
</em></span><span><em>     * 主动销毁<strong>Session</strong>会话
</em></span><span><em>     * </em></span><span><em>@param </em></span><span><em>$<strong>Session</strong>Id 要销毁的会话的唯一id
</em></span><span><em>     */
</em></span><span><em></em></span><span>public function </span><span>destroy</span><span>(</span><span>$<strong>Session</strong>Id</span><span>){
</span><span></span><span>return </span><span>$this</span><span>-></span><span>_options</span><span>[</span><span>'handler'</span><span>]-></span><span>delete</span><span>(</span><span>$<strong>Session</strong>Id</span><span>) >= </span><span>1 </span><span>? </span><span>true </span><span>: </span><span>false</span><span>;
</span><span></span><span>}
</span><span></span><span><em>/**
</em></span><span><em>     * 清理绘画中的过期数据
</em></span><span><em>     * </em></span><span><em>@param </em></span><span><em>有效期
</em></span><span><em>     */
</em></span><span><em></em></span><span>public function </span><span>gc</span><span>(</span><span>$lifeTime</span><span>){
</span><span></span><span>//获取所有<strong>Session</strong>id,让过期的释放掉
</span><span></span><span>$this</span><span>-></span><span>_options</span><span>[</span><span>'handler'</span><span>]-></span><span>keys</span><span>(</span><span>"*"</span><span>)</span><span>;
</span><span></span><span>return true</span><span>;
</span><span></span><span>}
</span><span>}</span>
以上就是一个完整的实现接口的类,在此封装成一个文件
<span>二、调用时使用方法:</span>
<pre font-size:9.0pt><span><strong>Session</strong>_set_save_handler(\OC::</span><span><em>$server</em></span><span>-></span><span>getRedis<strong>Session</strong></span><span>()</span><span>,</span><span>true</span><span>)</span><span>;
</span><span>register_shutdown_function(</span><span>'<strong>Session</strong>_write_close'</span><span>)</span><span>;</span><span></span><span><strong>Session</strong>_start()</span><span>;</span>
也就是在Session_start()方法之前调用,相当于告诉Session存储位置变成redis存储,至此,以后使用的所有$_Session方法存储的数据都会自动放到redis中去,而存储用的key值就是Session_id().
<span>三、解释:</span>
<span>\OC::</span><span><em>$server</em></span><span>-></span><span>getRedis<strong>Session</strong></span><span>()</span>

以上的调用对象的方法时使用了pimple容器存储对象并调用的方式。具体如下使用:

<span>namespace </span><span>OC</span><span>;</span>
<span>class </span><span>Server </span><span>extends </span><span>SimpleContainer </span><span>implements </span><span>IServerContainer {</span>

在此server类中注册加入对象

<span>$this</span><span>-></span><span>registerService</span><span>(</span><span>'Redis<strong>Session</strong>'</span><span>,</span><span>function </span><span>(Server </span><span>$c</span><span>) {
</span><span></span><span>return new </span><span>\OC\<strong>Session</strong>\redis<strong>Session</strong>(
</span><span></span><span>$c</span><span>-></span><span>getSystemConfig</span><span>()-></span><span>getValue</span><span>(</span><span>'redis'</span><span>)
</span><span>   )</span><span>;
</span><span>})</span><span>;</span>
获取对象方式:
<span><em>/**
</em></span><span><em> * 新增的获取用户redis<strong>Session</strong>的方法
</em></span><span><em> */
</em></span><span>public function </span><span>getRedis<strong>Session</strong></span><span>() {
</span><span></span><span>return </span><span>$this</span><span>-></span><span>query</span><span>(</span><span>'Redis<strong>Session</strong>'</span><span>)</span><span>;
</span><span>}</span>
其实也就是
<span><strong>Session</strong>_set_save_handler(\OC::</span><span><em>$server</em></span><span>-></span><span>getRedis<strong>Session</strong></span><span>()</span><span>,</span><span>true</span><span>)</span><span>;中的</span>
<span>\OC::</span><span><em>$server</em></span><span>-></span><span>getRedis<strong>Session</strong></span><span>()</span>

就是上面一、中的类的对象

参考:http://www.tuicool.com/articles/yeeyume

以上就介绍了PHP实现redis存储session,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.