使用方法:
Memcached
<BR>$cache = new Cache_MemCache(); <BR>$cache->addServer('www1'); <BR>$cache->addServer('www2',11211,20); // this server has double the memory, and gets double the weight <BR>$cache->addServer('www3',11211); <BR>// Store some data in the cache for 10 minutes <BR>$cache->store('my_key','foobar',600); <BR>// Get it out of the cache again <BR>echo($cache->fetch('my_key')); <BR>
文件缓存
<BR>$cache = new Cache_File(); <BR>$key = 'getUsers:selectAll'; <BR>// check if the data is not in the cache already <BR>if (!$data = $cache->fetch($key)) { <BR>// assuming there is a database connection <BR>$result = mysql_query("SELECT * FROM users"); <BR>$data = array(); <BR>// fetching all the data and putting it in an array <BR>while($row = mysql_fetch_assoc($result)) { $data[] = $row; } <BR>// Storing the data in the cache for 10 minutes <BR>$cache->store($key,$data,600); <BR>} <BR>
下载: class_cache3.php
<?php <br><br>abstract class Cache_Abstract { <BR>abstract function fetch($key); <BR>abstract function store($key, $data, $ttl); <BR>abstract function delete($key); <BR>} <br><br>class Cache_APC extends Cache_Abstract { <br><br>function fetch($key) { <BR>return apc_fetch($key); <BR>} <br><br>function store($key, $data, $ttl) { <BR>return apc_store($key, $data, $ttl); <BR>} <br><br>function delete($key) { <BR>return apc_delete($key); <BR>} <br><br>} <br><br>class Cache_MemCache extends Cache_Abstract { <BR>public $connection; <br><br>function __construct() { <BR>$this->connection = new MemCache; <BR>} <br><br>function store($key, $data, $ttl) { <BR>return $this->connection->set($key, $data, 0, $ttl); <BR>} <br><br>function fetch($key) { <BR>return $this->connection->get($key); <BR>} <br><br>function delete($key) { <BR>return $this->connection->delete($key); <BR>} <br><br>function addServer($host, $port = 11211, $weight = 10) { <BR>$this->connection->addServer($host, $port, true, $weight); <BR>} <br><br>} <br><br>class Cache_File extends Cache_Abstract { <br><br>function store($key, $data, $ttl) { <BR>$h = fopen($this->getFileName($key), 'a+'); <BR>if (!$h) <BR>throw new Exception('Could not write to cache'); <BR>flock($h, LOCK_EX); <BR>fseek($h, 0); <BR>ftruncate($h, 0); <BR>$data = serialize(array(time() + $ttl, $data)); <BR>if (fwrite($h, $data) === false) { <BR>throw new Exception('Could not write to cache'); <BR>} <BR>fclose($h); <BR>} <br><br>function fetch($key) { <BR>$filename = $this->getFileName($key); <BR>if (!file_exists($filename)) <BR>return false; <BR>$h = fopen($filename, 'r'); <BR>if (!$h) <BR>return false; <BR>flock($h, LOCK_SH); <BR>$data = file_get_contents($filename); <BR>fclose($h); <BR>$data = @ unserialize($data); <BR>if (!$data) { <BR>unlink($filename); <BR>return false; <BR>} <BR>if (time() > $data[0]) { <BR>unlink($filename); <BR>return false; <BR>} <BR>return $data[1]; <BR>} <br><br>function delete($key) { <BR>$filename = $this->getFileName($key); <BR>if (file_exists($filename)) { <BR>return unlink($filename); <BR>} <BR>else { <BR>return false; <BR>} <BR>} <br><br>private function getFileName($<b style="color:transparent">来&源gao@dai!ma.com搞$代^码%网</b><img>搞gaodaima代码</img>key) { <BR>return '/tmp/s_cache' . md5($key); <BR>} <br><br>} <BR>?><BR>