1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Tebru\Gson\Internal; |
6
|
|
|
|
7
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
8
|
|
|
use Psr\SimpleCache\CacheInterface; |
9
|
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter; |
10
|
|
|
use Symfony\Component\Cache\Adapter\ChainAdapter; |
11
|
|
|
use Symfony\Component\Cache\Adapter\NullAdapter; |
12
|
|
|
use Symfony\Component\Cache\Adapter\PhpFilesAdapter; |
13
|
|
|
use Symfony\Component\Cache\Exception\CacheException; |
14
|
|
|
use Symfony\Component\Cache\Psr16Cache; |
15
|
|
|
use Symfony\Component\Cache\Simple\ArrayCache; |
16
|
|
|
use Symfony\Component\Cache\Simple\ChainCache; |
17
|
|
|
use Symfony\Component\Cache\Simple\NullCache; |
18
|
|
|
use Symfony\Component\Cache\Simple\PhpFilesCache; |
19
|
|
|
|
20
|
|
|
final class CacheProvider |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
private $symfonyGte43; |
26
|
|
|
|
27
|
3 |
|
public function __construct() |
28
|
|
|
{ |
29
|
3 |
|
$this->symfonyGte43 = class_exists('Symfony\Component\Cache\Psr16Cache'); |
30
|
3 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return CacheInterface |
34
|
|
|
*/ |
35
|
3 |
|
public function provideNullCacheAsSimpleCache() |
36
|
|
|
{ |
37
|
3 |
|
if ($this->symfonyGte43) { |
38
|
3 |
|
return new Psr16Cache(new NullAdapter()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return new NullCache(); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param int $defaultLifetime |
46
|
|
|
* @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise |
47
|
|
|
* @return CacheInterface |
48
|
|
|
*/ |
49
|
3 |
|
public function provideArrayCacheAsSimpleCache(int $defaultLifetime = 0, bool $storeSerialized = true) |
50
|
|
|
{ |
51
|
3 |
|
if ($this->symfonyGte43) { |
52
|
3 |
|
return new Psr16Cache(new ArrayAdapter($defaultLifetime, $storeSerialized)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return new ArrayCache($defaultLifetime, $storeSerialized); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param int $defaultLifetime |
60
|
|
|
* @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise |
61
|
|
|
* @return CacheItemPoolInterface|CacheInterface |
62
|
|
|
*/ |
63
|
|
|
public function providerArrayCacheAsCacheItemPool(int $defaultLifetime = 0, bool $storeSerialized = true) |
64
|
|
|
{ |
65
|
|
|
if ($this->symfonyGte43) { |
66
|
|
|
return new ArrayAdapter($defaultLifetime, $storeSerialized); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return new ArrayCache($defaultLifetime, $storeSerialized); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param CacheItemPoolInterface[] $caches The ordered list of caches used to fetch cached items |
74
|
|
|
* @param int $defaultLifetime The lifetime of items propagated from lower caches to upper ones |
75
|
|
|
* @return CacheInterface |
76
|
|
|
*/ |
77
|
|
|
public function provideChainCacheAsSimpleCache(array $caches, int $defaultLifetime = 0) |
78
|
|
|
{ |
79
|
|
|
if ($this->symfonyGte43) { |
80
|
|
|
return new Psr16Cache(new ChainAdapter($caches, $defaultLifetime)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return new ChainCache($caches, $defaultLifetime); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// /** |
87
|
|
|
// * @param string $namespace |
88
|
|
|
// * @param int $defaultLifetime |
89
|
|
|
// * @param string|null $directory |
90
|
|
|
// * @param bool $appendOnly Set to `true` to gain extra performance when the items stored in this pool never expire. |
91
|
|
|
// * Doing so is encouraged because it fits perfectly OPcache's memory model. |
92
|
|
|
// * |
93
|
|
|
// * @return CacheInterface |
94
|
|
|
// * @throws CacheException |
95
|
|
|
// */ |
96
|
|
|
// public function providePhpFilesCacheAsSimpleCache(string $namespace = '', int $defaultLifetime = 0, string $directory = null, bool $appendOnly = false) |
97
|
|
|
// { |
98
|
|
|
// if ($this->symfonyGte43) { |
99
|
|
|
// return new Psr16Cache(new PhpFilesAdapter($namespace, $defaultLifetime, $directory, $appendOnly)); |
100
|
|
|
// } |
101
|
|
|
// |
102
|
|
|
// return new PhpFilesCache($namespace, $defaultLifetime, $directory, $appendOnly); |
103
|
|
|
// } |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $namespace |
107
|
|
|
* @param int $defaultLifetime |
108
|
|
|
* @param string|null $directory |
109
|
|
|
* @param bool $appendOnly Set to `true` to gain extra performance when the items stored in this pool never expire. |
110
|
|
|
* Doing so is encouraged because it fits perfectly OPcache's memory model. |
111
|
|
|
* |
112
|
|
|
* @return CacheItemPoolInterface|CacheInterface |
113
|
|
|
* @throws CacheException |
114
|
|
|
*/ |
115
|
|
|
public function providePhpFilesCacheAsCacheItemPool(string $namespace = '', int $defaultLifetime = 0, string $directory = null, bool $appendOnly = false) |
116
|
|
|
{ |
117
|
|
|
if ($this->symfonyGte43) { |
118
|
|
|
return new PhpFilesAdapter($namespace, $defaultLifetime, $directory, $appendOnly); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return new PhpFilesCache($namespace, $defaultLifetime, $directory, $appendOnly); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|