首页 php教程 php手册 PHP缓存技术实现

PHP缓存技术实现

Jun 21, 2016 am 08:52 AM
cache function key public return

  发个PHP缓存实现,实现了apc和文件缓存,继承Cache_Abstract即可实现调用第三方的缓存工具。

  参考shindig的缓存类和apc。

  Php代码

  

  class CacheException extends Exception {}

  /**

  * 缓存抽象类

  */

  abstract class Cache_Abstract {

  /**

  * 读缓存变量

  *

  * @param string $key 缓存下标

  * @return mixed

  */

  abstract public function fetch($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();

  /**

  * 锁定缓存变量

  *

  * @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;

  }

  $tries = 10;

  $count = 0;

  do {

  usleep(200);

  $count ++;

  } while ($count isLocked($key)); // 最多做十次睡眠等待解锁,超时则跳过并解锁

  $this->isLocked($key) && $this->unlock($key);

  return $this;

  }

  }

  /**

  * APC扩展缓存实现

  *

  *

  * @category Mjie

  * @package Cache

  * @author 流水孟春

  * @copyright Copyright (c) 2008- (at)qq.com>

  * @license New BSD License

  * @version $Id: Cache/Apc.php 版本号 2010-04-18 23:02 cmpan $

  */

  class Cache_Apc extends Cache_Abstract {

  protected $_prefix = 'cache.mjie.net';

  public function __construct() {

  if (!function_exists('apc_cache_info')) {

  throw new CacheException('apc extension didn\'t installed');

  }

  }

  /**

  * 保存缓存变量

  *

  * @param string $key

  * @param mixed $value

  * @return bool

  */

  public function store($key, $value) {

  return apc_store($this->_storageKey($key), $value);

  }

  /**

  * 读取缓存

  *

  * @param string $key

  * @return mixed

  */

  public function fetch($key) {

  return apc_fetch($this->_storageKey($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;

  }

  /**

  * 缓存单元是否被锁定

  *

  * @param string $key

  * @return bool

  */

  public function isLocked($key) {

  if ((apc_fetch($this->_storageKey($key) . '.lock')) === false) {

  return false;

  }

  return true;

  }

  /**

  * 锁定缓存单元

  *

  * @param string $key

  * @return Cache_Apc

  */

  public function lock($key) {

  apc_store($this->_storageKey($key) . '.lock', '', 5);

  return $this;

  }

  /**

  * 缓存单元解锁

  *

  * @param string $key

  * @return Cache_Apc

  */

  public function unlock($key) {

  apc_delete($this->_storageKey($key) . '.lock');

  return $this;

  }

  /**

  * 完整缓存名

  *

  * @param string $key

  * @return string

  */

  private function _storageKey($key) {

  return $this->_prefix . '_' . $key;

  }

  }

  /**

  * 文件缓存实现

  *

  *

  * @category Mjie

  * @package Cache

  * @author 流水孟春

  * @copyright Copyright (c) 2008- (at)qq.com>

  * @license New BSD License

  * @version $Id: Cache/File.php 版本号 2010-04-18 16:46 cmpan $

  */

  class Cache_File extends Cache_Abstract {

  public $useSubdir = false;

  protected $_cachesDir = 'cache';

  public function __construct() {

  if (defined('DATA_DIR')) {

  $this->_setCacheDir(DATA_DIR . '/cache');

  }

  }

  /**

  * 获取缓存文件

  *

  * @param string $key

  * @return string

  */

  protected function _getCacheFile($key) {

  $subdir = $this->useSubdir ? substr($key, 0, 2) . '/' : '';

  return $this->_cachesDir . '/' . $subdir . $key . '.php';

  }

  /**

  * 读取缓存变量

  * 为防止信息泄露,缓存文件格式为php文件,并以""开头

  *

  * @param string $key 缓存下标

  * @return mixed

  */

  public function fetch($key) {

  $cacheFile = self::_getCacheFile($key);

  if (file_exists($cacheFile) && is_readable($cacheFile)) {

  // include 方式

  //return include $cacheFile;

  // 系列化方式

  return unserialize(@file_get_contents($cacheFile, false, NULL, 13));

  }

  return false;

  }

  /**

  * 缓存变量

  * 为防止信息泄露,缓存文件格式为php文件,并以""开头

  *

  * @param string $key 缓存变量下标

  * @param string $value 缓存变量的值

  * @return bool

  */

  public function store($key, $value) {

  $cacheFile = self::_getCacheFile($key);

  $cacheDir = dirname($cacheFile);

  if(!is_dir($cacheDir)) {

  if(!@mkdir($cacheDir, 0755, true)) {

  throw new CacheException("Could not make cache directory");

  }

  }

  // 用include方式

  //return @file_put_contents($cacheFile, '

  return @file_put_contents($cacheFile, '' . serialize($value));

  }

  /**

  * 删除缓存变量

  *

  * @param string $key 缓存下标

  * @return Cache_File

  */

  public function delete($key) {

  if(emptyempty($key)) {

  throw new CacheException("Missing argument 1 for Cache_File::delete()");

  }

  $cacheFile = self::_getCacheFile($key);

  if(!@unlink($cacheFile)) {

  throw new CacheException("Cache file could not be deleted");

  }

  return $this;

  }

  /**

  * 缓存单元是否已经锁定

  *

  * @param string $key

  * @return bool

  */

  public function isLocked($key) {

  $cacheFile = self::_getCacheFile($key);

  clearstatcache();

  return file_exists($cacheFile . '.lock');

  }

  /**

  * 锁定

  *

  * @param string $key

  * @return Cache_File

  */

  public function lock($key) {

  $cacheFile = self::_getCacheFile($key);

  $cacheDir = dirname($cacheFile);

  if(!is_dir($cacheDir)) {

  if(!@mkdir($cacheDir, 0755, true)) {

  if(!is_dir($cacheDir)) {

  throw new CacheException("Could not make cache directory");

  }

  }

  }

  // 设定缓存锁文件的访问和修改时间

  @touch($cacheFile . '.lock');

  return $this;

  }

  /**

  * 解锁

  *

  * @param string $key

  * @return Cache_File

  */

  public function unlock($key) {

  $cacheFile = self::_getCacheFile($key);

  @unlink($cacheFile . '.lock');

  return



本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

C语言return的用法详解 C语言return的用法详解 Oct 07, 2023 am 10:58 AM

C语言return的用法详解

SQL中的identity属性是什么意思? SQL中的identity属性是什么意思? Feb 19, 2024 am 11:24 AM

SQL中的identity属性是什么意思?

function是什么意思 function是什么意思 Aug 04, 2023 am 10:33 AM

function是什么意思

SpringBoot怎么监听redis Key变化事件 SpringBoot怎么监听redis Key变化事件 May 26, 2023 pm 01:55 PM

SpringBoot怎么监听redis Key变化事件

入职后,我才明白什么叫Cache 入职后,我才明白什么叫Cache Jul 31, 2023 pm 04:03 PM

入职后,我才明白什么叫Cache

Java中return和finally语句的执行顺序是怎样的? Java中return和finally语句的执行顺序是怎样的? Apr 25, 2023 pm 07:55 PM

Java中return和finally语句的执行顺序是怎样的?

iOS的developer版和public版有什么区别? iOS的developer版和public版有什么区别? Mar 01, 2024 pm 12:55 PM

iOS的developer版和public版有什么区别?

不可修补的 Yubico 二因素身份验证密钥漏洞破坏了大多数 Yubikey 5、安全密钥和 YubiHSM 2FA 设备的安全性 不可修补的 Yubico 二因素身份验证密钥漏洞破坏了大多数 Yubikey 5、安全密钥和 YubiHSM 2FA 设备的安全性 Sep 04, 2024 pm 06:32 PM

不可修补的 Yubico 二因素身份验证密钥漏洞破坏了大多数 Yubikey 5、安全密钥和 YubiHSM 2FA 设备的安全性

See all articles