PHP は Redis ストレージ セッションを実装します

WBOY
リリース: 2016-07-29 09:15:18
オリジナル
1097 人が閲覧しました

一、首先实现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教程有兴趣的朋友有所帮助。

関連ラベル:
gt
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート