public static function _get_cache_name($key, $cache_name = 'default') { $cache_key = $key; $key_md5 = md5($key); $config = Cache::config($cache_name); if (strtolower($config["adapter"]) == 'file') { $cache_key = 'cache/' . substr($key_md5, 0, 1) . '/' . substr($key_md5, 1, 1) . '/' . substr($key_md5, 2, 1) . '/' . $key_md5; } else { $cache_key .= '_' . substr($key_md5, 0, 2); } return $cache_key; }
* Lithium: the most rad php framework * * @copyright Copyright 2010, Union of RAD (http://union-of-rad.org) * @license http://opensource.org/licenses/bsd-license.php The BSD License */ /** * This file creates a default cache configuration using the most optimized adapter available, and * uses it to provide default caching for high-overhead operations. */ use lithium\storage\Cache; use lithium\core\Libraries; use lithium\action\Dispatcher; use lithium\storage\cache\adapter\Apc; /** * If APC is not available and the cache directory is not writeable, bail out. */ if (!($apcEnabled = Apc::enabled() && !is_writable(LITHIUM_APP_PATH . '/resources/tmp/cache'))) { return; } Cache::config(array('default' => array('adapter' => '\\lithium\\storage\\cache\\adapter\\' . ($apcEnabled ? 'Apc' : 'File')))); Dispatcher::applyFilter('run', function ($self, $params, $chain) { if ($cache = Cache::read('default', 'core.libraries')) { $cache = (array) unserialize($cache) + Libraries::cache(); Libraries::cache($cache); } $result = $chain->next($self, $params, $chain); if ($cache != Libraries::cache()) { Cache::write('default', 'core.libraries', serialize(Libraries::cache()), '+1 day'); } return $result; });
* removed post-install, and the cache should be configured with the adapter you plan to use. */ $cachePath = Libraries::get(true, 'resources') . '/tmp/cache'; if (!($apcEnabled = Apc::enabled()) && !is_writable($cachePath)) { return; } /** * This configures the default cache, based on whether ot not APC user caching is enabled. If it is * not, file caching will be used. Most of this code is for getting you up and running only, and * should be replaced with a hard-coded configuration, based on the cache(s) you plan to use. */ $default = array('adapter' => 'File', 'strategies' => array('Serializer')); if ($apcEnabled) { $default = array('adapter' => 'Apc'); } Cache::config(compact('default')); /** * Caches paths for auto-loaded and service-located classes. */ Dispatcher::applyFilter('run', function ($self, $params, $chain) { $key = md5(LITHIUM_APP_PATH) . '.core.libraries'; if ($cache = Cache::read('default', $key)) { $cache = (array) $cache + Libraries::cache(); Libraries::cache($cache); } $result = $chain->next($self, $params, $chain); if ($cache != Libraries::cache()) { Cache::write('default', $key, Libraries::cache(), '+1 day'); } return $result; });
public function testIntegrationFileAdapterWrite() { $config = array('default' => array('adapter' => 'File', 'path' => LITHIUM_APP_PATH . '/resources/tmp/cache', 'filters' => array())); Cache::config($config); $result = Cache::write('default', 'key', 'value', '+1 minute'); $this->assertTrue($result); $time = time() + 60; $result = file_get_contents(LITHIUM_APP_PATH . '/resources/tmp/cache/key'); $expected = "{:expiry:{$time}}\nvalue"; $this->assertEqual($result, $expected); $result = unlink(LITHIUM_APP_PATH . '/resources/tmp/cache/key'); $this->assertTrue($result); $this->assertFalse(file_exists(LITHIUM_APP_PATH . '/resources/tmp/cache/key')); }
* Most of this code is for getting you up and running only, and should be replaced with * a hard-coded configuration, based on the cache(s) you plan to use. * * We create a default cache configuration using the most optimized adapter available, and * use it to provide default caching for high-overhead operations. If APC is not available * and we can't degrade to file based caching, bail out. * * @see lithium\storage\Cache * @see lithium\storage\cache\adapters * @see lithium\storage\cache\strategies */ $cachePath = Libraries::get(true, 'resources') . '/tmp/cache'; if (!(($apc = Apc::enabled()) || PHP_SAPI === 'cli') && !is_writable($cachePath)) { return; } Cache::config(array('default' => array('adapter' => $apc ? 'Apc' : 'File', 'strategies' => $apc ? array() : array('Serializer'), 'scope' => $apc ? md5(LITHIUM_APP_PATH) : null))); /** * Apply * * Applies caching to neuralgic points of the framework but only when we are running * in production. This is also a good central place to add your own caching rules. * * A couple of caching rules are already defined below: * 1. Cache paths for auto-loaded and service-located classes. * 2. Cache describe calls on all connections that use a `Database` based adapter. * * @see lithium\core\Environment * @see lithium\core\Libraries */ if (!Environment::is('production')) { return;
$cacheKey = 'core.libraries'; if ($cached = Cache::read('default', $cacheKey)) { $cached = (array) $cached + Libraries::cache(); Libraries::cache($cached); } $result = $chain->next($self, $params, $chain); if ($cached != ($data = Libraries::cache())) { Cache::write('default', $cacheKey, $data, '+1 day'); } return $result; }); Dispatcher::applyFilter('run', function ($self, $params, $chain) { foreach (Connections::get() as $name) { if (!($connection = Connections::get($name)) instanceof Database) { continue; } $connection->applyFilter('describe', function ($self, $params, $chain) use($name) { if ($params['fields']) { return $chain->next($self, $params, $chain); } $cacheKey = "data.connections.{$name}.sources.{$params['entity']}.schema"; return Cache::read('default', $cacheKey, array('write' => function () use($self, $params, $chain) { return array('+1 day' => $chain->next($self, $params, $chain)); })); }); } return $chain->next($self, $params, $chain); }); // setup default memcache cache Cache::config(array('memcache' => array('adapter' => 'Memcache', 'host' => '127.0.0.1:11211')));
public function testNonPortableCacheAdapterMethod() { $config = array('default' => array('adapter' => 'Memory', 'filters' => array())); Cache::config($config); $result = Cache::config(); $expected = $config; $this->assertEqual($expected, $result); }
<?php use lithium\storage\Cache; use app\extensions\core\Constant; use app\extensions\core\Connections; Constant::set_define('ISDEV', false); Constant::set_define('DEMO_LOGIN', false); Constant::set_define('CACHE', true); Constant::set_define('SYS_PATH', 'http://coffeesdk.sinaapp.com'); Constant::set_define('WEIXIN_APP_ID', ''); Constant::set_define('WEIXIN_APP_SECRET', ''); // ini_set('session.save_handler', 'memcache'); // ini_set('session.save_path', 'tcp://' . DB_SERVER . ':11221'); Connections::add('default', array('type' => 'database', 'adapter' => 'MySql', 'host' => SAE_MYSQL_HOST_M, 'login' => SAE_MYSQL_USER, 'password' => SAE_MYSQL_PASS, 'database' => SAE_MYSQL_DB, 'port' => SAE_MYSQL_PORT, 'encoding' => 'UTF-8')); Connections::add('default_read', array('type' => 'database', 'adapter' => 'MySql', 'host' => SAE_MYSQL_HOST_S, 'login' => SAE_MYSQL_USER, 'password' => SAE_MYSQL_PASS, 'database' => SAE_MYSQL_DB, 'port' => SAE_MYSQL_PORT, 'encoding' => 'UTF-8')); Connections::add('feeds', array('type' => 'database', 'adapter' => 'MySql', 'host' => SAE_MYSQL_HOST_M, 'login' => SAE_MYSQL_USER, 'password' => SAE_MYSQL_PASS, 'database' => SAE_MYSQL_DB, 'port' => SAE_MYSQL_PORT, 'encoding' => 'UTF-8')); Cache::config(array('default' => array('adapter' => 'Memcache', 'host' => '127.0.0.1:11221')));
public function testFileAdapterMultipleStrategies() { $resources = Libraries::get(true, 'resources'); $path = "{$resources}/tmp/cache"; $this->skipIf(!$this->_checkPath(), "{$path} does not have the proper permissions."); $config = array('default' => compact('path') + array('adapter' => 'File', 'filters' => array(), 'strategies' => array('Serializer', 'Base64'))); Cache::config($config); $data = array('some' => 'data'); $time = time(); $result = Cache::write('default', 'key', $data, "@{$time} +1 minute"); $this->assertNotEmpty($result); $time = $time + 60; $result = file_get_contents("{$path}/key"); $expected = "{:expiry:{$time}}\nYToxOntzOjQ6InNvbWUiO3M6NDoiZGF0YSI7fQ=="; $this->assertEqual($result, $expected); $result = Cache::read('default', 'key'); $this->assertEqual($data, $result); $result = unlink("{$path}/key"); $this->assertTrue($result); $this->assertFileNotExists("{$path}/key"); }
use lithium\action\Dispatcher; use lithium\storage\cache\adapter\Apc; /** * If APC is not available and the cache directory is not writeable, bail out. This block should be * removed post-install, and the cache should be configured with the adapter you plan to use. */ $cachePath = Libraries::get(true, 'resources') . '/tmp/cache'; if (!(($apcEnabled = Apc::enabled()) || PHP_SAPI === 'cli') && !is_writable($cachePath)) { return; } /** * This configures the default cache, based on whether ot not APC user caching is enabled. If it is * not, file caching will be used. Most of this code is for getting you up and running only, and * should be replaced with a hard-coded configuration, based on the cache(s) you plan to use. */ Cache::config(array('default' => $apcEnabled ? array('adapter' => 'Apc') : array('adapter' => 'File', 'strategies' => array('Serializer')))); /** * Caches paths for auto-loaded and service-located classes when in production. */ Dispatcher::applyFilter('run', function ($self, $params, $chain) { if (!Environment::is('production')) { return $chain->next($self, $params, $chain); } $key = md5(LITHIUM_APP_PATH) . '.core.libraries'; if ($cache = Cache::read('default', $key)) { $cache = (array) $cache + Libraries::cache(); Libraries::cache($cache); } $result = $chain->next($self, $params, $chain); if ($cache != Libraries::cache()) { Cache::write('default', $key, Libraries::cache(), '+1 day');
/** * Sets up and configers the logger and also the cache storage for testing. */ public function setUp() { CacheStorage::config(array('cachelog' => array('adapter' => 'Memory'))); $this->cachelog = new Cache(array('key' => 'cachelog_testkey', 'config' => 'cachelog')); Logger::config(array('cachelog' => array('adapter' => $this->cachelog, 'key' => 'cachelog_testkey', 'config' => 'cachelog'))); }
public function setUp() { Connections::add('connection_test', array('type' => 'MongoDb', 'adapter' => 'MongoDb', 'host' => 'localhost', 'database' => 'database_test')); Cache::config(array($this->cachedName => array('adapter' => 'MongoDb', 'connection' => 'connection_test', 'database' => 'database_test', 'collection' => 'collection_test', 'expiry' => '+10 days', 'capped' => false))); Cache::clear($this->cachedName); }
public function testIntegrationFileAdapterMultipleStrategies() { $directory = new SplFileInfo(Libraries::get(true, 'resources') . "/tmp/cache/"); $accessible = $directory->isDir() && $directory->isReadable() && $directory->isWritable(); $message = "{$directory} does not have the proper permissions."; $this->skipIf(!$accessible, $message); $config = array('default' => array('adapter' => 'File', 'path' => Libraries::get(true, 'resources') . '/tmp/cache', 'filters' => array(), 'strategies' => array('Serializer', 'Base64'))); Cache::config($config); $data = array('some' => 'data'); $result = Cache::write('default', 'key', $data, '+1 minute'); $this->assertTrue($result); $time = time() + 60; $result = file_get_contents(Libraries::get(true, 'resources') . '/tmp/cache/key'); $expected = "{:expiry:{$time}}\nYToxOntzOjQ6InNvbWUiO3M6NDoiZGF0YSI7fQ=="; $this->assertEqual($result, $expected); $result = Cache::read('default', 'key'); $this->assertEqual($data, $result); $result = unlink(Libraries::get(true, 'resources') . '/tmp/cache/key'); $this->assertTrue($result); $this->assertFalse(file_exists(Libraries::get(true, 'resources') . '/tmp/cache/key')); }
<?php use lithium\storage\Cache; use app\extensions\core\Connections; use app\extensions\core\Constant; Constant::set_define('ISDEV', true); Constant::set_define('DEMO_LOGIN', true); Constant::set_define('CACHE', false); Constant::set_define('SYS_PATH', 'http://dev.raytrend.oa.com'); Constant::set_define('DEV_SERVER_HOSET', 'localhost'); Constant::set_define('WEIXIN_APP_ID', ''); Constant::set_define('WEIXIN_APP_SECRET', ''); Connections::add('default', array('type' => 'database', 'adapter' => 'MySql', 'host' => 'localhost', 'login' => 'root', 'password' => 'sa', 'database' => 'rwe', 'encoding' => 'UTF-8', 'persistent' => false)); Connections::add('feeds', array('type' => 'database', 'adapter' => 'MySql', 'host' => 'localhost', 'login' => 'root', 'password' => 'sa', 'database' => 'rwe_feeds', 'encoding' => 'UTF-8')); Cache::config(array('default' => array('adapter' => 'File', 'strategies' => array('Serializer'))));
*/ use lithium\storage\Cache; use lithium\core\Libraries; use lithium\action\Dispatcher; use lithium\storage\cache\adapter\Memcache; use lithium\storage\cache\adapter\File; if (PHP_SAPI === 'cli') { return; } /** * If APC is not available and the cache directory is not writeable, bail out. */ if (!($memcacheEnabled = Memcache::enabled()) && !is_writable(LITHIUM_APP_PATH . '/resources/tmp/cache')) { return; } if ($memcacheEnabled && USE_MEMCACHED === true) { Cache::config(array('default' => array('adapter' => 'Memcache', 'host' => MEMCACHED_SERVER))); } else { Cache::config(array('default' => array('adapter' => 'File'))); } Dispatcher::applyFilter('run', function ($self, $params, $chain) { if ($cache = Cache::read('default', 'core.libraries')) { $cache = (array) unserialize($cache) + Libraries::cache(); Libraries::cache($cache); } $result = $chain->next($self, $params, $chain); if ($cache != Libraries::cache()) { Cache::write('default', 'core.libraries', serialize(Libraries::cache()), '+1 day'); } return $result; });
* Configures the adapters to use with the cache class. Available adapters are `Memcache`, * `File`, `Redis`, `Apc`, `XCache` and `Memory`. Please see the documentation on the * adapters for specific characteristics and requirements. * * Most of this code is for getting you up and running only, and should be replaced with * a hard-coded configuration, based on the cache(s) you plan to use. * * We create a default cache configuration using the most optimized adapter available, and * use it to provide default caching for high-overhead operations. If APC is not available * and we can't degrade to file based caching, bail out. * * @see lithium\storage\Cache * @see lithium\storage\cache\adapters * @see lithium\storage\cache\strategies */ Cache::config(array('default' => array('adapter' => 'Memcache', 'scope' => PROJECT_NAME . '_' . md5(LITHIUM_APP_PATH . PROJECT_VERSION)))); /** * Apply * * Applies caching to neuralgic points of the framework but only when we are running * in production. This is also a good central place to add your own caching rules. * * Here we cache paths for auto-loaded and service-located classes. * * @see lithium\core\Environment * @see lithium\core\Libraries */ Dispatcher::applyFilter('run', function ($self, $params, $chain) { $cacheKey = 'core.libraries'; if ($cached = Cache::read('default', $cacheKey)) { $cached = (array) $cached + Libraries::cache();