将Session写入Memcache
通过session_set_save_handler()方法自定义Session写入Memcache
<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;"> MemSession{</span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$handler</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">;</span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$lifetime</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">;</span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #800080;">$time</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">;</span><span style="color: #008080;"> 6</span> <span style="color: #0000ff;">const</span> MS = 'session'<span style="color: #000000;">;</span><span style="color: #008080;"> 7</span> <span style="color: #008080;"> 8</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> init(<span style="color: #800080;">$handler</span><span style="color: #000000;">){</span><span style="color: #008080;"> 9</span> self::<span style="color: #800080;">$handler</span> = <span style="color: #800080;">$handler</span><span style="color: #000000;">;</span><span style="color: #008080;">10</span> self::<span style="color: #800080;">$lifetime</span> = <span style="color: #008080;">ini_get</span>('session.gc_maxlifetime'<span style="color: #000000;">);</span><span style="color: #008080;">11</span> self::<span style="color: #800080;">$time</span> = <span style="color: #008080;">time</span><span style="color: #000000;">();</span><span style="color: #008080;">12</span> <span style="color: #000000;"> }</span><span style="color: #008080;">13</span> <span style="color: #008080;">14</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> start(<span style="color: #800080;">$memcache</span><span style="color: #000000;">){</span><span style="color: #008080;">15</span> self::init(<span style="color: #800080;">$memcache</span><span style="color: #000000;">);</span><span style="color: #008080;">16</span> <span style="color: #008000;">//</span><span style="color: #008000;">调用类中的方法要用数组,__CLASS__代表本类</span><span style="color: #008080;">17</span> <span style="color: #008080;">session_set_save_handler</span><span style="color: #000000;">(</span><span style="color: #008080;">18</span> <span style="color: #0000ff;">array</span>(<span style="color: #ff00ff;">__CLASS__</span>,'open'),<span style="color: #008080;">19</span> <span style="color: #0000ff;">array</span>(<span style="color: #ff00ff;">__CLASS__</span>,'close'),<span style="color: #008080;">20</span> <span style="color: #0000ff;">array</span>(<span style="color: #ff00ff;">__CLASS__</span>,'read'),<span style="color: #008080;">21</span> <span style="color: #0000ff;">array</span>(<span style="color: #ff00ff;">__CLASS__</span>,'write'),<span style="color: #008080;">22</span> <span style="color: #0000ff;">array</span>(<span style="color: #ff00ff;">__CLASS__</span>,'destroy'),<span style="color: #008080;">23</span> <span style="color: #0000ff;">array</span>(<span style="color: #ff00ff;">__CLASS__</span>,'gc'<span style="color: #000000;">)</span><span style="color: #008080;">24</span> <span style="color: #000000;"> );</span><span style="color: #008080;">25</span> <span style="color: #008080;">session_start</span><span style="color: #000000;">();</span><span style="color: #008080;">26</span> <span style="color: #000000;"> }</span><span style="color: #008080;">27</span> <span style="color: #008080;">28</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</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;">29</span> <span style="color: #008080;">30</span> <span style="color: #000000;"> }</span><span style="color: #008080;">31</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> close(){</span><span style="color: #008080;">32</span> <span style="color: #008080;">33</span> <span style="color: #000000;"> }</span><span style="color: #008080;">34</span> <span style="color: #008080;">35</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> read(<span style="color: #800080;">$PHPSESSID</span><span style="color: #000000;">){</span><span style="color: #008080;">36</span> <span style="color: #800080;">$val</span> = self::<span style="color: #800080;">$handler</span>->get(self::session_key(<span style="color: #800080;">$PHPSESSID</span><span style="color: #000000;">));</span><span style="color: #008080;">37</span> <span style="color: #008080;">38</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$val</span>===<span style="color: #0000ff;">false</span> || <span style="color: #800080;">$val</span>==<span style="color: #0000ff;">null</span><span style="color: #000000;">){</span><span style="color: #008080;">39</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;</span><span style="color: #008080;">40</span> <span style="color: #000000;"> }</span><span style="color: #008080;">41</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$val</span><span style="color: #000000;">;</span><span style="color: #008080;">42</span> <span style="color: #000000;"> }</span><span style="color: #008080;">43</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> write(<span style="color: #800080;">$PHPSESSID</span>,<span style="color: #800080;">$data</span><span style="color: #000000;">){</span><span style="color: #008080;">44</span> <span style="color: #800080;">$method</span> = <span style="color: #800080;">$data</span>? 'set':'replace'<span style="color: #000000;">;</span><span style="color: #008080;">45</span> <span style="color: #0000ff;">return</span> self::<span style="color: #800080;">$handler</span>-><span style="color: #800080;">$method</span>(self::session_key(<span style="color: #800080;">$PHPSESSID</span>),<span style="color: #800080;">$data</span>,MEMCACHE_COMPRESSED,self::<span style="color: #800080;">$lifetime</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: #008080;">48</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> destroy(<span style="color: #800080;">$PHPSESSID</span><span style="color: #000000;">){</span><span style="color: #008080;">49</span> <span style="color: #0000ff;">return</span> self::<span style="color: #800080;">$handle</span>->delete(self::session_key(<span style="color: #800080;">$PHPSESSID</span><span style="color: #000000;">));</span><span style="color: #008080;">50</span> <span style="color: #000000;"> }</span><span style="color: #008080;">51</span> <span style="color: #008000;">//</span><span style="color: #008000;">memcache本身就有限定时间,数据自动销毁,所以可不使用gc方法</span><span style="color: #008080;">52</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> gc(<span style="color: #800080;">$lifetime</span><span style="color: #000000;">){</span><span style="color: #008080;">53</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;</span><span style="color: #008080;">54</span> <span style="color: #000000;"> }</span><span style="color: #008080;">55</span> <span style="color: #008080;">56</span> <span style="color: #008000;">//</span><span style="color: #008000;">给sessionID加前缀,避免key重复</span><span style="color: #008080;">57</span> <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> session_key(<span style="color: #800080;">$PHPSESSID</span><span style="color: #000000;">){</span><span style="color: #008080;">58</span> <span style="color: #800080;">$session_key</span> = self::MS.<span style="color: #800080;">$PHPSESSID</span><span style="color: #000000;">;</span><span style="color: #008080;">59</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$session_key</span><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: #000000;"> }</span><span style="color: #008080;">62</span> <span style="color: #800080;">$mem</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Memcache;</span><span style="color: #008080;">63</span> <span style="color: #800080;">$mem</span>->connect("localhost",11211) or <span style="color: #0000ff;">die</span>("could not connect"<span style="color: #000000;">);</span><span style="color: #008080;">64</span> MemSession::start(<span style="color: #800080;">$mem</span>);