silverstripe-framework/tests/php/Security/InheritedPermissionsFlusherTest/TestCacheFlusher.php

69 lines
1.4 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Security\Tests\InheritedPermissionsFlusherTest;
use Psr\SimpleCache\CacheInterface;
2017-12-07 04:07:40 +01:00
use SilverStripe\Core\Cache\MemberCacheFlusher;
2017-12-07 04:07:40 +01:00
class TestCacheFlusher implements MemberCacheFlusher
{
/**
* @var array
*/
public static $categories = [
'apples',
'pears',
'bananas',
];
/**
* @var CacheInterface
*/
public $cache;
/**
* TestCacheFlusher constructor.
* @param CacheInterface $cache
*/
public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
}
/**
* Clear the cache for this instance only
2017-12-11 04:46:21 +01:00
*
2017-12-07 04:07:40 +01:00
* @param array $memberIDs A list of member IDs
*/
2017-12-07 04:07:40 +01:00
public function flushMemberCache($memberIDs = null)
{
if (!$this->cache) {
return;
}
// Hard flush, e.g. flush=1
2017-12-07 04:07:40 +01:00
if (!$memberIDs) {
$this->cache->clear();
}
2017-12-07 04:07:40 +01:00
if ($memberIDs && is_array($memberIDs)) {
foreach (self::$categories as $category) {
2017-12-07 04:07:40 +01:00
foreach ($memberIDs as $memberID) {
$key = $this->generateCacheKey($category, $memberID);
$this->cache->delete($key);
}
}
}
}
/**
* @param $category
* @param $memberID
* @return string
*/
public function generateCacheKey($category, $memberID)
{
return "{$category}__{$memberID}";
}
2017-12-11 04:46:21 +01:00
}