Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a Doctrine caching adapter.

  • Loading branch information...
commit 5051e865e2a4d7a1d792318924dd824643c325b4 1 parent 60acb46
@jhallbachner authored
View
82 Adapters/DoctrineAdapter.php
@@ -0,0 +1,82 @@
+<?php
+
+namespace Tedivm\StashBundle\Adapters;
+use Stash\Cache as StashCache;
+use Stash\Handlers;
+use Doctrine\Common\Cache\Cache as DoctrineCacheInterface;
+
+class DoctrineAdapter implements DoctrineCacheInterface
+{
+ protected $cacheService;
+
+ protected $caches = array();
+
+ public function __construct($cacheService)
+ {
+ $this->cacheService = $cacheService;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function fetch($id)
+ {
+ if(isset($this->caches[$id])) {
+ $cache = $this->caches[$id];
+ unset($this->caches[$id]);
+ } else {
+ $cache = $this->cacheService->get($id);
+ }
+
+ if($cache->isMiss()) {
+ return false;
+ } else {
+ return $cache->get();
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function contains($id)
+ {
+ $this->caches[$id] = $this->cacheService->get($id);
+
+ return $this->caches[$id]->isMiss();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function save($id, $data, $lifeTime = 0)
+ {
+ $cache = $this->cacheService->get($id);
+
+ return $cache->set($data, $lifeTime);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function delete($id)
+ {
+ $cache = $this->cacheService->get($id);
+ return $cache->clear();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getStats()
+ {
+ $stats = array();
+ $logger = $this->cacheService->getLogger();
+ $stats['hits'] = $logger->getHits();
+ $stats['misses'] = $logger->getCalls() - $stats['hits'];
+
+ return $stats;
+ }
+
+
+
+}
View
1  DependencyInjection/Configuration.php
@@ -98,6 +98,7 @@ protected function getCachesNode()
->end()
->end()
->end()
+ ->booleanNode('registerDoctrineAdapter')->defaultFalse()->end()
;
foreach($handlers as $handler) {
View
13 DependencyInjection/TedivmStashExtension.php
@@ -48,6 +48,9 @@ protected function addCacheService($name, $cache, $container)
$handlers = $cache['handlers'];
unset($cache['handlers']);
+ $doctrine = $cache['registerDoctrineAdapter'];
+ unset($cache['registerDoctrineAdapter']);
+
$container
->setDefinition(sprintf('stash.handler.%s_cache', $name), new DefinitionDecorator('stash.handler'))
->setArguments(array(
@@ -75,6 +78,16 @@ protected function addCacheService($name, $cache, $container)
->setAbstract(false)
;
+ if(interface_exists("\\Doctrine\\Common\\Cache\\Cache") && $doctrine) {
+ $container
+ ->setDefinition(sprintf('stash.adapter.doctrine.%s_cache', $name), new DefinitionDecorator('stash.adapter.doctrine'))
+ ->setArguments(array(
+ new Reference(sprintf('stash.%s_cache', $name))
+ ))
+ ->setAbstract(false)
+ ;
+ }
+
$container
->getDefinition('data_collector.stash')
->addMethodCall('addLogger', array(
View
4 Resources/config/services.yml
@@ -3,6 +3,7 @@ parameters:
stash.logger.class: Tedivm\StashBundle\Service\CacheLogger
stash.handler.class: Tedivm\Stash\Handler\HandlerInterface
stash.factory.class: Tedivm\StashBundle\Factory\HandlerFactory
+ stash.adapter.doctrine.class: Tedivm\StashBundle\Adapters\DoctrineAdapter
services:
stash.cache:
@@ -17,6 +18,9 @@ services:
synthetic: true
factory_method: createHandler
factory_class: %stash.factory.class%
+ stash.adapter.doctrine:
+ class: %stash.adapter.doctrine.class%
+ abstract: true
data_collector.stash:
class: Tedivm\StashBundle\Collector\CacheDataCollector
arguments: [ %stash.default_cache%, %stash.caches%, %stash.caches.options% ]
View
10 Service/CacheService.php
@@ -90,4 +90,14 @@ public function getHandlers()
{
return Handlers::getHandlers();
}
+
+ /**
+ * Returns the current logger
+ *
+ * @return CacheLogger
+ */
+ public function getLogger()
+ {
+ return $this->logger;
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.