合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# php操作Memcached对象 ``` <pre class="calibre15">``` <span class="token5">1.</span>创建一个Memcached对象 $m <span class="token">=</span> <span class="token2">new</span> <span class="token3">Memcached</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token">**</span><span class="token5">2.</span>传入一台或多台服务器<span class="token">**</span> <span class="token6">//传入一台服务器</span> $m<span class="token">-</span><span class="token">></span><span class="token3">addServer</span><span class="token1">(</span><span class="token4">'127.0.0.1'</span><span class="token1">,</span><span class="token4">'11211'</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//传入多台服务器</span> $servers <span class="token">=</span> <span class="token3">array</span><span class="token1">(</span> <span class="token3">array</span><span class="token1">(</span><span class="token4">'127.0.0.1'</span><span class="token1">,</span><span class="token4">'11211'</span><span class="token1">)</span><span class="token1">,</span> <span class="token3">array</span><span class="token1">(</span><span class="token4">'127.0.0.2'</span><span class="token1">,</span><span class="token4">'11211'</span><span class="token1">)</span> <span class="token1">)</span><span class="token1">;</span> $m<span class="token">-</span><span class="token">></span><span class="token3">addServers</span><span class="token1">(</span>$servers<span class="token1">)</span><span class="token1">;</span> <span class="token6">//查看一下运行状态</span> <span class="token3">print_r</span><span class="token1">(</span>$m<span class="token">-</span><span class="token">></span><span class="token3">getStats</span><span class="token1">(</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> 因为传入的第二台服务器是不存在的,所以无法获取到正确的信息。 <span class="token3">也可以使用getVersion</span><span class="token1">(</span><span class="token1">)</span>来直接获取memcached的版本信息 <span class="token5">3.</span><span class="token3">add</span><span class="token1">(</span><span class="token1">)</span>方法和<span class="token2">get</span><span class="token1">(</span><span class="token1">)</span>方法 <span class="token6">/* * add() * key代表加入缓存的key值 * value代表加入缓存的value值 * 600代表这条缓存的生效时间为600秒,0代表永久生效 */</span> $m<span class="token">-</span><span class="token">></span><span class="token3">add</span><span class="token1">(</span><span class="token4">'key'</span><span class="token1">,</span><span class="token4">'value'</span><span class="token1">,</span><span class="token5">600</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">/* * get() * 通过key值来获取缓存 */</span> echo <span class="token4">"缓存key值:"</span><span class="token1">.</span>$m<span class="token">-</span><span class="token">></span><span class="token2">get</span><span class="token1">(</span>'key<span class="token1">)</span><span class="token1">;</span> <span class="token3">如果我在第一个add</span><span class="token1">(</span><span class="token1">)</span>方法下面再添加一个: $m<span class="token">-</span><span class="token">></span><span class="token3">add</span><span class="token1">(</span><span class="token4">'key'</span><span class="token1">,</span><span class="token4">'value1'</span><span class="token1">,</span><span class="token5">600</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">其实这样是不会覆盖掉前面的值得,如果想要覆盖的话可以使用replace</span><span class="token1">(</span><span class="token1">)</span>方法 <span class="token5">4.</span><span class="token3">replace</span><span class="token1">(</span><span class="token1">)</span>方法 $m<span class="token">-</span><span class="token">></span><span class="token3">add</span><span class="token1">(</span><span class="token4">'key'</span><span class="token1">,</span><span class="token4">'value'</span><span class="token1">,</span><span class="token5">600</span><span class="token1">)</span><span class="token1">;</span> $m<span class="token">-</span><span class="token">></span><span class="token3">replace</span><span class="token1">(</span><span class="token4">'key'</span><span class="token1">,</span><span class="token4">'value11'</span><span class="token1">,</span><span class="token5">600</span><span class="token1">)</span><span class="token1">;</span> 这样当我们再次<span class="token2">get</span><span class="token1">(</span>‘key’<span class="token1">)</span>的时候就会获取到value11了。 <span class="token5">5.</span><span class="token2">set</span><span class="token1">(</span><span class="token1">)</span>方法 <span class="token2">set</span><span class="token1">(</span><span class="token1">)</span>方法其实就集成了add方法和replace方法,如果<span class="token2">set</span>的key值是不存在的,它就相当于add方法,如果<span class="token2">set</span>的key值已经存在了,它就相当于replace方法,所以一般的时候<span class="token2">set</span>方法才是最常用的方法。 $m<span class="token">-</span><span class="token">></span><span class="token2">set</span><span class="token1">(</span><span class="token4">'key'</span><span class="token1">,</span><span class="token4">'value'</span><span class="token1">,</span><span class="token5">600</span><span class="token1">)</span><span class="token1">;</span> <span class="token5">6.</span><span class="token2">delete</span><span class="token1">(</span><span class="token1">)</span>方法 删除缓存的方法,使用方法: <span class="token6">/* * delete() * 传入一条缓存的key */</span> $m<span class="token">-</span><span class="token">></span><span class="token2">delete</span><span class="token1">(</span><span class="token4">'key'</span><span class="token1">)</span><span class="token1">;</span> 加入我上面添加的缓存失效时间还没有到,执行<span class="token2">delete</span>方法后,我们依然无法获取到这条缓存了。 <span class="token5">7.</span><span class="token3">flush</span><span class="token1">(</span><span class="token1">)</span>方法 直接清除掉所有的缓存!(慎用!!!) <span class="token5">8.</span><span class="token3">increment</span><span class="token1">(</span><span class="token1">)</span>方法 加法操作,使用方法: <span class="token6">//设置一条key值为num,value为5,永久生效的缓存</span> $m<span class="token">-</span><span class="token">></span><span class="token2">set</span><span class="token1">(</span><span class="token4">'num'</span><span class="token1">,</span><span class="token5">5</span><span class="token1">,</span><span class="token5">0</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//没刷新一次页面key值为num的缓存value加5</span> $m<span class="token">-</span><span class="token">></span><span class="token3">increment</span><span class="token1">(</span><span class="token4">'num'</span><span class="token1">,</span><span class="token5">5</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//输出这条缓存</span> echo $m<span class="token">-</span><span class="token">></span><span class="token2">get</span><span class="token1">(</span><span class="token4">'num'</span><span class="token1">)</span><span class="token1">;</span> <span class="token5">9.</span><span class="token3">decrement</span><span class="token1">(</span><span class="token1">)</span>方法 <span class="token3">和increment</span><span class="token1">(</span><span class="token1">)</span><span class="token3">方法相似,只是变成了减法,具体使用方法和increment</span><span class="token1">(</span><span class="token1">)</span>方法一样。 更加优雅的使用Memcached方法 <span class="token5">10.</span><span class="token3">setMulti</span><span class="token1">(</span><span class="token1">)</span>方法 如果一直按照前面的方法进行<span class="token2">set</span><span class="token3">缓存的话,一条一条存入是很慢的,所以Memcached为我们提供给了setMulti</span><span class="token1">(</span><span class="token1">)</span>方法,使用方法如下: <span class="token6">//首先定义要存入缓存的数组</span> $data <span class="token">=</span> <span class="token3">array</span><span class="token1">(</span> <span class="token4">'key1'</span><span class="token">=></span><span class="token4">'value1'</span><span class="token1">,</span> <span class="token4">'key2'</span><span class="token">=></span><span class="token4">'value2'</span> <span class="token1">)</span><span class="token1">;</span> <span class="token6">/* * setMulti() * 第一个参数代表传入的数组,第二个参数代表生效时间,0代表永久 */</span> $m<span class="token">-</span><span class="token">></span><span class="token3">setMulti</span><span class="token1">(</span>$data<span class="token1">,</span><span class="token5">0</span><span class="token1">)</span><span class="token1">;</span> <span class="token5">11.</span><span class="token3">getMulti</span><span class="token1">(</span><span class="token1">)</span>方法 同样获取缓存也可以一次获取多条缓存,使用方法如下: <span class="token6">//定义获取缓存的key值得数组</span> $<span class="token2">get</span> <span class="token">=</span> <span class="token3">array</span><span class="token1">(</span><span class="token4">'key1'</span><span class="token1">,</span><span class="token4">'key2'</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">/* * getMulti() * 传入需要获取缓存的key值 */</span> $result <span class="token">=</span> $m<span class="token">-</span><span class="token">></span><span class="token3">getMulti</span><span class="token1">(</span>$<span class="token2">get</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">var_dump</span><span class="token1">(</span>$result<span class="token1">)</span><span class="token1">;</span> <span class="token5">12.</span><span class="token3">deleteMulti</span><span class="token1">(</span><span class="token1">)</span>方法 <span class="token3">和getMulti</span><span class="token1">(</span><span class="token1">)</span>方法类似,不过是删除缓存,使用方法如下: $<span class="token2">delete</span> <span class="token">=</span> <span class="token3">array</span><span class="token1">(</span><span class="token4">'key1'</span><span class="token1">,</span><span class="token4">'key2'</span><span class="token1">)</span><span class="token1">;</span> $m<span class="token">-</span><span class="token">></span><span class="token3">deleteMulti</span><span class="token1">(</span>$<span class="token2">delete</span><span class="token1">)</span><span class="token1">;</span> 这时key值为key1和key2的缓存就被删除掉了 <span class="token5">13.</span><span class="token3">getResultCode</span><span class="token1">(</span><span class="token1">)</span>方法 当我执行完一个操作之后可以使用这个方法来查看执行的情况,使用方法如下: <span class="token6">//刚才我执行了deleteMulti()方法,这时候我可以使用getResultCode()方法来查看操作是否成功</span> echo $m<span class="token">-</span><span class="token">></span><span class="token3">getResultCode</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> 当正确执行时会放回<span class="token5">0</span>; <span class="token5">14.</span><span class="token3">getResultMessage</span><span class="token1">(</span><span class="token1">)</span>方法 <span class="token3">和getResultCode</span><span class="token1">(</span><span class="token1">)</span><span class="token3">方法的区别是,getResultCode</span><span class="token1">(</span><span class="token1">)</span><span class="token3">是返回状态码,getResultMessage</span><span class="token1">(</span><span class="token1">)</span>方法是返回具体信息。使用方法如下: <span class="token6">//上面执行deleteMulti()方法</span> $m<span class="token">-</span><span class="token">></span><span class="token3">getResultMessage</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> 这是回返回一个<span class="token5">SUCCESS</span>的信息,告诉我们操作成功了 ``` ```