Skip to content

Commit 08e3bfe

Browse files
committed
Allow for disabling sql statement cache (configuration: PersistenceFacade.statementCaching [0|1])
1 parent af59ca7 commit 08e3bfe

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/wcmf/lib/model/ObjectQuery.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ protected function buildQuery($buildDepth, $orderby=null, PagingInfo $pagingInfo
294294
$tableName = self::processTableName($this->typeNode);
295295
$attributes = $buildDepth === false ? $mapper->getPkNames() : null;
296296
$selectStmt = $mapper->getSelectSQL(null, $tableName['alias'], $attributes, null, $pagingInfo, $this->getId());
297-
if (!$selectStmt->isCached() || !$selectStmt->getMeta('parameterCriteriaMap')) {
297+
if (!$selectStmt->isCached()) {
298298
// initialize the statement
299299
$selectStmt->quantifier(SelectStatement::QUANTIFIER_DISTINCT);
300300

src/wcmf/lib/model/mapper/SelectStatement.php

+23-11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class SelectStatement extends Select {
3737
private $adapter = null;
3838

3939
private static $logger = null;
40+
private static $isCaching = null;
4041

4142
/**
4243
* Get the SelectStatement instance with the given id.
@@ -46,18 +47,17 @@ class SelectStatement extends Select {
4647
* @return SelectStatement
4748
*/
4849
public static function get(RDBMapper $mapper, $id=self::NO_CACHE) {
49-
$cache = ObjectFactory::getInstance('staticCache');
50-
$cacheSection = self::getCacheSection($mapper->getType());
51-
$cacheId = self::getCacheId($id);
52-
if ($id == self::NO_CACHE || !$cache->exists($cacheSection, $cacheId)) {
50+
$isCaching = self::isCaching() && $id !== self::NO_CACHE;
51+
if ($isCaching) {
52+
$cache = ObjectFactory::getInstance('staticCache');
53+
$cacheSection = self::getCacheSection($mapper->getType());
54+
$cacheId = self::getCacheId($id);
55+
}
56+
if (!$isCaching || !$cache->exists($cacheSection, $cacheId)) {
5357
$selectStmt = new SelectStatement($mapper, $id);
5458
}
5559
else {
5660
$selectStmt = $cache->get($cacheSection, $cacheId);
57-
if (!$selectStmt) {
58-
$cache->remove($cacheSection, $cacheId);
59-
$selectStmt = new SelectStatement($mapper, $id);
60-
}
6161
$selectStmt->adapter = $mapper->getAdapter();
6262
}
6363
return $selectStmt;
@@ -108,7 +108,7 @@ public function getType() {
108108
*/
109109
public function isCached() {
110110
$cache = ObjectFactory::getInstance('staticCache');
111-
return $this->id == self::NO_CACHE ? false :
111+
return (!self::isCaching() || $this->id == self::NO_CACHE) ? false :
112112
$cache->exists(self::getCacheSection($this->type), self::getCacheId($this->id));
113113
}
114114

@@ -209,7 +209,7 @@ public function getRowCount() {
209209
* Put the statement into the cache
210210
*/
211211
public function save() {
212-
if ($this->id != self::NO_CACHE) {
212+
if (self::isCaching() && $this->id != self::NO_CACHE) {
213213
$cache = ObjectFactory::getInstance('staticCache');
214214
$cache->put(self::getCacheSection($this->type), self::getCacheId($this->id), $this);
215215
}
@@ -315,13 +315,25 @@ protected function getAdapter() {
315315
return $this->adapter;
316316
}
317317

318+
/**
319+
* Check if statements are cached
320+
* @return Boolean
321+
*/
322+
protected static function isCaching() {
323+
if (self::$isCaching == null) {
324+
$configuration = ObjectFactory::getInstance('configuration');
325+
self::$isCaching = !$configuration->hasValue('statementCaching', 'persistenceFacade') || $configuration->getBooleanValue('statementCaching', 'persistenceFacade') === true;
326+
}
327+
return self::$isCaching;
328+
}
329+
318330
/**
319331
* Get the cache section
320332
* @param $type The type
321333
* @return String
322334
*/
323335
protected static function getCacheSection($type) {
324-
return self::CACHE_KEY.'/'.$type;
336+
return self::CACHE_KEY.'.'.$type;
325337
}
326338

327339
/**

0 commit comments

Comments
 (0)