PHP   发布时间:2019-11-18  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP Memcached + APC + 文件缓存封装实现代码大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

使用方法: @H_689_1@memcached
<div class="codetitle"><a style="cursOR: pointer" data="41350" class="copybut" id="copybut41350" onclick="doCopy('code41350')"> 代码如下:@H_616_7@<div class="codebody" id="code41350">
$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'));
@H_616_7@
文件缓存
<div class="codetitle"><a style="cursOR: pointer" data="33064" class="copybut" id="copybut33064" onclick="doCopy('code33064')"> 代码如下:@H_616_7@<div class="codebody" id="code33064">
$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);
}
@H_616_7@
下载: class_cache3.php
<div class="codetitle"><a style="cursOR: pointer" data="11038" class="copybut" id="copybut11038" onclick="doCopy('code11038')"> 代码如下:@H_616_7@<div class="codebody" id="code11038">
<?php abstract class Cache_Abstract {
abstract function fetch($key);
abstract function store($key,$ttl);
abstract function delete($key);
} class Cache_APC extends Cache_Abstract { function fetch($key) {
return apc_fetch($key);
} function store($key,$ttl) {
return apc_store($key,$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,$ttl) {
return $this->connection->set($key,$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,$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);
} }
?>
@H_616_7@

大佬总结

以上是大佬教程为你收集整理的PHP Memcached + APC + 文件缓存封装实现代码全部内容,希望文章能够帮你解决PHP Memcached + APC + 文件缓存封装实现代码所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: