PHP memcache 的使用常规方法!

<?php 
# Memcache连接
$memcache = new Memcache;
$memcache->connect("127.0.0.1", 11211) or die ("Could not connect");

# Memcache版本
$version = $memcache->getVersion();
echo "Memcached Server version: " . $version . "<br />";

# Memcache保存数据
$memcache->set('key1', 'This is first value', 0, 60);
$val = $memcache->get('key1');
echo "Get key1 value: " . $val ."<br />";

# Memcache替换数据
$memcache->replace('key1', 'This is replace value', 0, 60);
$val = $memcache->get('key1');
echo "Get key1 value: " . $val . "<br />";

# Memcache保存数组
$arr = array('aaa', 'bbb', 'ccc', 'ddd');
$memcache->set('key2', $arr, 0, 60);
$val2 = $memcache->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "<br />";

# Memcache删除数据(当前)
$memcache->delete('key1');
$val = $memcache->get('key1');
echo "Get key1 value: " . $val . "<br />";

# Memcache清除数据(所有)
$memcache->flush();
$val2 = $memcache->get('key2');
echo "Get key2 value: ";
print_r($val2);
echo "<br />";

# Memcache关闭连接
$memcache->close();
?>




你可能感兴趣的:(PHP memcache 的使用常规方法!)