1
|
|
|
<?php |
2
|
|
|
namespace Suven\FlintRedis; |
3
|
|
|
|
4
|
|
|
class FlintRedisCacheFactory |
5
|
|
|
{ |
6
|
|
|
const STRATEGY_REDIS = 1; |
7
|
|
|
const STRATEGY_FLINTSTONE = 2; |
8
|
|
|
|
9
|
|
|
private static $cachedInstances = []; |
10
|
|
|
|
11
|
|
|
private static function getKey($realm, $strategy) |
12
|
|
|
{ |
13
|
|
|
return "${realm}.${strategy}"; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
private static function getFullKey($key, $options) |
17
|
|
|
{ |
18
|
|
|
$optionsKey = md5(json_encode($options)); |
19
|
|
|
return "${key}.${optionsKey}"; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function create($realm, $strategy = false, $options = false) |
23
|
|
|
{ |
24
|
|
|
$key = self::getKey($realm, $strategy); |
25
|
|
|
$fullKey = self::getFullKey($key, $options); |
26
|
|
|
|
27
|
|
|
if ($strategy && $options && isset(self::$cachedInstances[$fullKey])) { |
28
|
|
|
return self::$cachedInstances[$fullKey]; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
if ($strategy && !$options && isset(self::$cachedInstances[$key])) { |
|
|
|
|
32
|
|
|
return self::$cachedInstances[$key]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
View Code Duplication |
if (!$strategy && !$options && isset(self::$cachedInstances[$realm])) { |
|
|
|
|
36
|
|
|
return self::$cachedInstances[$realm]; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// No instance available so far... Lets create one with default values |
40
|
|
|
$strategy = $strategy ? $strategy : self::STRATEGY_REDIS; |
41
|
|
|
$options = $options ? $options : []; |
42
|
|
|
|
43
|
|
|
$key = self::getKey($realm, $strategy); |
44
|
|
|
$fullKey = self::getFullKey($key, $options); |
45
|
|
|
|
46
|
|
|
$instance = self::newCacheInstance($strategy, $realm, $options); |
47
|
|
|
|
48
|
|
|
self::$cachedInstances[$fullKey] = &$instance; |
49
|
|
|
self::$cachedInstances[$key] = &$instance; |
50
|
|
|
self::$cachedInstances[$realm] = &$instance; |
51
|
|
|
|
52
|
|
|
return $instance; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private static function newCacheInstance($strategy, $realm, $options) { |
56
|
|
|
if ($strategy === self::STRATEGY_REDIS) { |
57
|
|
|
return new FlintRedisCacheRedis($strategy, $realm, $options); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($strategy === self::STRATEGY_FLINTSTONE) { |
61
|
|
|
return new FlintRedisCacheRedis($strategy, $realm, $options); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.