/** * 获取Redis适配器 * @return \Redis */ protected function getAdapter() { if ($this->adapter === NULL) { $this->adapter = CacheBase::getRedis(); } return $this->adapter; }
protected function __construct($driverConfig) { $this->driver = \HuiLib\Cache\CacheBase::create($driverConfig); if (!$this->driver instanceof \HuiLib\Cache\CacheBase) { throw new \HuiLib\Error\Exception('Session cache driver initialized failed'); } //session管理器 $this->manager = \HuiLib\Session\SessionManager::create(); $this->manager->setAdapter($this); }
/** * 解析应用配置ini文件 * @throws \HuiLib\Error\Exception */ private function parse() { if (!is_file($this->filePath)) { throw new \HuiLib\Error\Exception("Config file: {$this->filePath} Not exists!"); } $this->configSource = parse_ini_file($this->filePath, self::PARSE_SECTION); if (!is_array($this->configSource)) { throw new \HuiLib\Error\Exception("Config ini file parsed Exception!"); } //检测是否存在服务器环境标签 $allowEnvTag = Front::getInstance()->getBootstrap()->getAllowEnv(); $existSection = FALSE; foreach ($allowEnvTag as $envTag) { if (isset($this->configSource[$envTag])) { $existSection = TRUE; } } /** * 解析块元素 * * 1、存在[production]等标签,根据继承合并块 * 2、不包括环境标签的直接解析数组 */ if ($existSection) { $this->configFinal = $this->mergeSection(); if (isset($this->configFinal[APP_ENV])) { $this->configEnv =& $this->configFinal[APP_ENV]; } else { $this->configEnv = array(); } } else { $this->configFinal = $this->getSettingFromBlock($this->configSource); $this->configEnv =& $this->configFinal; } //缓存到缓存服务器 $cache = array(); $cache['data'] = $this->configFinal; $cache['section'] = $existSection; $cache['stamp'] = filemtime($this->filePath); $this->lastUpate = $cache['stamp']; $this->cacheAdapter->add($this->getCacheKey(), $cache); }
/** * 测试通过静态函数获取 */ private function testStaticInit() { $cache = \HuiLib\Cache\CacheBase::getDefault(Front::getInstance()->getAppConfig()); echo $cache->toString(); $cache->add('hanhui2', date('Y-m-d H:i:s')); echo $cache->get('hanhui2'); //测试数组 $cache->replace('array', Front::getInstance()->getAppConfig()->getByKey('cache.memcache')); \HuiLib\Helper\Debug::out($cache->get('array')); $cache->add('count', 0); $cache->increase('count'); echo $cache->get('count'); $cache = \HuiLib\Cache\CacheBase::getRedis(Front::getInstance()->getAppConfig()); echo $cache->toString(); $cache = \HuiLib\Cache\CacheBase::getMemcache(Front::getInstance()->getAppConfig()); echo $cache->toString(); $cache = \HuiLib\Cache\CacheBase::getApc(Front::getInstance()->getAppConfig()); echo $cache->toString(); $cache = \HuiLib\Cache\CacheBase::getFile(Front::getInstance()->getAppConfig()); echo $cache->toString(); }
private function __construct() { $this->redis = \HuiLib\Cache\CacheBase::getRedis(); }
<?php /** * HuiLib Cache库操作指南 * * @since 2013/11/10 * * Cache支持储存后端:Apc, Memcache, Redis * * 适配器接口: * add($key, $value):强制添加一个缓存 * addnx($key, $value):添加一个缓存,如果已经存在返回false * delete($key):删除一个键 * get($key):获取一个键的值 * increase($key, $value=1):给指定键增加一个值 * decrease($key, $value=1):给缓存键减少一个值 * * 2013/11/10 取消支持File储存,因为本Cache模块主要定位KV储存 * File一般是直接写入,跟上面的接口格格不入。如果需要,可以直接file_put_contents。 */ //快速获取一个Apc缓存实例 $cache = \HuiLib\Cache\CacheBase::getApc(); //快速获取一个Memcache缓存实例 $cache = \HuiLib\Cache\CacheBase::getMemcache(); //快速获取一个Redis缓存实例 $cache = \HuiLib\Cache\CacheBase::getRedis();