一,memcache 与 memecached的区别与联系:
memcache扩展是完全在PHP框架内开发的,
memecached扩展是使用libmemcached的。
从手册上看,memcached 会比 memcache 多几个方法,使用方式上都差不多
二,php使用 memcache
<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
echo "Memcached's version: " . $memcache->getVersion() . "<br />";
$memcache_key = "xxxxxxxxxx"; // 随便命名,与其他缓存不要重复
// 根据键,从缓存服务器中获取它的值
$cache_result = $memcache->get($memcache_key);
// 没有再取值缓存(数组,字符串都行)
if(!$cache_result){
$result = array("id"=>'1',"name"=>'小明');
$memcache->set($memcache_key, $result, MEMCACHE_COMPRESSED, 1200);
}
三,php使用 memcached
<?php
require_once('memcached-client.php');
$options = array(
'servers' => array('192.168.1.1:11211'),
'debug' => true,
'compress_threshold' => 10240,
'persistant' => false
);
$mc = new memcached($options);
$key = 'mykey';
$mc->add($key, 'some random strings');
$val = $mc->get($key);
$mc->replace($key, array('some'=>'haha', 'array'=>'xxx'));
$val = $mc->get($key);
$mc->delete($key);
$val = $mc->get($key);
echo "n".str_pad('$mc->delete() ', 60, '_')."n";
var_dump($val);
四, memcache定时
$memcahe = OpMemcache::getInstance();
$memcaheKey = "DECORATION_SIGN_TAG_TIME";
$ref = $memcahe->get($memcaheKey);
if ($ref == false || $ref + 3600 < time()) {
$ref = time();
$memcahe->set($memcaheKey, $ref, 0, 3600);
$refresh = true;
}
```