Skip to content

Commit

Permalink
Merge pull request #54 from svycka/feature/allow-custom-cache-tags
Browse files Browse the repository at this point in the history
Add ability to set custom cache tags
  • Loading branch information
bramstroker committed Nov 20, 2017
2 parents 4d8d0f6 + c7a0b00 commit 6ce0628
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 8 deletions.
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -221,6 +221,19 @@ The events are listed as constants in the [CacheEvent](https://github.com/bramst

### Examples

Setting custom tags example

```php
public function onBootstrap(MvcEvent $e)
{
$serviceManager = $e->getApplication()->getServiceManager();
$cacheService = $serviceManager->get('strokercache_service');
$cacheService->getEventManager()->attach(CacheEvent::EVENT_SAVE, function (CacheEvent $e) {
$e->setTags(['custom_tag']);
});
}
```

Log to file whenever a page is written to the cache storage

```php
Expand Down
20 changes: 20 additions & 0 deletions src/Event/CacheEvent.php
Expand Up @@ -26,6 +26,11 @@ class CacheEvent extends Event
*/
protected $mvcEvent;

/**
* @var array
*/
protected $tags = [];

/**
* @return string
*/
Expand Down Expand Up @@ -64,4 +69,19 @@ public function setMvcEvent($mvcEvent)
return $this;
}

/**
* @return array
*/
public function getTags()
{
return $this->tags;
}

/**
* @param array $tags
*/
public function setTags(array $tags)
{
$this->tags = $tags;
}
}
27 changes: 19 additions & 8 deletions src/Service/CacheService.php
Expand Up @@ -107,7 +107,8 @@ public function save(MvcEvent $mvcEvent)

$cacheStorage = $this->getCacheStorage();
if ($cacheStorage instanceof TaggableInterface) {
$cacheStorage->setTags($id, $this->getTags($mvcEvent));
$tags = array_unique(array_merge($this->getTags($mvcEvent), $cacheEvent->getTags()));
$cacheStorage->setTags($id, $this->prefixTags($tags));
}
}

Expand Down Expand Up @@ -144,10 +145,7 @@ public function clearByTags(array $tags = array(), $disjunction = null)
if (!$cacheStorage instanceof TaggableInterface) {
throw new UnsupportedAdapterException('purging by tags is only supported on adapters implementing the TaggableInterface');
}
$tags = array_map(
function ($tag) { return CacheService::TAG_PREFIX . $tag; },
$tags
);
$tags = $this->prefixTags($tags);

return $cacheStorage->clearByTags($tags, $disjunction);
}
Expand All @@ -162,19 +160,32 @@ public function getTags(MvcEvent $event)
{
$routeName = $event->getRouteMatch()->getMatchedRouteName();
$tags = [
self::TAG_PREFIX . 'route_' . $routeName
'route_' . $routeName
];
foreach ($event->getRouteMatch()->getParams() as $key => $value) {
if ($key == 'controller') {
$tags[] = self::TAG_PREFIX . 'controller_' . $value;
$tags[] = 'controller_' . $value;
} else {
$tags[] = self::TAG_PREFIX . 'param_' . $key . '_' . $value;
$tags[] = 'param_' . $key . '_' . $value;
}
}

return $tags;
}

/**
* @param array $tags
*
* @return array
*/
private function prefixTags(array $tags)
{
return array_map(
function ($tag) { return CacheService::TAG_PREFIX . $tag; },
$tags
);
}

/**
* @param string $eventName
* @param string $cacheKey
Expand Down
31 changes: 31 additions & 0 deletions tests/Service/CacheServiceTest.php
Expand Up @@ -215,6 +215,37 @@ public function testSaveGeneratesCorrectTags()
$this->cacheService->save($this->getMvcEvent());
}

public function testSaveGeneratesCustomTags()
{
$expectedTags = array(
'strokercache_route_home',
'strokercache_controller_myTestController',
'strokercache_param_someParam_someValue',
'strokercache_custom_tag',
);

$this->getMvcEvent()->getRouteMatch()->setMatchedRouteName('home');
$this->getMvcEvent()->getRouteMatch()->setParam('controller', 'myTestController');
$this->getMvcEvent()->getRouteMatch()->setParam('someParam', 'someValue');

// Storage mock should implement the TaggableInterface
$storageMock = Mockery::mock('Zend\Cache\Storage\TaggableInterface')
->shouldReceive('setItem')
->once()
->shouldReceive('setTags')
->once()
->with('/foo/bar', $expectedTags)
->getMock();
$this->cacheService->setCacheStorage($storageMock);

$this->cacheService->getEventManager()->attach(CacheEvent::EVENT_SHOULDCACHE, function () { return true; });
$this->cacheService->getEventManager()->attach(CacheEvent::EVENT_SAVE, function ($event) {
$event->setTags(['custom_tag']);
});

$this->cacheService->save($this->getMvcEvent());
}

public function testClearByTags()
{
$tags = ['foo', 'bar'];
Expand Down

0 comments on commit 6ce0628

Please sign in to comment.