Heim > Backend-Entwicklung > PHP-Tutorial > 基于redis的处理session的步骤

基于redis的处理session的步骤

WBOY
Freigeben: 2016-06-13 12:28:53
Original
832 Leute haben es durchsucht

基于redis的处理session的方法

一个基于redis的处理session的方法,如下。

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Session_custom {</span><span style="color: #008080;"> 3</span>     <span style="color: #0000ff;">private</span> <span style="color: #800080;">$redis</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> redis实例</span><span style="color: #008080;"> 4</span>     <span style="color: #0000ff;">private</span> <span style="color: #800080;">$prefix</span> = 'sess_'; <span style="color: #008000;">//</span><span style="color: #008000;"> session_id前缀</span><span style="color: #008080;"> 5</span>     <span style="color: #008080;"> 6</span> <span style="color: #008000;">    // 会话开始时,会执行该方法,连接redis服务器</span><span style="color: #008080;"> 7</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> open(<span style="color: #800080;">$path</span>, <span style="color: #800080;">$name</span><span style="color: #000000;">) {</span><span style="color: #008080;"> 8</span>         <span style="color: #800080;">$this</span>->redis = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Redis();</span><span style="color: #008080;"> 9</span>         <span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->redis->connect("127.0.0.1",6379<span style="color: #000000;">);</span><span style="color: #008080;">10</span> <span style="color: #000000;">    }</span><span style="color: #008080;">11</span>     <span style="color: #008080;">12</span>     <span style="color: #008000;">//</span><span style="color: #008000;"> 会话结束时,调用该方法,关闭redis连接</span><span style="color: #008080;">13</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> close() {</span><span style="color: #008080;">14</span>         <span style="color: #800080;">$this</span>->redis-><span style="color: #000000;">close();</span><span style="color: #008080;">15</span>         <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;</span><span style="color: #008080;">16</span> <span style="color: #000000;">    }</span><span style="color: #008080;">17</span>     <span style="color: #008080;">18</span>     <span style="color: #008000;">//</span><span style="color: #008000;"> 会话保存数据时调用该方法,在脚本执行完或session_write_close方法调用之后调用</span><span style="color: #008080;">19</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> write(<span style="color: #800080;">$session_id</span>, <span style="color: #800080;">$data</span><span style="color: #000000;">) {</span><span style="color: #008080;">20</span>         <span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->redis->hMSet(<span style="color: #800080;">$this</span>->prefix.<span style="color: #800080;">$session_id</span>, <span style="color: #0000ff;">array</span>('expires' => <span style="color: #008080;">time</span>(), 'data' => <span style="color: #800080;">$data</span><span style="color: #000000;">));</span><span style="color: #008080;">21</span> <span style="color: #000000;">    }</span><span style="color: #008080;">22</span>     <span style="color: #008080;">23</span>     <span style="color: #008000;">//</span><span style="color: #008000;"> 在自动开始会话或者通过调用 session_start() 函数手动开始会话之后,PHP 内部调用 read 回调函数来获取会话数据。</span><span style="color: #008080;">24</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> read(<span style="color: #800080;">$session_id</span><span style="color: #000000;">) {</span><span style="color: #008080;">25</span>         <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->redis->exists(<span style="color: #800080;">$this</span>->prefix.<span style="color: #800080;">$session_id</span><span style="color: #000000;">)) {</span><span style="color: #008080;">26</span>             <span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->redis->hGet(<span style="color: #800080;">$this</span>->prefix.<span style="color: #800080;">$session_id</span>, 'data'<span style="color: #000000;">);</span><span style="color: #008080;">27</span> <span style="color: #000000;">        }</span><span style="color: #008080;">28</span>         <span style="color: #0000ff;">return</span> ''<span style="color: #000000;">;</span><span style="color: #008080;">29</span> <span style="color: #000000;">    }</span><span style="color: #008080;">30</span>     <span style="color: #008080;">31</span>     <span style="color: #008000;">//</span><span style="color: #008000;"> 清除会话中的数据,当调用session_destroy()函数,或者调用 session_regenerate_id()函数并且设置 destroy 参数为 TRUE 时,会调用此回调函数。</span><span style="color: #008080;">32</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> destroy(<span style="color: #800080;">$session_id</span><span style="color: #000000;">) {</span><span style="color: #008080;">33</span>         <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->redis->exists(<span style="color: #800080;">$this</span>->prefix.<span style="color: #800080;">$session_id</span><span style="color: #000000;">)) {</span><span style="color: #008080;">34</span>             <span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->redis->del(<span style="color: #800080;">$this</span>->prefix.<span style="color: #800080;">$session_id</span>) > 0 ? <span style="color: #0000ff;">true</span> : <span style="color: #0000ff;">false</span><span style="color: #000000;">;</span><span style="color: #008080;">35</span> <span style="color: #000000;">        }</span><span style="color: #008080;">36</span>         <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;</span><span style="color: #008080;">37</span> <span style="color: #000000;">    }</span><span style="color: #008080;">38</span>     <span style="color: #008080;">39</span>     <span style="color: #008000;">//</span><span style="color: #008000;"> 垃圾回收函数,调用周期由 session.gc_probability 和 session.gc_divisor 参数控制</span><span style="color: #008080;">40</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> gc(<span style="color: #800080;">$maxlifetime</span><span style="color: #000000;">) {</span><span style="color: #008080;">41</span>         <span style="color: #800080;">$allKeys</span> = <span style="color: #800080;">$this</span>->redis->keys("{<span style="color: #800080;">$this</span>->prefix}*"<span style="color: #000000;">);</span><span style="color: #008080;">42</span>         <span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$allKeys</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span><span style="color: #000000;">) {</span><span style="color: #008080;">43</span>             <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->redis->exists(<span style="color: #800080;">$key</span>) && <span style="color: #800080;">$this</span>->redis->hGet(<span style="color: #800080;">$key</span>, 'expires') + <span style="color: #800080;">$maxlifetime</span> time<span style="color: #000000;">()) {</span><span style="color: #008080;">44</span>                 <span style="color: #800080;">$this</span>->redis->del(<span style="color: #800080;">$key</span><span style="color: #000000;">);</span><span style="color: #008080;">45</span> <span style="color: #000000;">            }</span><span style="color: #008080;">46</span> <span style="color: #000000;">        }</span><span style="color: #008080;">47</span>         <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;</span><span style="color: #008080;">48</span> <span style="color: #000000;">    }</span><span style="color: #008080;">49</span> <span style="color: #000000;">}</span><span style="color: #008080;">50</span> <span style="color: #008080;">51</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 调用自定义的session处理方法</span><span style="color: #008080;">52</span> <span style="color: #800080;">$handler</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Session_custom();</span><span style="color: #008080;">53</span> <span style="color: #008080;">session_set_save_handler</span><span style="color: #000000;">(</span><span style="color: #008080;">54</span>     <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$handler</span>, 'open'),<span style="color: #008080;">55</span>     <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$handler</span>, 'close'),<span style="color: #008080;">56</span>     <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$handler</span>, 'read'),<span style="color: #008080;">57</span>     <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$handler</span>, 'write'),<span style="color: #008080;">58</span>     <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$handler</span>, 'destroy'),<span style="color: #008080;">59</span>     <span style="color: #0000ff;">array</span>(<span style="color: #800080;">$handler</span>, 'gc'<span style="color: #000000;">)</span><span style="color: #008080;">60</span> <span style="color: #000000;">);</span><span style="color: #008080;">61</span> <span style="color: #008080;">62</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 下面这行代码可以防止使用对象作为会话保存管理器时可能引发的非预期行为,表示当脚本执行之后或调用exit()之后,存储当前会话数据并关闭当前会话</span><span style="color: #008080;">63</span> <span style="color: #008080;">register_shutdown_function</span>('session_write_close'<span style="color: #000000;">);</span><span style="color: #008080;">64</span> <span style="color: #008080;">65</span> <span style="color: #008080;">session_start</span><span style="color: #000000;">();</span><span style="color: #008080;">66</span> <span style="color: #008080;">67</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 可以使用session了</span>
Nach dem Login kopieren

补充:

php.ini文件中的session.gc_probability与session.gc_divisor两个配置选项共同决定gc函数调用的时机。默认值分为为1和1000,表示每个请求只有1/1000的机会调用gc函数。

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage