使用方法: Memcached 复制代码 代码如下: $cache = new Cache_MemCache(); $cache->addServer("www1"); $cache->addServer("www2",11211,20); // this server has double the memory, and gets double the weight $cache->addServer("www3",11211); // Store some data in the cache for 10 minutes $cache->store("my_key","foobar",600); // Get it out of the cache again echo($cache->fetch("my_key"));
文件缓存 复制代码 代码如下: $cache = new Cache_File(); $key = "getUsers:selectAll"; // check if the data is not in the cache already if (!$data = $cache->fetch($key)) { // assuming there is a database connection $result = mysql_query("SELECT * FROM users"); $data = array(); // fetching all the data and putting it in an array while($row = mysql_fetch_assoc($result)) { $data[] = $row; } // Storing the data in the cache for 10 minutes $cache->store($key,$data,600); }
下载: class_cache3.php 复制代码 代码如下: <?php
abstract class Cache_Abstract { abstract function fetch($key); abstract function store($key, $data, $ttl); abstract function delete($key); }
class Cache_APC extends Cache_Abstract {
function fetch($key) { return apc_fetch($key); }
function store($key, $data, $ttl) { return apc_store($key, $data, $ttl); }
function delete($key) { return apc_delete($key); }
}
class Cache_MemCache extends Cache_Abstract { public $connection;
function __construct() { $this->connection = new MemCache; }