PHP Memcached + APC + 文件缓存封装实现代码
2018-10-12 13:53
1196 查看
使用方法:
Memcached
文件缓存
下载: class_cache3.php
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);
}
}
?>
您可能感兴趣的文章:
相关文章推荐
- PHP Memcached + APC + 文件缓存封装实现代码
- PHP Memcached + APC + 文件缓存封装
- php中文件缓存实现程序代码
- php文件缓存技术实现代码
- php中操作memcached缓存进行增删改查数据的实现代码
- php中操作memcached缓存进行增删改查数据的实现代码
- php文件缓存技术实现代码
- php实现文件数据缓存实现代码
- php实现文件下载的一段代码
- 利用discuz实现PHP大文件上传应用实例代码
- 使用PHP获取网络文件的实现代码
- PHP 文件上传进度条的两种实现方法的代码
- PHP Memcached应用实现代码
- 用php实现的下载css文件中的图片的代码
- .Net/C# 封装磁盘目录文件搜索功能的工具类 (实现了与搜索相关的事件,以便插入客户处理代码)
- 同台服务器使用缓存APC效率高于Memcached的演示代码
- php download.php实现代码 跳转到下载文件(response.redirect)
- 如何用php代码清空squid缓存下的指定文件
- PHP APC扩展模块实现大文件上传
- PHP用代码实现文件下载