|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Core\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
6
|
|
|
use Symfony\Component\Cache\Simple\FilesystemCache; |
|
7
|
|
|
use Symfony\Component\Cache\Simple\ApcuCache; |
|
8
|
|
|
use Symfony\Component\Cache\Simple\ChainCache; |
|
9
|
|
|
use Symfony\Component\Cache\Simple\PhpFilesCache; |
|
10
|
|
|
use Symfony\Component\Cache\Adapter\ApcuAdapter; |
|
11
|
|
|
use Symfony\Component\Cache\Adapter\PhpFilesAdapter; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Returns the most performant combination of caches available on the system: |
|
15
|
|
|
* - `PhpFilesCache` (PHP 7 with opcache enabled) |
|
16
|
|
|
* - `ApcuCache` (requires APC) with a `FilesystemCache` fallback (for larger cache volumes) |
|
17
|
|
|
* - `FilesystemCache` if none of the above is available |
|
18
|
|
|
* |
|
19
|
|
|
* Modelled after `Symfony\Component\Cache\Adapter\AbstractAdapter::createSystemCache()` |
|
20
|
|
|
*/ |
|
21
|
|
|
class DefaultCacheFactory implements CacheFactory |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string Absolute directory path |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $directory; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string APC version for apcu_add() |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $version; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $directory |
|
36
|
|
|
* @param string $version |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct($directory, $version = null) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->directory = $directory; |
|
41
|
|
|
$this->version = $version; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @inheritdoc |
|
46
|
|
|
*/ |
|
47
|
|
|
public function create($service, array $params = array()) |
|
48
|
|
|
{ |
|
49
|
|
|
$namespace = (isset($args['namespace'])) ? $args['namespace'] : ''; |
|
|
|
|
|
|
50
|
|
|
$defaultLifetime = (isset($args['defaultLifetime'])) ? $args['defaultLifetime'] : 0; |
|
51
|
|
|
$version = $this->version; |
|
52
|
|
|
$directory = $this->directory; |
|
53
|
|
|
|
|
54
|
|
|
$apcuSupported = null; |
|
55
|
|
|
$phpFilesSupported = null; |
|
56
|
|
|
|
|
57
|
|
|
if (null === $apcuSupported) { |
|
58
|
|
|
$apcuSupported = ApcuAdapter::isSupported(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (!$apcuSupported && null === $phpFilesSupported) { |
|
|
|
|
|
|
62
|
|
|
$phpFilesSupported = PhpFilesAdapter::isSupported(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if ($phpFilesSupported) { |
|
66
|
|
|
$opcache = Injector::inst()->create(PhpFilesCache::class, false, [$namespace, $defaultLifetime, $directory]); |
|
67
|
|
|
return $opcache; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$fs = Injector::inst()->create(FilesystemCache::class, false, [$namespace, $defaultLifetime, $directory]); |
|
71
|
|
|
if (!$apcuSupported) { |
|
|
|
|
|
|
72
|
|
|
return $fs; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$apcu = Injector::inst()->create(ApcuCache::class, false, [$namespace, (int) $defaultLifetime / 5, $version]); |
|
76
|
|
|
|
|
77
|
|
|
return Injector::inst()->create(ChainCache::class, false, [[$apcu, $fs]]); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
This check looks for calls to
isset(...)orempty()on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.