For each data value being cached, FileCache will store it in a separate file. The cache files are placed under [[cachePath]]. FileCache will perform garbage collection automatically to remove expired cache files. Please refer to Cache for common cache operations that are supported by FileCache.
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends Cache
Example #1
0
 public function testInvalidate()
 {
     $cache = new FileCache(['cachePath' => '@yiiunit/runtime/cache']);
     // single tag test
     $cache->set('a1', 11, 0, new TagDependency(['tags' => 't1']));
     $cache->set('a2', 12, 0, new TagDependency(['tags' => 't1']));
     $cache->set('b1', 21, 0, new TagDependency(['tags' => 't2']));
     $cache->set('b2', 22, 0, new TagDependency(['tags' => 't2']));
     $this->assertEquals(11, $cache->get('a1'));
     $this->assertEquals(12, $cache->get('a2'));
     $this->assertEquals(21, $cache->get('b1'));
     $this->assertEquals(22, $cache->get('b2'));
     TagDependency::invalidate($cache, 't1');
     $this->assertFalse($cache->get('a1'));
     $this->assertFalse($cache->get('a2'));
     $this->assertEquals(21, $cache->get('b1'));
     $this->assertEquals(22, $cache->get('b2'));
     TagDependency::invalidate($cache, 't2');
     $this->assertFalse($cache->get('a1'));
     $this->assertFalse($cache->get('a2'));
     $this->assertFalse($cache->get('b1'));
     $this->assertFalse($cache->get('b2'));
     // multiple tag test
     $cache->set('a1', 11, 0, new TagDependency(['tags' => ['t1', 't2']]));
     $cache->set('a2', 12, 0, new TagDependency(['tags' => 't1']));
     $cache->set('b1', 21, 0, new TagDependency(['tags' => ['t1', 't2']]));
     $cache->set('b2', 22, 0, new TagDependency(['tags' => 't2']));
     $this->assertEquals(11, $cache->get('a1'));
     $this->assertEquals(12, $cache->get('a2'));
     $this->assertEquals(21, $cache->get('b1'));
     $this->assertEquals(22, $cache->get('b2'));
     TagDependency::invalidate($cache, 't1');
     $this->assertFalse($cache->get('a1'));
     $this->assertFalse($cache->get('a2'));
     $this->assertFalse($cache->get('b1'));
     $this->assertEquals(22, $cache->get('b2'));
     TagDependency::invalidate($cache, 't2');
     $this->assertFalse($cache->get('a1'));
     $this->assertFalse($cache->get('a2'));
     $this->assertFalse($cache->get('b1'));
     $this->assertFalse($cache->get('b2'));
     $cache->set('a1', 11, 0, new TagDependency(['tags' => ['t1', 't2']]));
     $cache->set('a2', 12, 0, new TagDependency(['tags' => 't1']));
     $cache->set('b1', 21, 0, new TagDependency(['tags' => ['t1', 't2']]));
     $cache->set('b2', 22, 0, new TagDependency(['tags' => 't2']));
     $this->assertEquals(11, $cache->get('a1'));
     $this->assertEquals(12, $cache->get('a2'));
     $this->assertEquals(21, $cache->get('b1'));
     $this->assertEquals(22, $cache->get('b2'));
     TagDependency::invalidate($cache, ['t1', 't2']);
     $this->assertFalse($cache->get('a1'));
     $this->assertFalse($cache->get('a2'));
     $this->assertFalse($cache->get('b1'));
     $this->assertFalse($cache->get('b2'));
 }
Example #2
0
 /**
  * Initialization of cache component.
  * @throws InvalidConfigException if cacheComponent is incorrect.
  */
 protected function initCacheComponent()
 {
     if ($this->cacheComponent === null || is_array($this->cacheComponent)) {
         $this->cacheComponent = Yii::createObject(array_merge(['class' => FileCache::className(), 'cachePath' => rtrim($this->compiledPath, '\\/') . '/cache', 'fileMode' => $this->fileMode, 'dirMode' => $this->mkDirMode], $this->cacheComponent ?: []));
     } elseif (is_string($this->cacheComponent)) {
         $this->cacheComponent = Yii::$app->get($this->cacheComponent);
     }
     if ($this->cacheComponent !== false && !$this->cacheComponent instanceof Cache) {
         throw new InvalidConfigException('Incorrect value of ' . __CLASS__ . '::$cacheComponent param. Calculated value of this param must be an instance of ' . Cache::className() . '.');
     }
 }
 /**
  * Rewrites the configuration file
  */
 public static function rewriteConfiguration()
 {
     // Get Current Configuration
     $config = Configuration::get();
     // Add Application Name to Configuration
     $config['name'] = Yii::$app->config->get(Enum::APP_NAME);
     // Add Caching
     $cacheClass = Yii::$app->config->get(Enum::CACHE_CLASS);
     if (!$cacheClass) {
         $cacheClass = FileCache::className();
     }
     $config['components']['cache'] = ['class' => $cacheClass];
     // Add Auth Clients
     $config['components']['authClientCollection']['class'] = 'yii\\authclient\\Collection';
     Configuration::set($config);
 }
Example #4
0
 protected function setValue($key, $value, $duration)
 {
     $value = base64_encode($value);
     parent::setValue($key, $value, $duration);
 }
?>
		</div>

		<div class="form-group">
			<?php 
echo $form->field($model, 'appFrontendTheme')->dropDownList($themes, ['class' => 'form-control', 'options' => [Yii::$app->config->get(Enum::APP_FRONTEND_THEME, 'readable') => ['selected ' => TRUE]]]);
?>
		</div>

		<hr/>

		<h4>Cache Setting</h4>

		<div class="form-group">
			<?php 
echo $form->field($model, 'cacheClass')->dropDownList([FileCache::className() => 'File Cache', DbCache::className() => 'Db Cache'], ['class' => 'form-control', 'options' => [Yii::$app->config->get(Enum::CACHE_CLASS, FileCache::className()) => ['selected ' => TRUE]]]);
?>
		</div>

		<hr/>

		<h4>Introduction Tour</h4>

		<div class="form-group">
			<div class="checkbox">
				<?php 
echo $form->field($model, 'appTour')->checkbox();
?>
			</div>
		</div>
Example #6
0
    $collectorsConfigPath = CANIS_APP_CONFIG_PATH . DIRECTORY_SEPARATOR . 'modules.php';
    if (file_exists($collectorsConfigPath)) {
        $base['components']['collectors'] = (include $collectorsConfigPath);
    } else {
        $base['components']['collectors'] = (include 'collectors.php');
    }
}
if (!isset($base['components']['cache'])) {
    if (isset($base['components']['redis'])) {
        $base['components']['cache'] = ['class' => RedisCache::className()];
    } else {
        $base['components']['cache'] = ['class' => DummyCache::className()];
    }
}
if (!isset($base['components']['fileCache'])) {
    $base['components']['fileCache'] = ['class' => FileCache::className()];
}
if (!isset($base['components']['gk'])) {
    $base['components']['gk'] = ['class' => Gatekeeper::className(), 'authority' => ['type' => 'Individual']];
}
if (class_exists('cascade\\models\\User')) {
    $base['components']['user'] = ['class' => 'canis\\web\\User', 'enableAutoLogin' => false, 'identityClass' => 'cascade\\server\\models\\User', 'loginUrl' => ['/user/login']];
}
if (!isset($base['components']['fileStorage'])) {
    $base['components']['fileStorage'] = ['class' => FileStorage::className()];
}
if (!isset($base['components']['view'])) {
    $base['components']['view'] = ['class' => View::className()];
}
if (!isset($base['components']['response'])) {
    $base['components']['response'] = ['class' => Response::className()];
Example #7
0
 private function getCache($key)
 {
     $cache = new FileCache();
     $data = $cache[$key];
     if (!$data) {
         return false;
     }
     $jsonData = @json_decode($data, true);
     if ($jsonData['expire_time'] < time()) {
         $cache->delete($key);
         return false;
     }
     return $jsonData['value'];
 }
Example #8
0
 protected function setValue($key, $value, $duration)
 {
     $value = bzcompress($value);
     parent::setValue($key, $value, $duration);
 }