memcached php封装类,PHP Memcached + APC + 文件缓存封装的简单示例

这篇文章主要为大家详细介绍了PHP Memcached + APC + 文件缓存封装的简单示例,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随512笔记的小编小韵来看看吧!使用方法:

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

代码如下:

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 + 文件缓存封装的简单示例的内容就先介绍到这里,更多相关文章的可以留意512笔记的其他信息。

关键词:

您可能感兴趣的文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值