PHP Memcached + APC + 文件缓存封装实现代码

yipeiwu_com5年前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 echo, print, print_r, sprintf, var_dump, var_expor的使用区别

/*******echo********/echo— 输出一个或多个字符串描述echo ( string arg1 [, string ...] )echo()实际上不是一个函数(它是一...

PHP学习笔记(二) 了解PHP的基本语法以及目录结构

PHP学习笔记(二) 了解PHP的基本语法以及目录结构

通过这节课, 1.了解AppServ的目录结构 2.我们可以了解PHP的基本语法结构 1 我的AppServ安装目录是E盘: ①运用命令行的方式去操作apache服务器 apache服...

php 代码优化的42条建议 推荐

1.如果一个方法可静态化,就对它做静态声明。速率可提升至4倍。 2.echo 比 print 快。 3.使用echo的多重参数(译注:指用逗号而不是句点)代替字符串连接。 4.在执行fo...

php获取ip的三个属性区别介绍(HTTP_X_FORWARDED_FOR,HTTP_VIA,REMOTE_ADDR)

一、没有使用代理服务器的情况: REMOTE_ADDR = 您的 IP HTTP_VIA = 没数值或不显示 HTTP_X_FORWARDED_FOR = 没数值或不显示 二、使用透明代...

PHP 图片合成、仿微信群头像的方法示例

本文实例讲述了PHP 图片合成、仿微信群头像的方法。分享给大家供大家参考,具体如下: 参考文章: 作者:凯歌~,php图片合成方法(多张图片合成一张)https://www.jb51.n...