天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

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 + 文件緩存封裝實現代碼,轉載需保留來源!

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

主站蜘蛛池模板: 国产亚洲人成在线视频 | 国产精品久久久久久免费字体 | 亚洲一区在线观看视频 | 果冻传媒色AV国产播放 | 久久视频在线视频观品15 | 国产精品久久久久一区二区三区 | 中文字幕人成人乱码亚洲AV | 九九精品视频在线播放 | 蜜桃麻豆WWW久久囤产精品免费 | 国产主播福利一区二区 | 国产曰批试看免费视频播放免费 | 久久国产加勒比精品无码 | 亚洲熟女乱色一区二区三区 | 长泽梓黑人初解禁bdd07 | 亲嘴扒胸摸屁股视频免费网站 | 国产亚洲精品久久久999密臂 | 一个色综合久久 | 被老总按在办公桌吸奶头 | 综合精品欧美日韩国产在线 | 99C视频色欲在线 | 亚洲中文字幕手机版 | 亚洲中文字幕乱码熟女在线 | 中文字幕亚洲男人的天堂网络 | 97视频国产 | 白丝女仆被强扒内裤 | 狠狠色香婷婷久久亚洲精品 | 噜妇插内射精品 | 爱看吧孕妇网 | 手机看片成人 | 国产成人精品综合在线观看 | 欧美乱妇15p图 | 久久99re2在线视频精品 | 欧美成人无码视频午夜福利 | 国产亚洲精品久久综合阿香 | 日本内射精品一区二区视频 | 乳巨揉みま痴汉电车中文字幕动漫 | 香港日本三级亚洲三级 | 一级做a爰片久久毛片苍井优 | 台湾果冻传媒在线观看 | 国产精品观看视频免费完整版 | 国产三级精品三级男人的天堂 |