Skip to content

Uncachers #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 3, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions core/Cache/Cache.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public static function me()
self::$peer = $peer;
}

/**
* @return CachePeer
*/
public static function getPeer()
{
return self::$peer;
}

/* void */ public static function setDefaultWorker($worker)
{
Assert::classExists($worker);
Expand Down
40 changes: 39 additions & 1 deletion core/DB/DB.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ abstract class DB

private $queue = array();
private $toQueue = false;
/**
* @var UncachersPool
*/
private $uncacher = null;
private $outOfTransactionCachePeer = null;

abstract public function connect();
abstract public function disconnect();
Expand Down Expand Up @@ -130,6 +135,9 @@ public function begin(

$this->transaction = true;

$this->outOfTransactionCachePeer = Cache::getPeer();
Cache::setPeer(new RuntimeMemory());

return $this;
}

Expand All @@ -146,6 +154,9 @@ public function commit()
$this->transaction = false;
$this->savepointList = array();

Cache::setPeer($this->outOfTransactionCachePeer);
$this->triggerUncacher();

return $this;
}

Expand All @@ -162,6 +173,9 @@ public function rollback()
$this->transaction = false;
$this->savepointList = array();

Cache::setPeer($this->outOfTransactionCachePeer);
$this->triggerUncacher();

return $this;
}

Expand Down Expand Up @@ -401,6 +415,14 @@ public function setEncoding($encoding)
return $this;
}

public function registerUncacher(UncacherBase $uncacher)
{
$uncacher->uncache();
if ($this->inTransaction()) {
$this->getUncacher()->merge($uncacher);
}
}

/**
* @param string $savepointName
* @return DB
Expand Down Expand Up @@ -436,5 +458,21 @@ private function assertSavePointName($savepointName)
{
Assert::isEqual(1, preg_match('~^[A-Za-z][A-Za-z0-9]*$~iu', $savepointName));
}

/**
* @return UncachersPool
*/
private function getUncacher()
{
return $this->uncacher = $this->uncacher ?: UncachersPool::create();
}

private function triggerUncacher()
{
if ($this->uncacher) {
$this->uncacher->uncache();
$this->uncacher = null;
}
}
}
?>
?>
27 changes: 27 additions & 0 deletions doc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
2012-07-03 Alexey S. Denisov

* core/Cache/Cache.class.php
core/DB/DB.class.php
global.inc.php.tpl
main/DAOs/BaseDAO.class.php
main/DAOs/GenericDAO.class.php
main/DAOs/Uncachers/UncacherBase.class.php
main/DAOs/Uncachers/UncacherBaseDaoWorker.class.php
main/DAOs/Uncachers/UncacherCacheDaoWorkerLists.class.php
main/DAOs/Uncachers/UncacherCommonDaoWorker.class.php
main/DAOs/Uncachers/UncacherGenericDAO.class.php
main/DAOs/Uncachers/UncacherNullDaoWorker.class.php
main/DAOs/Uncachers/UncacherSmartDaoWorkerLists.class.php
main/DAOs/Uncachers/UncacherTaggableDaoWorker.class.php
main/DAOs/Uncachers/UncacherVoodoDaoWorkerLists.class.php
main/DAOs/Uncachers/UncachersPool.class.php
main/DAOs/Workers/BaseDaoWorker.class.php
main/DAOs/Workers/CacheDaoWorker.class.php
main/DAOs/Workers/CommonDaoWorker.class.php
main/DAOs/Workers/DalayedDropDaoWorker.class.php
main/DAOs/Workers/NullDaoWorker.class.php
main/DAOs/Workers/SmartDaoWorker.class.php
main/DAOs/Workers/VoodooDaoWorker.class.php:

Introducing Uncachers

2012-06-29 N. Konstantinov

* core/DB/PgSQL.class.php
Expand Down
1 change: 1 addition & 0 deletions global.inc.php.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
.ONPHP_MAIN_PATH.'DAOs' .PATH_SEPARATOR
.ONPHP_MAIN_PATH.'DAOs'.DIRECTORY_SEPARATOR.'Handlers'.PATH_SEPARATOR
.ONPHP_MAIN_PATH.'DAOs'.DIRECTORY_SEPARATOR.'Workers'.PATH_SEPARATOR
.ONPHP_MAIN_PATH.'DAOs'.DIRECTORY_SEPARATOR.'Uncachers'.PATH_SEPARATOR

.ONPHP_MAIN_PATH.'Flow' .PATH_SEPARATOR
.ONPHP_MAIN_PATH.'SPL' .PATH_SEPARATOR
Expand Down
6 changes: 5 additions & 1 deletion main/DAOs/BaseDAO.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ public function dropByIds(array $ids);
/// uncachers
//@{
public function uncacheById($id);
/**
* @return UncacherBase
*/
public function getUncacherById($id);
public function uncacheByIds($ids);
public function uncacheLists();
//@}
}
?>
?>
52 changes: 44 additions & 8 deletions main/DAOs/GenericDAO.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,17 +301,32 @@ public function dropByIds(array $ids)

public function uncacheById($id)
{
unset($this->identityMap[$id]);

return Cache::worker($this)->uncacheById($id);
return $this->getUncacherById($id)->uncache();
}

/**
* @return UncachersPool
*/
public function getUncacherById($id)
{
return UncacherGenericDAO::create(
$this,
$id,
Cache::worker($this)->getUncacherById($id)
);
}

public function uncacheByIds($ids)
{
if (empty($ids))
return;

$uncacher = $this->getUncacherById(array_shift($ids));

foreach ($ids as $id)
unset($this->identityMap[$id]);
$uncacher->merge($this->getUncacherById($id));

return Cache::worker($this)->uncacheByIds($ids);
return $uncacher->uncache();
}

public function uncacheLists()
Expand Down Expand Up @@ -339,6 +354,11 @@ public function dropObjectIdentityMapById($id)
return $this;
}

public function registerWorkerUncacher(UncacherBase $uncacher)
{
DBPool::getByDao($this)->registerUncacher($uncacher);
}

protected function inject(
InsertOrUpdateQuery $query,
Identifiable $object
Expand All @@ -362,19 +382,35 @@ protected function doInject(
$db = DBPool::getByDao($this);

if (!$db->isQueueActive()) {
$preUncacher = is_scalar($object->getId())
? $this->getUncacherById($object->getId())
: null;

$count = $db->queryCount($query);

$this->uncacheById($object->getId());
$uncacher = $this->getUncacherById($object->getId());
if ($preUncacher) {
$uncacher->merge($uncacher);
}
$uncacher->uncache();

if ($count !== 1)
throw new WrongStateException(
$count.' rows affected: racy or insane inject happened: '
.$query->toDialectString($db->getDialect())
);
} else {
$preUncacher = is_scalar($object->getId())
? $this->getUncacherById($object->getId())
: null;

$db->queryNull($query);

$this->uncacheById($object->getId());
$uncacher = $this->getUncacherById($object->getId());
if ($preUncacher) {
$uncacher->merge($uncacher);
}
$uncacher->uncache();
}

// clean out Identifier, if any
Expand Down Expand Up @@ -403,4 +439,4 @@ private function addObjectListToMap($list)
return $list;
}
}
?>
?>
25 changes: 25 additions & 0 deletions main/DAOs/Uncachers/UncacherBase.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/***************************************************************************
* Copyright (C) 2012 by Aleksey S. Denisov *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/

/**
* @ingroup Uncachers
**/
interface UncacherBase
{
/**
* @param $uncacher BaseUncacher same as self class
* @return BaseUncacher (this)
*/
public function merge(UncacherBase $uncacher);

public function uncache();
}
?>
80 changes: 80 additions & 0 deletions main/DAOs/Uncachers/UncacherBaseDaoWorker.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/***************************************************************************
* Copyright (C) 2012 by Aleksey S. Denisov *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of the *
* License, or (at your option) any later version. *
* *
***************************************************************************/

/**
* @ingroup Uncachers
**/
class UncacherBaseDaoWorker implements UncacherBase
{
private $classNameMap = array();

/**
* @return UncacherBaseDaoWorker
*/
public static function create($className, $idKey)
{
return new self($className, $idKey);
}

public function __construct($className, $idKey)
{
$this->classNameMap[$className] = array($idKey);
}

public function getClassNameMap()
{
return $this->classNameMap;
}

/**
* @param $uncacher UncacherNullDaoWorker same as self class
* @return BaseUncacher (this)
*/
public function merge(UncacherBase $uncacher)
{
Assert::isInstance($uncacher, get_class($this));
return $this->mergeSelf($uncacher);
}

public function uncache()
{
foreach ($this->classNameMap as $className => $idKeys) {
foreach ($idKeys as $key) {
$this->uncacheClassName($className, $idKeys);
}
}
}

protected function uncacheClassName($className, $idKeys) {
foreach ($idKeys as $key)
Cache::me()->mark($className)->delete($key);
}

/**
* @param UncacherBaseDaoWorker $uncacher
* @return UncacherBaseDaoWorker
*/
private function mergeSelf(UncacherBaseDaoWorker $uncacher)
{
foreach ($uncacher->getClassNameMap() as $className => $idKeys) {
if (isset($this->classNameMap[$className])) {
$this->classNameMap[$className] = ArrayUtils::mergeUnique(
$this->classNameMap[$className],
$idKeys
);
} else {
$this->classNameMap[$className] = $idKeys;
}
}
return $this;
}
}
?>
Loading