一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

PHP Memcached + APC + 文件緩存封裝實現代碼

使用方法:
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技術PHP Memcached + APC + 文件緩存封裝實現代碼,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 怡红院一区二区在线观看 | 四虎国产精品一区二区 | 久草色在线 | 激情五月婷婷色 | 国产自产一c区 | 成人性a激情免费视频 | 久草一级片 | 亚州视频一区 | 亚洲美女在线视频 | 41sao.can在线观看国产 | 伊人影院在线观看视频 | 美女黄色免费网站 | 国产精品一区二区在线播放 | 毛片区| 国产成人亚洲精品乱码在线观看 | 色综合久久六月婷婷中文字幕 | 久久综合九色综合97伊人麻豆 | 黄色片视频网站 | 亚洲午夜大片 | 在线观看免费精品国产 | 国产在线精品一区二区三区 | selaoban在线视频免费精品 | 国内成人精品亚洲日本语音 | 久久国产乱子伦精品免费一 | 亚洲欧洲无码一区二区三区 | 91福利视频免费 | 国产福利最新手机在线观看 | 国产二级毛片 | 大量国产激情视频在线观看 | 中国毛片免费观看 | 女人被免费视频网站 | 精品成人免费一区二区在线播放 | 三级小说第一页 | 国产精品女同一区二区久久夜 | 久久国产成人精品国产成人亚洲 | 亚洲国产成人综合精品2020 | 亚洲精品成人a在线观看 | 黑人巨大videos极度另类 | 亚洲国产成人久久一区www | 久久精品视频网站 | 美女脱了内裤打开腿让人桶网站o |