php无法操作php memcache操作类求助

The page is temporarily unavailable
nginx error!
The page you are looking for is temporarily unavailable.
Please try again later.
Website Administrator
Something has triggered an error on your
This is the default error page for
nginx that is distributed with
It is located
/usr/share/nginx/html/50x.html
You should customize this error page for your own
site or edit the error_page directive in
the nginx configuration file
/etc/nginx/nginx.conf.Memcache 在PHP中的使用技巧
字体:[ ] 类型:转载 时间:
Memcache 在PHP中的使用
add($key, $value, $expiry); $key: 唯一标识, 用于区分写入的数据 $value: 要写入的数据 $expiry: 过期时间, 默认为永远有效 用途: 将数据写入到memcache中 get($key) $key: 通过写入时的$key获取对应的数据 用途: 获取memcache中的数据 replace($key, $value, $expiry) 该方法参数与add方法的参数相同 用途也很明显就是替换数据 delete($key, $time = 0) $key: 唯一标识 $time: 延迟时间 用途: 删除memcache中存储的数据 下面来看看具体用法: add($key, $value, $expiry); $key: 唯一标识, 用于区分写入的数据 $value: 要写入的数据 $expiry: 过期时间, 默认为永远有效 用途: 将数据写入到memcache中 get($key) $key: 通过写入时的$key获取对应的数据 用途: 获取memcache中的数据 replace($key, $value, $expiry) 该方法参数与add方法的参数相同 用途也很明显就是替换数据 delete($key, $time = 0) $key: 唯一标识 $time: 延迟时间 用途: 删除memcache中存储的数据 下面来看看具体用法: 代码
代码如下: &?php $m = new Memcache(); $m-&connect('localhost', 11211); $data = 'content'; //需要缓存的数据 $m-&add('mykey', $data);echo $m-&get('mykey'); // 输出 content $m-&replace('mykey', 'data'); //替换内容为dataecho $m-&get('mykey');//输出 data $m-&delete('mykey'); //删除echo $m-&get('mykey'); //输出 false 因为已经删掉了哦.. ?&
是不是很简单.. 在实际应用中,通常会把数据库查询的结果集保存到 memcached 中 下次访问时直接从 memcached 中获取,而不再做数据库查询操作,这样可以在很大程度上减轻数据库的负担。 通常会将 SQL 语句 md5() 之后的值作为唯一标识符 key。下边是一个利用 memcached 来缓存数据库查询结果集的示例 代码
代码如下: &?php //连接memcache $m = new Memcache(); $m-&connect('localhost', 11211); //连接数据库的我就不写了. $sql = 'SELECT * FROM users'; $key = md5($sql); //md5 SQL命令 作为 memcache的唯一标识符 $rows = $m-&get($key); //先重memcache获取数据 if (!$rows) { //如果$rows为false那么就是没有数据咯, 那么就写入数据 $res = mysql_query($sql); $rows = array(); while ($row = mysql_fetch_array($res)) { $rows[] = $ } $m-&add($key, $rows); //这里写入重数据库中获取的数据, 可以设置缓存时间, 具体时间设置多少, 根据自己需求吧. } var_dump($rows); //打印出数据 //上面第一次运行程序时, 因为还没有缓存数据, 所以会读取一次数据库, 当再次访问程序时, 就直接重memcache获取了. ?&
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具The page is temporarily unavailable
nginx error!
The page you are looking for is temporarily unavailable.
Please try again later.
Website Administrator
Something has triggered an error on your
This is the default error page for
nginx that is distributed with
It is located
/usr/share/nginx/html/50x.html
You should customize this error page for your own
site or edit the error_page directive in
the nginx configuration file
/etc/nginx/nginx.conf.The page is temporarily unavailable
nginx error!
The page you are looking for is temporarily unavailable.
Please try again later.
Website Administrator
Something has triggered an error on your
This is the default error page for
nginx that is distributed with
It is located
/usr/share/nginx/html/50x.html
You should customize this error page for your own
site or edit the error_page directive in
the nginx configuration file
/etc/nginx/nginx.conf.The page is temporarily unavailable
nginx error!
The page you are looking for is temporarily unavailable.
Please try again later.
Website Administrator
Something has triggered an error on your
This is the default error page for
nginx that is distributed with
It is located
/usr/share/nginx/html/50x.html
You should customize this error page for your own
site or edit the error_page directive in
the nginx configuration file
/etc/nginx/nginx.conf.

我要回帖

更多关于 php memcache操作类 的文章

 

随机推荐