Skip to content

Commit

Permalink
add unique per instance installation_id to database cache keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ahilles107 committed May 13, 2015
1 parent dfd0c99 commit 7291a14
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
22 changes: 22 additions & 0 deletions newscoop/library/Newscoop/Cache/CacheKey.php
@@ -0,0 +1,22 @@
<?php
/**
* @package Newscoop
* @copyright 2015 Sourcefabric o.p.s.
* @author Paweł Mikołąjczuk <pawel.mikolajczuk@sourcefabric.org>
* @license http://www.gnu.org/licenses/gpl-3.0.txt
*/

namespace Newscoop;

/**
* Cache key
*/
class CacheKey extends ValueObject
{
public $key;

public function __toString()
{
return $this->key;
}
}
12 changes: 9 additions & 3 deletions newscoop/library/Newscoop/Services/CacheService.php
Expand Up @@ -8,6 +8,8 @@

namespace Newscoop\Services;

use Newscoop\CacheKey;

/**
* Cache service
*/
Expand Down Expand Up @@ -87,20 +89,24 @@ public function delete($id)

public function getCacheKey($id, $namespace = null)
{
if (is_a($id, 'Newscoop\CacheKey')) {
return $id->key;
}

if (is_array($id)) {
$id = implode('__', $id);
}

// make cache key short
$id = base64_encode($id);
$id = base64_encode($id.'|'.$this->systemPreferences->installation_id);

if ($namespace) {
$namespace = $this->getNamespace($namespace);

return $namespace.'__'.$id;
}

return $id;
return new CacheKey(array('key' => $id));
}

public function getNamespace($namespace)
Expand All @@ -109,7 +115,7 @@ public function getNamespace($namespace)
return $this->getCacheDriver()->fetch($namespace);
}

$value = $namespace .'|'.time();
$value = $namespace .'|'.time().'|'.$this->systemPreferences->installation_id;
$this->getCacheDriver()->save($namespace, $value);

return $value;
Expand Down
Expand Up @@ -89,7 +89,7 @@ public function loadUsersAction(Request $request)
}

$criteria = $this->getCriteria($request);
$cacheKey = array('author_articles__'.md5(serialize($criteria)), $author->getId());
$cacheKey = $cacheService->getCacheKey(array('author_articles__'.md5(serialize($criteria)), $author->getId()));

if ($cacheService->contains($cacheKey)) {
$responseArray = $cacheService->fetch($cacheKey);
Expand Down
Expand Up @@ -70,7 +70,7 @@ public function loadUsersAction(Request $request)
$registered = $userService->countBy(array('status' => User::STATUS_ACTIVE));
$pending = $userService->countBy(array('status' => User::STATUS_INACTIVE));

$cacheKey = array('users__'.md5(serialize($criteria)), $registered, $pending);
$cacheKey = $cacheService->getCacheKey(array('users__'.md5(serialize($criteria)), $registered, $pending));

if ($cacheService->contains($cacheKey)) {
$responseArray = $cacheService->fetch($cacheKey);
Expand Down
Expand Up @@ -43,7 +43,7 @@ public function onRequest(GetResponseEvent $event)
}

$locale = $event->getRequest()->getLocale();
$cacheKey = 'oldPlugins_translations_'.count($this->pluginsService->getEnabledPlugins());
$cacheKey = $cacheService->getCacheKey('oldPlugins_translations_'.count($this->pluginsService->getEnabledPlugins()));
if ($this->cacheService->contains($cacheKey)) {
$files = $this->cacheService->fetch($cacheKey);
} else {
Expand Down

0 comments on commit 7291a14

Please sign in to comment.