php缓存扩展memcached的使用,主要是set、get、delete等用法。
安装(省略的步骤,自行百度)好memcached后在php.ini引入扩展,重启php即可:
......(省略其他)
extension=memcached.so
看看phpinfo:
ok,接下来测试:
$options = [ 'host' => '127.0.0.1', 'port' => f, 'expire' => 0, 'timeout' => 0, // 超时时间(单位:毫秒) 'prefix' => '', 'username' => '', //账号 'password' => '', //密码 'option' => [], ]; $mem = new Memcached($options); $mem->set('xiaowei', '爱死你了', 3600); $mem->set('arrtest', ['job' => '收钱', 'man' => '对我好的'], 3600); $mem->set('json', json_encode(['job' => '收钱', 'man' => '对我好的'], JSON_UNESCAPED_UNICODE), 3600); print_r([ 'str' => $mem->get('xiaowei'),//字符串格式获取 'arr' => $mem->get('arrtest'),//数组获取 'json' => $mem->get('json'),//json格式,其实就是字符串 'remember' => $mem->remember('da', 'ccc'),//如果不存在则写入缓存 'rm' => $mem->rm('xiaowei'),//删除delete 'afterXiaowei' => $mem->get('xiaowei'),//获取 'has' => $mem->has('arrtest'),//判断有无 ]);
结果:
Array ( [str] => 爱死你了 [arr] => Array ( [job] => 收钱 [man] => 对我好的 ) [json] => {"job":"收钱","man":"对我好的"} [remember] => ccc [rm] => 1 [afterXiaowei] => [has] => 1 )
这次使用的是基于tp5框架的使用,直接使用是一样的,是支持服务器集群的缓存。