TCache is the base class for cache classes with different cache storage implementation. TCache implements the interface {@link ICache} with the following methods, - {@link get} : retrieve the value with a key (if any) from cache - {@link set} : store the value with a key into cache - {@link add} : store the value only if cache does not have this key - {@link delete} : delete the value with the specified key from cache - {@link flush} : delete all values from cache Each value is associated with an expiration time. The {@link get} operation ensures that any expired value will not be returned. The expiration time by the number of seconds. A expiration time 0 represents never expire. By definition, cache does not ensure the existence of a value even if it never expires. Cache is not meant to be an persistent storage. Child classes must implement the following methods: - {@link getValue} - {@link setValue} - {@link addValue} - {@link deleteValue} and optionally {@link flush} Since version 3.1.2, TCache implements the \ArrayAccess interface such that the cache acts as an array.
부터: 3.0
저자: Qiang Xue (qiang.xue@gmail.com)
상속: extends Prado\TModule, implements Prado\Caching\ICache, implements ArrayAccess
예제 #1
0
 * @package Prado\Caching
 */
namespace Prado\Caching;

use Prado\Exceptions\TConfigurationException;
/**
 * TEACache class
 *
예제 #2
0
파일: TXCache.php 프로젝트: pradosoft/prado
 /**
  * Initializes this module.
  * This method is required by the IModule interface.
  * @param TXmlElement configuration for this module, can be null
  * @throws TConfigurationException if xcache extension is not installed or not started, check your php.ini
  */
 public function init($config)
 {
     if (!function_exists('xcache_isset')) {
         throw new TConfigurationException('xcache_extension_required');
     }
     $enabled = (int) ini_get('xcache.cacher') !== 0;
     $var_size = (int) ini_get('xcache.var_size');
     if (!($enabled && $var_size > 0)) {
         throw new TConfigurationException('xcache_extension_not_enabled');
     }
     parent::init($config);
 }
예제 #3
0
 /**
  * Initializes this module.
  * This method is required by the IModule interface.
  * @param TXmlElement configuration for this module, can be null
  * @throws TConfigurationException if apc extension is not installed or not started, check your php.ini
  */
 public function init($config)
 {
     if (!extension_loaded('apc')) {
         throw new TConfigurationException('apccache_extension_required');
     }
     if (ini_get('apc.enabled') == false) {
         throw new TConfigurationException('apccache_extension_not_enabled');
     }
     if (substr(php_sapi_name(), 0, 3) === 'cli' and ini_get('apc.enable_cli') == false) {
         throw new TConfigurationException('apccache_extension_not_enabled_cli');
     }
     parent::init($config);
 }
예제 #4
0
 /**
  * Initializes this module.
  * This method is required by the IModule interface.
  * attach {@link doInitializeCache} to TApplication.OnLoadStateComplete event
  * attach {@link doFlushCacheExpired} to TApplication.OnSaveState event
  *
  * @param TXmlElement configuration for this module, can be null
  */
 public function init($config)
 {
     $this->getApplication()->attachEventHandler('OnLoadStateComplete', array($this, 'doInitializeCache'));
     $this->getApplication()->attachEventHandler('OnSaveState', array($this, 'doFlushCacheExpired'));
     parent::init($config);
 }
예제 #5
0
 /**
  * Initializes this module.
  * This method is required by the IModule interface. It checks if the DbFile
  * property is set, and creates a SQLiteDatabase instance for it.
  * The database or the cache table does not exist, they will be created.
  * Expired values are also deleted.
  * @param TXmlElement configuration for this module, can be null
  * @throws TConfigurationException if sqlite extension is not installed,
  *         DbFile is set invalid, or any error happens during creating database or cache table.
  */
 public function init($config)
 {
     if (!function_exists('sqlite_open')) {
         throw new TConfigurationException('sqlitecache_extension_required');
     }
     if ($this->_file === null) {
         $this->_file = $this->getApplication()->getRuntimePath() . '/sqlite.cache';
     }
     $error = '';
     if (($this->_db = new \SQLiteDatabase($this->_file, 0666, $error)) === false) {
         throw new TConfigurationException('sqlitecache_connection_failed', $error);
     }
     if (@$this->_db->query('DELETE FROM ' . self::CACHE_TABLE . ' WHERE expire<>0 AND expire<' . time()) === false) {
         if ($this->_db->query('CREATE TABLE ' . self::CACHE_TABLE . ' (key CHAR(128) PRIMARY KEY, value BLOB, expire INT)') === false) {
             throw new TConfigurationException('sqlitecache_table_creation_failed', sqlite_error_string(sqlite_last_error()));
         }
     }
     $this->_initialized = true;
     parent::init($config);
 }
예제 #6
0
 /**
  * Initializes this module.
  * This method is required by the IModule interface. It makes sure that
  * UniquePrefix has been set, creates a Memcache instance and connects
  * to the memcache server.
  * @param TApplication Prado application, can be null
  * @param TXmlElement configuration for this module, can be null
  * @throws TConfigurationException if memcache extension is not installed or memcache sever connection fails
  */
 public function init($config)
 {
     if (!extension_loaded('memcache') && !$this->_useMemcached) {
         throw new TConfigurationException('memcache_extension_required');
     }
     if (!extension_loaded('memcached') && $this->_useMemcached) {
         throw new TConfigurationException('memcached_extension_required');
     }
     $this->_cache = $this->_useMemcached ? new Memcached() : new Memcache();
     $this->loadConfig($config);
     if (count($this->_servers)) {
         foreach ($this->_servers as $server) {
             Prado::trace('Adding server ' . $server['Host'] . ' from serverlist', '\\Prado\\Caching\\TMemCache');
             if ($this->_cache->addServer($server['Host'], $server['Port'], $server['Persistent'], $server['Weight'], $server['Timeout'], $server['RetryInterval']) === false) {
                 throw new TConfigurationException('memcache_connection_failed', $server['Host'], $server['Port']);
             }
         }
     } else {
         Prado::trace('Adding server ' . $this->_host, '\\Prado\\Caching\\TMemCache');
         if ($this->_cache->addServer($this->_host, $this->_port) === false) {
             throw new TConfigurationException('memcache_connection_failed', $this->_host, $this->_port);
         }
     }
     if ($this->_threshold !== 0) {
         $this->_cache->setCompressThreshold($this->_threshold, $this->_minSavings);
     }
     $this->_initialized = true;
     parent::init($config);
 }