mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUG Fix ApcuCache and MemCache namespace
This commit is contained in:
parent
0c52ea067c
commit
8b9f41d4f4
@ -27,9 +27,13 @@ class ApcuCacheFactory implements CacheFactory
|
|||||||
*/
|
*/
|
||||||
public function create($service, array $params = array())
|
public function create($service, array $params = array())
|
||||||
{
|
{
|
||||||
return Injector::inst()->create(ApcuCache::class, false, [
|
$namespace = isset($params['namespace'])
|
||||||
(isset($args['namespace'])) ? $args['namespace'] : '',
|
? $params['namespace'] . '_' . md5(BASE_PATH)
|
||||||
(isset($args['defaultLifetime'])) ? $args['defaultLifetime'] : 0,
|
: md5(BASE_PATH);
|
||||||
|
$defaultLifetime = isset($params['defaultLifetime']) ? $params['defaultLifetime'] : 0;
|
||||||
|
return Injector::inst()->createWithArgs(ApcuCache::class, [
|
||||||
|
$namespace,
|
||||||
|
$defaultLifetime,
|
||||||
$this->version
|
$this->version
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
@ -71,7 +71,8 @@ class DefaultCacheFactory implements CacheFactory
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Chain this cache with ApcuCache
|
// Chain this cache with ApcuCache
|
||||||
$apcu = $this->createCache(ApcuCache::class, [$namespace, (int) $defaultLifetime / 5, $version]);
|
$apcuNamespace = $namespace . ($namespace ? '_' : '') . md5(BASE_PATH);
|
||||||
|
$apcu = $this->createCache(ApcuCache::class, [$apcuNamespace, (int) $defaultLifetime / 5, $version]);
|
||||||
return $this->createCache(ChainCache::class, [[$apcu, $fs]]);
|
return $this->createCache(ChainCache::class, [[$apcu, $fs]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ class MemcachedCacheFactory implements CacheFactory
|
|||||||
/**
|
/**
|
||||||
* @param Memcached $memcachedClient
|
* @param Memcached $memcachedClient
|
||||||
*/
|
*/
|
||||||
public function __construct(Memcached $memcachedClient)
|
public function __construct(Memcached $memcachedClient = null)
|
||||||
{
|
{
|
||||||
$this->memcachedClient = $memcachedClient;
|
$this->memcachedClient = $memcachedClient;
|
||||||
}
|
}
|
||||||
@ -27,10 +27,14 @@ class MemcachedCacheFactory implements CacheFactory
|
|||||||
*/
|
*/
|
||||||
public function create($service, array $params = array())
|
public function create($service, array $params = array())
|
||||||
{
|
{
|
||||||
return Injector::inst()->create(MemcachedCache::class, false, [
|
$namespace = isset($params['namespace'])
|
||||||
|
? $params['namespace'] . '_' . md5(BASE_PATH)
|
||||||
|
: md5(BASE_PATH);
|
||||||
|
$defaultLifetime = isset($params['defaultLifetime']) ? $params['defaultLifetime'] : 0;
|
||||||
|
return Injector::inst()->createWithArgs(MemcachedCache::class, [
|
||||||
$this->memcachedClient,
|
$this->memcachedClient,
|
||||||
(isset($args['namespace'])) ? $args['namespace'] : '',
|
$namespace,
|
||||||
(isset($args['defaultLifetime'])) ? $args['defaultLifetime'] : 0
|
$defaultLifetime
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
88
tests/php/Core/Cache/CacheTest.php
Normal file
88
tests/php/Core/Cache/CacheTest.php
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\Core\Tests\Cache;
|
||||||
|
|
||||||
|
use Psr\SimpleCache\CacheInterface;
|
||||||
|
use SilverStripe\Core\Cache\ApcuCacheFactory;
|
||||||
|
use SilverStripe\Core\Cache\MemcachedCacheFactory;
|
||||||
|
use SilverStripe\Core\Config\Config;
|
||||||
|
use SilverStripe\Core\Injector\Injector;
|
||||||
|
use SilverStripe\Core\Test\Cache\CacheTest\MockCache;
|
||||||
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
use Symfony\Component\Cache\Simple\ApcuCache;
|
||||||
|
use Symfony\Component\Cache\Simple\MemcachedCache;
|
||||||
|
|
||||||
|
class CacheTest extends SapphireTest
|
||||||
|
{
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
Config::modify()
|
||||||
|
->set(
|
||||||
|
Injector::class,
|
||||||
|
ApcuCacheFactory::class,
|
||||||
|
[
|
||||||
|
'constructor' => [ 'version' => 4400 ]
|
||||||
|
]
|
||||||
|
)
|
||||||
|
->set(
|
||||||
|
Injector::class,
|
||||||
|
CacheInterface::class . '.TestApcuCache',
|
||||||
|
[
|
||||||
|
'factory' => ApcuCacheFactory::class,
|
||||||
|
'constructor' => [
|
||||||
|
'namespace' => 'TestApcuCache',
|
||||||
|
'defaultLifetime' => 2600,
|
||||||
|
],
|
||||||
|
]
|
||||||
|
)
|
||||||
|
->set(
|
||||||
|
Injector::class,
|
||||||
|
CacheInterface::class . '.TestMemcache',
|
||||||
|
[
|
||||||
|
'factory' => MemcachedCacheFactory::class,
|
||||||
|
'constructor' => [
|
||||||
|
'namespace' => 'TestMemCache',
|
||||||
|
'defaultLifetime' => 5600,
|
||||||
|
],
|
||||||
|
]
|
||||||
|
)
|
||||||
|
->set(Injector::class, ApcuCache::class, MockCache::class)
|
||||||
|
->set(Injector::class, MemcachedCache::class, MockCache::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testApcuCacheFactory()
|
||||||
|
{
|
||||||
|
$cache = Injector::inst()->get(CacheInterface::class .'.TestApcuCache');
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
MockCache::class,
|
||||||
|
$cache
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
[
|
||||||
|
'TestApcuCache_'.md5(BASE_PATH),
|
||||||
|
2600,
|
||||||
|
4400
|
||||||
|
],
|
||||||
|
$cache->getArgs()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMemCacheFactory()
|
||||||
|
{
|
||||||
|
$cache = Injector::inst()->get(CacheInterface::class .'.TestMemcache');
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
MockCache::class,
|
||||||
|
$cache
|
||||||
|
);
|
||||||
|
$this->assertEquals(
|
||||||
|
[
|
||||||
|
null,
|
||||||
|
'TestMemCache_'.md5(BASE_PATH),
|
||||||
|
5600
|
||||||
|
],
|
||||||
|
$cache->getArgs()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
25
tests/php/Core/Cache/CacheTest/MockCache.php
Normal file
25
tests/php/Core/Cache/CacheTest/MockCache.php
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\Core\Test\Cache\CacheTest;
|
||||||
|
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
|
||||||
|
class MockCache implements TestOnly
|
||||||
|
{
|
||||||
|
protected $args = [];
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->args = func_get_args();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get constructor args
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getArgs()
|
||||||
|
{
|
||||||
|
return $this->args;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user