php 缓存类的简单示例

一个简单的PHP缓存类感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!

<?PHP
/**
 * 一个简单的PHP缓存类
 *
 * @param 
 * @author 网: jb51.cc
 * Class Cache
 * @author Koen Ekelschot
 * @license WTFPL
 */
class Cache {
 private $cachedFile;
 public function __construct($identifier) {
  $this->cachedFile = ROOT.DS.'tmp'.DS.'cache'.DS.md5($identifier);
 }
 public function cacheExists($maxAge) {
  if (file_exists($this->cachedFile) && !is_dir($this->cachedFile)) {
   if (filemtime($this->cachedFile) + $maxAge > time()) {
	return true;
   } else {
	$this->invalidateCache();
   }
  }
  return false;
 }
 public function getCachedcopy() {
  $contents = file_get_contents($this->cachedFile);
  return unserialize(base64_decode($contents));
 }
 public function getCachedFilename() {
  return str_replace(ROOT,'',$this->cachedFile);
 }
 public function cacheResult($result) {
  if (file_exists($this->cachedFile) && !is_dir($this->cachedFile)) {
   $this->invalidateCache();
  }
  $base64 = base64_encode(serialize($result));
  file_put_contents($this->cachedFile,$base64);
 }
 private function invalidateCache() {
  unlink($this->cachedFile);
 }
}
            
/***   来自编程之家 jb51.cc(jb51.cc)   ***/

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...