PHP Memcached + APC + 文件缓存封装实现代码
(编辑:jimmy 日期: 2024/11/12 浏览:2)
使用方法:
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;
}
function store($key, $data, $ttl) {
return $this->connection->set($key, $data, 0, $ttl);
}
function fetch($key) {
return $this->connection->get($key);
}
function delete($key) {
return $this->connection->delete($key);
}
function addServer($host, $port = 11211, $weight = 10) {
$this->connection->addServer($host, $port, true, $weight);
}
}
class Cache_File extends Cache_Abstract {
function store($key, $data, $ttl) {
$h = fopen($this->getFileName($key), 'a+');
if (!$h)
throw new Exception('Could not write to cache');
flock($h, LOCK_EX);
fseek($h, 0);
ftruncate($h, 0);
$data = serialize(array(time() + $ttl, $data));
if (fwrite($h, $data) === false) {
throw new Exception('Could not write to cache');
}
fclose($h);
}
function fetch($key) {
$filename = $this->getFileName($key);
if (!file_exists($filename))
return false;
$h = fopen($filename, 'r');
if (!$h)
return false;
flock($h, LOCK_SH);
$data = file_get_contents($filename);
fclose($h);
$data = @ unserialize($data);
if (!$data) {
unlink($filename);
return false;
}
if (time() > $data[0]) {
unlink($filename);
return false;
}
return $data[1];
}
function delete($key) {
$filename = $this->getFileName($key);
if (file_exists($filename)) {
return unlink($filename);
}
else {
return false;
}
}
private function getFileName($key) {
return '/tmp/s_cache' . md5($key);
}
}
?>
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?