Skip to content
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

Rework the bundle to provide multiple services #3

Merged
merged 12 commits into from
Oct 12, 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
121 changes: 121 additions & 0 deletions Adapters/DoctrineAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?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 $namespace = '';

protected $caches = array();

public function __construct($cacheService)
{
$this->cacheService = $cacheService;
}

public function setNamespace($namespace)
{
$this->namespace = $namespace;
}

public function getNamespace()
{
return $this->namespace;
}

/**
* {@inheritDoc}
*/
public function fetch($id)
{
$id = $this->normalizeId($id);

if(isset($this->caches[$id])) {
$cache = $this->caches[$id];
unset($this->caches[$id]);
} else {
$cache = $this->cacheService->get($id);
}


$value = $cache->get();
if($cache->isMiss()) {
return false;
} else {
return $value;
}
}

/**
* {@inheritDoc}
*/
public function contains($id)
{
$id = $this->normalizeId($id);

$this->caches[$id] = $this->cacheService->get($id);

return $this->caches[$id]->isMiss();
}

/**
* {@inheritDoc}
*/
public function save($id, $data, $lifeTime = 0)
{
$id = $this->normalizeId($id);

$cache = $this->cacheService->get($id);

return $cache->set($data, $lifeTime);
}

/**
* {@inheritDoc}
*/
public function delete($id)
{
$id = $this->normalizeId($id);

return $this->cacheService->clear($id);
}

public function deleteAll()
{
return $this->flushAll();
}

/**
* {@inheritDoc}
*/
public function getStats()
{
$stats = array();
$logger = $this->cacheService->getLogger();
$stats['hits'] = $logger->getHits();
$stats['misses'] = $logger->getCalls() - $stats['hits'];

return $stats;
}

public function flushAll()
{
$this->delete('');
}

protected function normalizeId($id)
{
if(isset($this->namespace)) {
$id = sprintf('zz_%s_zz/%s', $this->namespace, $id);
$id = trim($id, '/');
return $id;
} else {
return $id;
}
}
}
160 changes: 78 additions & 82 deletions Collector/CacheDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,86 +16,82 @@
*/
class CacheDataCollector extends DataCollector
{

protected $handlerType;
protected $handlerOptions;

public function __construct($type, $options)
{
$this->handlerType = $type;
$this->handlerOptions = $options;
}

public function collect(Request $request, Response $response, \Exception $exception = null)
{
$record = Cache::$queryRecord;
if(!isset($record))
$record = array();

$data = array();
foreach($record as $query => $calls)
$data[$query] = array('calls' => count($calls), 'returns' => array_sum($calls));

$info = array('calls' => Cache::$cacheCalls, 'returns' => Cache::$cacheReturns, 'record' => $data);
$info['handlerType'] = $this->handlerType;
$info['handlerOptions'] = $this->handlerOptions;
$handlers = Handlers::getHandlers();
foreach($handlers as $handler) {
$pieces = explode('\\', $handler);
$info['availableHandlers'][] = array_pop($pieces);
}
$info['availableHandlers'] = join(', ', $info['availableHandlers']);

if($this->handlerType === 'MultiHandler') {
$handlers = $info['handlerOptions']['handlers'];
$info['handlerOptions']['handlers'] = join(', ', $handlers);

foreach($handlers as $h) {
$info['subhandlerOptions'][$h] = isset($info['handlerOptions'][$h]) ? $info['handlerOptions'][$h] : array();
unset($info['handlerOptions'][$h]);
}
}

$this->data = $info;
}

public function getCalls()
{
return $this->data['calls'];
}

public function getReturns()
{
return $this->data['returns'];
}

public function getRecord()
{
return $this->data['record'];
}

public function getHandlertype()
{
return $this->data['handlerType'];
}

public function getHandleroptions()
{
return $this->data['handlerOptions'];
}

public function getSubhandleroptions()
{
return $this->data['subhandlerOptions'];
}

public function gethandlers()
{
return $this->data['availableHandlers'];
}

public function getname()
{
return 'stash';
}
protected $defaultCache;
protected $cacheNames;
protected $cacheOptions;

protected $loggers = array();

public function __construct($default, $caches, $options)
{
$this->defaultCache = $default;
$this->cacheNames = $caches;
$this->cacheOptions = $options;
}

public function addLogger($logger)
{
$this->loggers[] = $logger;
}

public function collect(Request $request, Response $response, \Exception $exception = null)
{
$info = array('calls' => 0, 'hits' => 0);
foreach($this->loggers as $logger) {
$name = $logger->getName();
$calls = $logger->getCalls();
$hits = $logger->getHits();

$info['calls'] += $calls;
$info['hits'] += $hits;

$info['caches'][$name]['options'] = $this->cacheOptions[$name];
$info['caches'][$name]['queries'] = $logger->getQueries();
$info['caches'][$name]['calls'] = $calls;
$info['caches'][$name]['hits'] = $hits;
}

$handlers = Handlers::getHandlers();
foreach($handlers as $handler) {
$pieces = explode('\\', $handler);
$name = array_pop($pieces);
if(!in_array($name, array('Ephemeral', 'MultiHandler'))) {
$info['availableHandlers'][] = $name;
}
}

$info['default'] = $this->defaultCache;

$this->data = $info;
}

public function getCalls()
{
return $this->data['calls'];
}

public function getHits()
{
return $this->data['hits'];
}

public function gethandlers()
{
return $this->data['availableHandlers'];
}

public function getCaches()
{
return $this->data['caches'];
}

public function getDefault()
{
return $this->data['default'];
}

public function getname()
{
return 'stash';
}
}
Loading