动用办法:
Memcached

复制代码 代码如下:

复制代码 代码如下:

class CacheException extends Exception {}
/**
* 缓存抽象类
*/
abstract class Cache_Abstract {
/**
* 读缓存变量
*
* @param string $key 缓存下标
* @return mixed
*/
abstract public function fetch($key);

$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’));

/**
* 缓存变量
*
* @param string $key 缓存变量下标
* @param string $value 缓存变量的值
* @return bool
*/
abstract public function store($key, $value);

文件缓存

/**
* 删除缓存变量
*
* @param string $key 缓存下标
* @return Cache_Abstract
*/
abstract public function delete($key);

复制代码 代码如下:

/**
* 清(删)除具有缓存
*
* @return Cache_Abstract
*/
abstract public function clear();

$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);
}

/**
* 锁定缓存变量
*
* @param string $key 缓存下标
* @return Cache_Abstract
*/
abstract public function lock($key);
/**
* 缓存变量解锁
*
* @param string $key 缓存下标
* @return Cache_Abstract
*/
abstract public function unlock($key);
/**
* 取得缓存变量是还是不是被锁定
*
* @param string $key 缓存下标
* @return bool
*/
abstract public function isLocked($key);
/**
* 确定保障不是锁定状态
* 最多做$tries次睡眠等待解锁,超时则跳过并解锁
*
* @param string $key 缓存下标
*/
public function checkLock($key) {
if (!$this->isLocked($key)) {
return $this;
}

下载: class_cache3.php

$tries = 10;
$count = 0;
do {
usleep(200);
$count ++;
} while ($count <= $tries && $this->isLocked($key)); //
最多做13回睡眠等待解锁,超时则跳过并解锁
$this->isLocked($key) && $this->unlock($key);

复制代码 代码如下:

return $this;
}
}

<?php

/**
* APC扩张缓存落成
*
*
* @category Mjie
* @package Cache
* @author 流水菊序
* @copyright Copyright (c) 2008- <cmpan(at)qq.com>
* @license New BSD License
* @version $Id: Cache/Apc.php 版本号 2010-04-18 23:02 cmpan $
*/
class Cache_Apc extends Cache_Abstract {

abstract class Cache_Abstract {
abstract function fetch($key);
abstract function store($key, $data, $ttl);
abstract function delete($key);
}

protected $_prefix = ‘cache.mjie.net’;

class Cache_APC extends Cache_Abstract {

public function __construct() {
if (!function_exists(‘apc_cache_info’)) {
throw new CacheException(‘apc extension didn\’t installed’);
}
}

function fetch($key) {
return apc_fetch($key);
}

/**
* 保存缓存变量
*
* @param string $key
* @param mixed $value
* @return bool
*/
public function store($key, $value) {
return apc_store($this->_storageKey($key), $value);
}

function store($key, $data, $ttl) {
return apc_store($key, $data, $ttl);
}

/**
* 读取缓存
*
* @param string $key
* @return mixed
*/
public function fetch($key) {
return apc_fetch($this->_storageKey($key));
}

function delete($key) {
return apc_delete($key);
}

/**
* 清除缓存
*
* @return Cache_Apc
*/
public function clear() {
apc_clear_cache();
return $this;
}

}

/**
* 删除缓存单元
*
* @return Cache_Apc
*/
public function delete($key) {
apc_delete($this->_storageKey($key));
return $this;
}

class Cache_MemCache extends Cache_Abstract {
public $connection;

/**
* 缓存单元是不是被锁定
*
* @param string $key
* @return bool
*/
public function isLocked($key) {
if ((apc_fetch($this->_storageKey($key) . ‘.lock’)) === false) {
return false;
}

function __construct() {
$this->connection = new MemCache;
}

return true;
}

function store($key, $data, $ttl) {
return $this->connection->set($key, $data, 0, $ttl);
}

/**
* 锁定缓存单元
*
* @param string $key
* @return Cache_Apc
*/
public function lock($key) {
apc_store($this->_storageKey($key) . ‘.lock’, ”, 5);
return $this;
}

网站地图xml地图