/** * Install hstore * /usr/share/postgresql/contrib # cat hstore.sql | psql -U pgsql -d onphp **/ public function testHstore() { foreach (DBTestPool::me()->getPool() as $connector => $db) { DBPool::me()->setDefault($db); $properties = array('age' => '23', 'weight' => 80, 'comment' => null); $user = TestUser::create()->setCity($moscow = TestCity::create()->setName('Moscow'))->setCredentials(Credentials::create()->setNickname('fake')->setPassword(sha1('passwd')))->setLastLogin(Timestamp::create(time()))->setRegistered(Timestamp::create(time())->modify('-1 day'))->setProperties(Hstore::make($properties)); $moscow = TestCity::dao()->add($moscow); $user = TestUser::dao()->add($user); Cache::me()->clean(); TestUser::dao()->dropIdentityMap(); $user = TestUser::dao()->getById('1'); $this->assertInstanceOf('Hstore', $user->getProperties()); $this->assertEquals($properties, $user->getProperties()->getList()); $form = TestUser::proto()->makeForm(); $form->get('properties')->setFormMapping(array(Primitive::string('age'), Primitive::integer('weight'), Primitive::string('comment'))); $form->import(array('id' => $user->getId())); $this->assertNotNull($form->getValue('id')); $object = $user; FormUtils::object2form($object, $form); $this->assertInstanceOf('Hstore', $form->getValue('properties')); $this->assertEquals(array_filter($properties), $form->getValue('properties')->getList()); $subform = $form->get('properties')->getInnerForm(); $this->assertEquals($subform->getValue('age'), '23'); $this->assertEquals($subform->getValue('weight'), 80); $this->assertNull($subform->getValue('comment')); $user = new TestUser(); FormUtils::form2object($form, $user, false); $this->assertEquals($user->getProperties()->getList(), array_filter($properties)); } }
protected function getLayerId() { if (!($result = Cache::me()->mark($this->className)->get($this->className))) { $result = mt_rand(1, self::MAX_RANDOM_ID); Cache::me()->mark($this->className)->set($this->className, $result, Cache::EXPIRES_FOREVER); } return '@' . $result; }
public function testCriteria() { foreach (DBTestPool::me()->getPool() as $db) { /* @var $db DB */ DBPool::me()->setDefault($db); $this->getDBCreator()->fillDB(); $queryResult = Criteria::create(TestCity::dao())->getResult(); $this->assertEquals(2, $queryResult->getCount()); Cache::me()->clean(); } }
public function uncache() { foreach ($this->classNameMap as $className => $uncaches) { list($idKeys, $tags, $worker) = $uncaches; /* @var $worker TaggableDaoWorker */ $worker->expireTags($tags); foreach ($idKeys as $key) { Cache::me()->mark($className)->delete($idKey); } ClassUtils::callStaticMethod("{$className}::dao")->uncacheLists(); } }
public function testUnified() { foreach (DBTestPool::me()->getPool() as $db) { DBPool::me()->setDefault($db); $this->getDBCreator()->fillDB(); $this->unified(); Cache::me()->clean(); TestUser::dao()->dropById(1); try { TestUser::dao()->dropByIds(array(1, 2)); $this->fail(); } catch (WrongStateException $e) { // ok } } }
protected function uncacheClassName($className, $indexKey, $intKey) { $cache = Cache::me(); $pool = SemaphorePool::me(); if ($pool->get($intKey)) { $indexList = $cache->mark($className)->get($indexKey); $cache->mark($className)->delete($indexKey); if ($indexList) { foreach (array_keys($indexList) as $key) { $cache->mark($className)->delete($key); } } $pool->free($intKey); return true; } $cache->mark($className)->delete($indexKey); return false; }
private function doTestMemcached(SelectivePeer $cache) { Cache::setPeer($cache); if (!Cache::me()->isAlive()) { return $this->markTestSkipped('memcached not available'); } for ($i = 0; $i < self::QUERIES; ++$i) { $this->assertTrue(Cache::me()->mark('one')->set($i, $i)); $this->assertTrue(Cache::me()->mark('two')->set($i, $i)); } $oneHit = 0; $twoHit = 0; for ($i = 0; $i < self::QUERIES; ++$i) { if (Cache::me()->mark('one')->get($i) == $i) { ++$oneHit; } if (Cache::me()->mark('two')->get($i) == $i) { ++$twoHit; } } $this->assertEquals($oneHit, $twoHit); $this->assertEquals($twoHit, self::QUERIES); }
private function lazyTest() { $city = TestCity::dao()->getById(1); $object = TestLazy::dao()->add(TestLazy::create()->setCity($city)->setCityOptional($city)->setEnum(new ImageType(ImageType::getAnyId()))); Cache::me()->clean(); $form = TestLazy::proto()->makeForm(); $form->import(array('id' => $object->getId())); $this->assertNotNull($form->getValue('id')); FormUtils::object2form($object, $form); foreach ($object->proto()->getPropertyList() as $name => $property) { if ($property->getRelationId() == MetaRelation::ONE_TO_ONE && $property->getFetchStrategyId() == FetchStrategy::LAZY) { $this->assertEquals($object->{$property->getGetter()}(), $form->getValue($name)); } } }
protected function uncacheClassName($className, $idKeys) { foreach ($idKeys as $key) { Cache::me()->mark($className)->delete($key); } }
protected function gentlyGetByKey($key) { if ($this->handler->ping($this->keyToInt($key))) { return Cache::me()->mark($this->className)->get($key); } else { Cache::me()->mark($this->className)->delete($key); return null; } }
public function getCachedData($key) { return Cache::me()->mark($this->className)->get($this->makeDataKey($key, self::SUFFIX_QUERY)); }
protected function cacheNullById($id) { return Cache::me()->mark($this->className)->add($this->makeIdKey($id), Cache::NOT_FOUND, Cache::EXPIRES_FOREVER); }
public function drop() { return Cache::me()->delete($this->index); }
private function uncacheClassName($className) { if (!Cache::me()->mark($className)->increment($className, 1)) { Cache::me()->mark($className)->delete($className); } }
private function checkMap($objectKey) { $pool = SemaphorePool::me(); $semKey = $this->keyToInt($this->indexKey); if (!$pool->get($semKey)) { return false; } if (!($map = Cache::me()->mark($this->className)->get($this->indexKey))) { $pool->free($semKey); return false; } if (!isset($map[$objectKey])) { $pool->free($semKey); return false; } $pool->free($semKey); return true; }
//Определяем глобальный массив $registry = array(); // Автозагрузка классов--------- function __autoload($class_name) { $file = D . '/classes/' . $class_name . '.php'; if (!is_file($file)) { return false; } include $file; } //--------------------------- //Подключение к БД include D . '/sys/config.php'; //Класс кэширования $registry['cache'] = Cache::me(); try { $db = new DebugPDO('mysql:host=' . DB_SERVER . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $db->log_on(); $db->cache =& $registry['cache']; $db->query("SET NAMES utf8;"); $registry['db'] = $db; } catch (Exception $e) { echo 'Ошибка соединения с базой данных'; exit; } //Считывание настроек из базы---------------------- $raw_config = array(); $config = array(); $raw_conf_array = array();
<?php // $Id$ require dirname(__FILE__) . '/../../global.inc.php.tpl'; // set up default cache peer Cache::setPeer(Memcached::create()); // or even several aggregated peers Cache::setPeer(AggregateCache::create()->addPeer('memcached daemon at localhost', Memcached::create())->addPeer('local low-priority file system', RubberFileSystem::create('/tmp/onphp-cache'), AggregateCache::LEVEL_VERYLOW)); // let's test out cache system $ts = new Timestamp(time()); $key = 'timestamp_object'; if (Cache::me()->set($key, $ts, 2)) { echo "object is in cache now\n"; if ($cached = Cache::me()->get($key)) { echo "got from cache:\n"; print_r($cached); } } else { echo "failed to store object in cache\n"; }
protected function getCachedByQuery(SelectQuery $query) { return Cache::me()->mark($this->className)->get($this->makeQueryKey($query, self::SUFFIX_QUERY)); }
/** * @param TestCase $test * @return DBTestCreator */ public function fillDB(TestCase $test = null) { $moscow = TestCity::create()->setName('Moscow'); $piter = TestCity::create()->setName('Saint-Peterburg'); $mysqler = TestUser::create()->setCity($moscow)->setCredentials(Credentials::create()->setNickname('mysqler')->setPassword(sha1('mysqler')))->setLastLogin(Timestamp::create(time()))->setRegistered(Timestamp::create(time())->modify('-1 day')); $postgreser = clone $mysqler; $postgreser->setCredentials(Credentials::create()->setNickName('postgreser')->setPassword(sha1('postgreser')))->setCity($piter)->setUrl(HttpUrl::create()->parse('http://postgresql.org/')); $piter = TestCity::dao()->add($piter); $moscow = TestCity::dao()->add($moscow); if ($test) { $test->assertEquals($piter->getId(), 1); $test->assertEquals($moscow->getId(), 2); } $postgreser = TestUser::dao()->add($postgreser); for ($i = 0; $i < 10; $i++) { $encapsulant = TestEncapsulant::dao()->add(TestEncapsulant::create()->setName($i)); $encapsulant->getCities()->fetch()->setList(array($piter, $moscow))->save(); } $mysqler = TestUser::dao()->add($mysqler); if ($test) { $test->assertEquals($postgreser->getId(), 1); $test->assertEquals($mysqler->getId(), 2); } if ($test) { // put them in cache now TestUser::dao()->dropIdentityMap(); TestUser::dao()->getById(1); TestUser::dao()->getById(2); if ($test instanceof DBDataTest) { $test->getListByIdsTest(); } Cache::me()->clean(); $test->assertTrue($postgreser == TestUser::dao()->getById(1)); $test->assertTrue($mysqler == TestUser::dao()->getById(2)); } $firstClone = clone $postgreser; $secondClone = clone $mysqler; $firstCount = TestUser::dao()->dropById($postgreser->getId()); $secondCount = TestUser::dao()->dropByIds(array($mysqler->getId())); if ($test) { $test->assertEquals($firstCount, 1); $test->assertEquals($secondCount, 1); try { TestUser::dao()->getById(1); $test->fail(); } catch (ObjectNotFoundException $e) { /* pass */ } $result = Criteria::create(TestUser::dao())->add(Expression::eq(1, 2))->getResult(); $test->assertEquals($result->getCount(), 0); $test->assertEquals($result->getList(), array()); } TestUser::dao()->import($firstClone); TestUser::dao()->import($secondClone); if ($test && $test instanceof DBDataTest) { // cache multi-get $test->getListByIdsTest(); $test->getListByIdsTest(); } return $this; }
protected function cacheByQuery(SelectQuery $query, $object, $expires = Cache::DO_NOT_CACHE) { if ($expires !== Cache::DO_NOT_CACHE) { Cache::me()->mark($this->className)->add($this->makeQueryKey($query, self::SUFFIX_QUERY), $object, $expires); } return $object; }
/** * @return DB **/ public function begin($level = null, $mode = null) { $begin = 'begin'; if ($level && $level instanceof IsolationLevel) { $begin .= ' ' . $level->toString(); } if ($mode && $mode instanceof AccessMode) { $begin .= ' ' . $mode->toString(); } if ($this->toQueue) { $this->queue[] = $begin; } else { $this->queryRaw("{$begin};\n"); } $this->transaction = true; $this->outOfTransactionCachePeer = Cache::me(); Cache::setPeer(Cache::me()->getRuntimeCopy()); return $this; }