Example #1
0
 /**
  * Calculates the cache identifier for read and write operations.
  *
  * @param CacheKey $cacheKey The applied cache key.
  *
  * @return string The cache identifier with respect for the given cache key (simple and advanced).
  *
  * @author Christian Achatz
  * @version
  * Version 0.1, 08.01.2013<br />
  */
 protected function getCacheIdentifier(CacheKey $cacheKey)
 {
     $identifier = $this->getConfigAttribute('Namespace');
     $identifier .= self::CACHE_KEY_DELIMITER . $cacheKey->getKey();
     if ($cacheKey instanceof AdvancedCacheKey) {
         $identifier .= self::CACHE_KEY_DELIMITER . $cacheKey->getSubKey();
     }
     return $identifier;
 }
 public function clear(CacheKey $cacheKey = null)
 {
     $baseFolder = $this->getConfigAttribute('BaseFolder');
     $namespace = str_replace('\\', '/', $this->getConfigAttribute('Namespace'));
     // in case we do not have a cache key, remove the entire cache
     if ($cacheKey === null) {
         try {
             FilesystemManager::deleteFolder($baseFolder . '/' . $namespace, true);
             return true;
         } catch (FileException $e) {
             return false;
             // indicate, that nothing was to delete (e.g. cache not active or empty)
         }
     }
     /* @var $cacheKey AdvancedCacheKey */
     $key = $cacheKey->getKey();
     $subKey = $cacheKey->getSubKey();
     if ($key == null && $subKey == null) {
         FilesystemManager::deleteFolder($baseFolder . '/' . $namespace, true);
     } elseif ($key != null && $subKey == null) {
         // in case we have the cache key only, delete the entire structure
         // including all sub cache entries
         $key = md5($key);
         $folder = $baseFolder . '/' . $namespace . '/' . substr($key, 0, 2) . '/' . $key;
         FilesystemManager::deleteFolder($folder, true);
         return true;
     } else {
         // in case we have both cache key and cache sub key, delete the local
         // cache entry structure
         $file = $this->getCacheFile($cacheKey);
         try {
             FilesystemManager::removeFile($file);
             return true;
         } catch (FileException $e) {
             return false;
         }
     }
     return false;
 }
Example #3
0
 /**
  * Returns the complete cache file name. Due to filesystem performance reasons,
  * the cache key folder is separated into several parts:
  * <ul>
  * <li>base folder</li>
  * <li>namespace</li>
  * <li>2 letters of the cache key</li>
  * <li>cache key</li>
  * <li>2 letters of cache sub key (in case of an AdvancedCacheKey)</li>
  * <li>cache sub key as file name (in case of an AdvancedCacheKey)</li>
  * <li>apfc as file extension</li>
  * </ul>
  *
  * @param CacheKey $cacheKey the application's cache key.
  *
  * @return string The cache file name.
  *
  * @author Christian Achatz
  * @version
  * Version 0.1, 21.11.2008<br />
  * Version 0.2, 05.08.2010 (Enhanced cache file structure)<br />
  */
 protected function getCacheFile(CacheKey $cacheKey)
 {
     $baseFolder = $this->getConfigAttribute('BaseFolder');
     $namespace = str_replace('\\', '/', $this->getConfigAttribute('Namespace'));
     $key = md5($cacheKey->getKey());
     $folder = substr($key, 0, 2);
     if ($cacheKey instanceof AdvancedCacheKey) {
         $subKey = md5($cacheKey->getSubKey());
         $subFolder = substr($subKey, 0, 2);
         return $baseFolder . '/' . $namespace . '/' . $folder . '/' . $key . '/' . $subFolder . '/' . $subKey . '.apfc';
     } else {
         return $baseFolder . '/' . $namespace . '/' . $folder . '/' . $key . '.apfc';
     }
 }