public function testAdd() { CM_Config::get()->CM_Paging_Ip_Blocked->maxAge = 3 * 86400; $ip = '127.0.0.1'; $ip2 = '127.0.0.2'; $paging = new CM_Paging_Ip_Blocked(); $paging->add(ip2long($ip)); $this->assertEquals(1, $paging->getCount()); $entry = $paging->getItem(0); $this->assertTrue($paging->contains(ip2long($ip))); CMTest_TH::timeDaysForward(2); $paging->add(ip2long($ip2)); CM_Cache_Local::getInstance()->flush(); $paging->_change(); $this->assertEquals(2, $paging->getCount()); CMTest_TH::timeDaysForward(2); CM_Paging_Ip_Blocked::deleteOld(); CM_Cache_Local::getInstance()->flush(); $paging->_change(); $this->assertEquals(1, $paging->getCount()); CMTest_TH::timeDaysForward(2); CM_Paging_Ip_Blocked::deleteOld(); CM_Cache_Local::getInstance()->flush(); $this->assertEquals(1, $paging->getCount()); $paging->_change(); $this->assertEquals(0, $paging->getCount()); }
/** * @param string $property * @return string|false */ public function getVersion($property) { $property = (string) $property; $cache = CM_Cache_Local::getInstance(); return $cache->get($cache->key(__METHOD__, $this->_headerList, $property), function () use($property) { return $this->_parser->version($property); }); }
/** * @return array[] ['name' => string, 'fileName' => string, 'codes' => string[]] */ public static function getEmoticonData() { $cache = CM_Cache_Local::getInstance(); $cacheKey = CM_CacheConst::Emoticons; if (false === ($dataList = $cache->get($cacheKey))) { $dataList = static::_readEmoticonData(); $cache->set($cacheKey, $dataList); } return $dataList; }
public function testContains() { $user = CMTest_TH::createUser(); $this->assertFalse((new CM_Paging_User_Online())->contains($user)); CM_Cache_Local::getInstance()->flush(); $user->setOnline(true); $this->assertTrue((new CM_Paging_User_Online())->contains($user)); CM_Cache_Local::getInstance()->flush(); $user->setOnline(false); $this->assertFalse((new CM_Paging_User_Online())->contains($user)); }
public function testCacheLocal() { $source = new CM_PagingSource_Sql('`num`', 'test'); $source->enableCacheLocal(); $this->assertEquals(100, $source->getCount()); CM_Db_Db::delete('test', array('num' => 0)); $this->assertEquals(100, $source->getCount()); $source->clearCache(); $this->assertEquals(99, $source->getCount()); CM_Cache_Local::getInstance()->flush(); }
/** * @param int $width * @param int $height * @return string */ private static function _createBlankImage($width, $height) { $cache = CM_Cache_Local::getInstance(); return $cache->get($cache->key(__CLASS__, $width, $height), function () use($width, $height) { $image = imagecreate($width, $height); ob_start(); imagecolorallocatealpha($image, 255, 255, 255, 127); imagepng($image); $imageData = ob_get_contents(); ob_end_clean(); return $imageData; }); }
/** * @param string $text * @return string */ protected function _getCacheKey($text) { $cacheKey = CM_CacheConst::Usertext . '_text:' . md5($text); $filterList = $this->_getFilters(); if (0 !== count($filterList)) { $cacheKeyListFilter = array_map(function (CM_Usertext_Filter_Interface $filter) { return $filter->getCacheKey(); }, $filterList); $cache = CM_Cache_Local::getInstance(); $cacheKey .= '_filter:' . $cache->key($cacheKeyListFilter); } return $cacheKey; }
/** * @param CM_Site_Abstract $site * @param string $zoneName * @return array * @throws CM_Exception_Invalid */ protected function _getZoneData(CM_Site_Abstract $site, $zoneName) { $cacheKey = CM_CacheConst::AdproviderZones . '_siteId:' . $site->getId(); $cache = CM_Cache_Local::getInstance(); if (false === ($zones = $cache->get($cacheKey))) { $zones = CM_Config::get()->CM_Adprovider->zones; if (isset($site->getConfig()->CM_Adprovider->zones)) { $zones = array_merge($zones, $site->getConfig()->CM_Adprovider->zones); } $cache->set($cacheKey, $zones); } if (!array_key_exists($zoneName, $zones)) { return null; } return $zones[$zoneName]; }
protected function _cacheGet($key) { $tag = CM_Cache_Shared::getInstance()->key(CM_CacheConst::PagingSource, $this->_cacheKeyBase()); $key = CM_Cache_Shared::getInstance()->key(CM_CacheConst::PagingSource, $key); if ($this->_cacheLocalLifetime) { if (($result = CM_Cache_Local::getInstance()->getTagged($tag, $key)) !== false) { return $result; } } if ($this->_cacheLifetime) { if (($result = CM_Cache_Shared::getInstance()->getTagged($tag, $key)) !== false) { return $result; } } return false; }
/** * @param CM_Site_Abstract $site * @param string $zoneName * @return mixed * @throws CM_Exception_Invalid */ protected function _getZone(CM_Site_Abstract $site, $zoneName) { $cacheKey = CM_CacheConst::AdproviderZones . '_siteId:' . $site->getId(); $cache = CM_Cache_Local::getInstance(); if (false === ($zones = $cache->get($cacheKey))) { $zones = CM_Config::get()->CM_Adprovider->zones; if (isset($site->getConfig()->CM_Adprovider->zones)) { $zones = array_merge($zones, $site->getConfig()->CM_Adprovider->zones); } $cache->set($cacheKey, $zones); } if (!array_key_exists($zoneName, $zones)) { throw new CM_Exception_Invalid('Zone `' . $zoneName . '` not configured.'); } return $zones[$zoneName]; }
/** * @return array of arrays */ public static function getDefaults() { $cacheKey = CM_CacheConst::User_Asset_Preferences_Defaults; $cache = CM_Cache_Local::getInstance(); if (($defaults = $cache->get($cacheKey)) === false) { $defaults = array(); $rows = CM_Db_Db::select('cm_user_preferenceDefault', array('section', 'key', 'preferenceId', 'defaultValue', 'configurable'))->fetchAll(); foreach ($rows as $default) { if (!isset($defaults[$default['section']])) { $defaults[$default['section']] = array(); } $defaults[$default['section']][$default['key']] = array('id' => (int) $default['preferenceId'], 'value' => (bool) $default['defaultValue'], 'configurable' => (bool) $default['configurable']); } $cache->set($cacheKey, $defaults); } return $defaults; }
/** * @param CM_Model_User $user * @throws CM_Exception_Invalid * @return boolean */ public function getEnabled(CM_Model_User $user) { $cacheKey = CM_CacheConst::SplitFeature_Fixtures . '_userId:' . $user->getId(); $cacheWrite = false; $cache = CM_Cache_Local::getInstance(); if (($fixtures = $cache->get($cacheKey)) === false) { $fixtures = CM_Db_Db::select('cm_splitfeature_fixture', array('splitfeatureId', 'fixtureId'), array('userId' => $user->getId()))->fetchAllTree(); $cacheWrite = true; } if (!array_key_exists($this->getId(), $fixtures)) { $fixtureId = CM_Db_Db::replace('cm_splitfeature_fixture', array('splitfeatureId' => $this->getId(), 'userId' => $user->getId())); $fixtures[$this->getId()] = $fixtureId; $cacheWrite = true; } if ($cacheWrite) { $cache->set($cacheKey, $fixtures); } return $this->_calculateEnabled($fixtures[$this->getId()]); }
/** * @param CM_Frontend_Render $render * @return array */ private function _getEmoticonData(CM_Frontend_Render $render) { $cacheKey = CM_CacheConst::Usertext_Filter_EmoticonList . '_fixedHeight:' . (string) $this->_fixedHeight; $cache = CM_Cache_Local::getInstance(); if (($emoticons = $cache->get($cacheKey)) === false) { $emoticons = array('codes' => array(), 'htmls' => array()); $fixedHeight = ''; if (null !== $this->_fixedHeight) { $fixedHeight = ' height="' . $this->_fixedHeight . '"'; } /** @var CM_Emoticon $emoticon */ foreach (new CM_Paging_Emoticon_All() as $emoticon) { foreach ($emoticon->getCodes() as $code) { $emoticons['codes'][] = $code; $emoticons['htmls'][] = '<img src="' . $render->getUrlResource('layout', 'img/emoticon/' . $emoticon->getFileName()) . '" class="emoticon emoticon-' . $emoticon->getName() . '" title="' . $emoticon->getDefaultCode() . '"' . $fixedHeight . ' />'; } } $cache->set($cacheKey, $emoticons); } return $emoticons; }
public function testSetDefault() { $this->assertSame(array(), CM_ModelAsset_User_Preferences::getDefaults()); CM_ModelAsset_User_Preferences::setDefault('foo', 'bar', true, false); CM_Cache_Local::getInstance()->flush(); $preferences = CM_ModelAsset_User_Preferences::getDefaults(); $this->assertCount(1, $preferences); $this->assertTrue($preferences['foo']['bar']['value']); $this->assertFalse($preferences['foo']['bar']['configurable']); $id = $preferences['foo']['bar']['id']; CM_ModelAsset_User_Preferences::setDefault('foo', 'bar', false, false); CM_Cache_Local::getInstance()->flush(); $preferences = CM_ModelAsset_User_Preferences::getDefaults(); $this->assertCount(1, $preferences); $this->assertSame($id, $preferences['foo']['bar']['id']); $this->assertFalse($preferences['foo']['bar']['value']); CM_ModelAsset_User_Preferences::setDefault('bar', 'foo', false, false); CM_Cache_Local::getInstance()->flush(); $preferences = CM_ModelAsset_User_Preferences::getDefaults(); $this->assertCount(2, $preferences); }
/** * @param int $id * @param int|null $type * @throws CM_Exception_Invalid|CM_Exception_Nonexistent * @return static */ public static function factory($id, $type = null) { if (null === $type) { $cacheKey = CM_CacheConst::StreamChannel_Type . '_id:' . $id; $cache = CM_Cache_Local::getInstance(); if (false === ($type = $cache->get($cacheKey))) { $type = CM_Db_Db::select('cm_streamChannel', 'type', array('id' => $id))->fetchColumn(); if (false === $type) { throw new CM_Exception_Nonexistent('No record found in `cm_streamChannel`', null, ['id' => $id]); } $cache->set($cacheKey, $type); } } $class = self::_getClassName($type); $instance = new $class($id); if (!$instance instanceof static) { throw new CM_Exception_Invalid('Unexpected instance', null, ['actualClass' => $class, 'expectedClass' => get_called_class()]); } return $instance; }
/** * @param string $abbreviation */ private static function _deleteCacheByAbbreviation($abbreviation) { $cache = CM_Cache_Local::getInstance(); $cacheKey = CM_CacheConst::Currency_ByAbbreviation . '_abbreviation:' . $abbreviation; $cache->delete($cacheKey); }
/** * @return CM_Model_Language|null */ public static function findDefault() { $cacheKey = CM_CacheConst::Language_Default; $cache = CM_Cache_Local::getInstance(); if (false === ($languageId = $cache->get($cacheKey))) { $languageId = CM_Db_Db::select('cm_model_language', 'id', array('enabled' => true, 'backupId' => null))->fetchColumn(); $cache->set($cacheKey, $languageId); } if (!$languageId) { return null; } return new static($languageId); }
public function testGetByLocation() { $currencyDefault = CMTest_TH::createDefaultCurrency(); $currencyEUR = CM_Model_Currency::create('978', 'EUR'); $this->assertEquals($currencyDefault, CM_Model_Currency::getByLocation(null)); $countryId = CM_Db_Db::insert('cm_model_location_country', array('abbreviation' => 'DE', 'name' => 'Germany')); $country = new CM_Model_Location(CM_Model_Location::LEVEL_COUNTRY, $countryId); $this->assertEquals($currencyDefault, CM_Model_Currency::getByLocation($country)); $currencyEUR->setCountryMapping($country); $cache = CM_Cache_Local::getInstance(); $cacheKey = CM_CacheConst::Currency_CountryId . '_countryId:' . $country->getId(); $cache->delete($cacheKey); $this->assertEquals($currencyEUR, CM_Model_Currency::getByLocation($country)); }
public function delete($type, array $id) { CM_Cache_Local::getInstance()->delete($this->_getCacheKey($type, $id)); }
/** * @return CM_Tree_Language */ public static function getTree() { $cacheKey = CM_CacheConst::LanguageKey_Tree; $cache = CM_Cache_Local::getInstance(); if (false === ($tree = $cache->get($cacheKey))) { $tree = new CM_Tree_Language(); $cache->set($cacheKey, $tree); } return $tree; }
/** * Enable local (non-invalidatable) cache * Cost: 1 apc request * * @param int $lifetime */ public function enableCacheLocal($lifetime = 60) { $this->enableCache($lifetime, CM_Cache_Local::getInstance()); }
/** * @param Closure|null $callbackBefore fn($version) * @param Closure|null $callbackAfter fn($version) * @return int Number of version bumps */ public function runUpdateScripts(Closure $callbackBefore = null, Closure $callbackAfter = null) { CM_Cache_Shared::getInstance()->flush(); CM_Cache_Local::getInstance()->flush(); $versionBumps = 0; foreach ($this->getUpdateScriptPaths() as $namespace => $path) { $version = $versionStart = $this->getVersion($namespace); while (true) { $version++; if (!$this->runUpdateScript($namespace, $version, $callbackBefore, $callbackAfter)) { $version--; break; } $this->setVersion($version, $namespace); } $versionBumps += $version - $versionStart; } if ($versionBumps > 0) { $db = $this->getServiceManager()->getDatabases()->getMaster()->getDatabaseName(); CM_Db_Db::exec('DROP DATABASE IF EXISTS `' . $db . '_test`'); } return $versionBumps; }
/** * @param int $ip * @return CM_Model_Location|null */ public static function findByIp($ip) { $cacheKey = CM_CacheConst::Location_ByIp . '_ip:' . $ip; $cache = CM_Cache_Local::getInstance(); $location = null; if ((list($level, $id) = $cache->get($cacheKey)) === false) { if ($location = self::_findByIp($ip)) { $level = $location->getLevel(); $id = $location->getId(); } $cache->set($cacheKey, array($level, $id)); } if (!$level || !$id) { return null; } if (!$location) { $location = new self($level, $id); } return $location; }
public function testFindDefault() { $language = CM_Model_Language::create('English', 'en', true); $this->assertEquals($language, CM_Model_Language::findDefault()); $language->setEnabled(false); $this->assertEquals($language, CM_Model_Language::findDefault()); CM_Cache_Local::getInstance()->flush(); $this->assertNull(CM_Model_Language::findDefault()); }
public static function clearCache() { CM_Cache_Shared::getInstance()->flush(); CM_Cache_Local::getInstance()->flush(); }
/** * @return array */ protected function _getAggregationData() { $cacheKey = $this->_getCacheKeyAggregation(); $cache = CM_Cache_Local::getInstance(); if (false === ($aggregationData = $cache->get($cacheKey))) { $conversionData = CM_Db_Db::execRead(' SELECT COUNT(1) as `conversionCount`, SUM(`conversionWeight`) as `conversionWeight`, SUM(`conversionWeight` * `conversionWeight`) as `conversionWeightSquared` FROM `cm_splittestVariation_fixture` WHERE `splittestId`=? AND `variationId`=? AND `conversionStamp` IS NOT NULL', array($this->_getSplittestId(), $this->getId()))->fetch(); $fixtureCount = (int) CM_Db_Db::execRead('SELECT COUNT(1) FROM `cm_splittestVariation_fixture` WHERE `splittestId`=? AND `variationId`=?', array($this->_getSplittestId(), $this->getId()))->fetchColumn(); $aggregationData = array('conversionCount' => (int) $conversionData['conversionCount'], 'conversionWeight' => (double) $conversionData['conversionWeight'], 'conversionWeightSquared' => (double) $conversionData['conversionWeightSquared'], 'fixtureCount' => $fixtureCount); $cache->set($cacheKey, $aggregationData, 30); } return $aggregationData; }
public function testFind() { $this->assertNull(CM_Model_Splitfeature::find('foo')); CM_Cache_Local::getInstance()->flush(); $splitfeature = CM_Model_Splitfeature::createStatic(array('name' => 'foo', 'percentage' => 0)); $this->assertEquals($splitfeature, CM_Model_Splitfeature::find('foo')); }
/** * @param string $className * @param boolean|null $includeAbstracts * @return string[] */ public static function getClassChildren($className, $includeAbstracts = null) { $key = CM_CacheConst::ClassChildren . '_className:' . $className . '_abstracts:' . (int) $includeAbstracts; $cache = CM_Cache_Local::getInstance(); if (false === ($classNames = $cache->get($key))) { $pathsFiltered = array(); $paths = array(); foreach (CM_Bootloader::getInstance()->getModules() as $modulePath) { $namespacePaths = CM_Util::rglob('*.php', CM_Util::getModulePath($modulePath) . 'library/'); $paths = array_merge($paths, $namespacePaths); } $regexp = '#\\bclass\\s+(?<name>[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)\\s+#'; foreach ($paths as $path) { $file = new CM_File($path); $fileContents = $file->read(); if (preg_match($regexp, $fileContents, $matches)) { if (class_exists($matches['name'], true)) { $reflectionClass = new ReflectionClass($matches['name']); if (($reflectionClass->isSubclassOf($className) || interface_exists($className) && $reflectionClass->implementsInterface($className)) && (!$reflectionClass->isAbstract() || $includeAbstracts)) { $pathsFiltered[] = $path; } } } } $classNames = self::getClasses($pathsFiltered); $cache->set($key, $classNames); } return $classNames; }
public function testFindByAttributesCache() { $cacheKey = CM_CacheConst::Location_ByAttribute . '_level:' . CM_Model_Location::LEVEL_COUNTRY . '_name:abbreviation_value:CH_name:name_value:Switzerland'; $cache = CM_Cache_Local::getInstance(); $this->assertFalse($cache->get($cacheKey)); $country = CM_Model_Location::findByAttributes(CM_Model_Location::LEVEL_COUNTRY, ['abbreviation' => 'CH', 'name' => 'Switzerland']); $this->assertSame($country->getId(), (int) $cache->get($cacheKey)); }
/** * @param CM_Splittest_Fixture $fixture * @param bool|null $updateCache * @return array */ protected function _getVariationListFixture(CM_Splittest_Fixture $fixture, $updateCache = null) { $columnId = $fixture->getColumnId(); $columnIdQuoted = CM_Db_Db::getClient()->quoteIdentifier($columnId); $fixtureId = $fixture->getId(); $updateCache = (bool) $updateCache; $cacheKey = self::_getCacheKeyFixture($fixture); $cache = CM_Cache_Local::getInstance(); if ($updateCache || ($variationListFixture = $cache->get($cacheKey)) === false) { $variationListFixture = CM_Db_Db::exec(' SELECT `variation`.`splittestId`, `variation`.`name` AS `variation`, `splittest`.`createStamp` AS `flushStamp` FROM `cm_splittestVariation_fixture` `fixture` JOIN `cm_splittestVariation` `variation` ON(`variation`.`id` = `fixture`.`variationId`) JOIN `cm_splittest` `splittest` ON(`splittest`.`id` = `fixture`.`splittestId`) WHERE `fixture`.' . $columnIdQuoted . ' = ?', array($fixtureId))->fetchAllTree(); $cache->set($cacheKey, $variationListFixture); } return $variationListFixture; }