|
1
|
|
|
<?php |
|
2
|
|
|
namespace Suven\FlintRedis; |
|
3
|
|
|
|
|
4
|
|
|
abstract class FlintRedisCacheFactory |
|
5
|
|
|
{ |
|
6
|
|
|
const STRATEGY_REDIS = 1; |
|
7
|
|
|
const STRATEGY_FLINTSTONE = 2; |
|
8
|
|
|
|
|
9
|
|
|
private static $cachedInstances = []; |
|
10
|
|
|
|
|
11
|
15 |
|
private static function getKey($realm, $strategy) |
|
12
|
|
|
{ |
|
13
|
15 |
|
return "${realm}.${strategy}"; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
private static function getFullKey($key, $options) |
|
17
|
|
|
{ |
|
18
|
15 |
|
$optionsKey = md5(json_encode($options)); |
|
19
|
15 |
|
return "${key}.${optionsKey}"; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Retrieves a new or previously created cache-instance for the provided |
|
24
|
|
|
* parameters. |
|
25
|
|
|
* |
|
26
|
|
|
* If you previously created an instance for a collection 'foo' with a |
|
27
|
|
|
* strategy [and options] and are later 'creating' in instance but only |
|
28
|
|
|
* provide the realm 'foo', you will retreive your previously created instance. |
|
29
|
|
|
* |
|
30
|
|
|
* That way you only have to provide the strategy and options once per $realm. |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $realm Your collections name |
|
33
|
|
|
* @param number $strategy Either FlintRedisCacheFactory::STRATEGY_REDIS |
|
34
|
|
|
* or FlintRedisCacheFactory:: STRATEGY_FLINTSTONE |
|
35
|
|
|
* @param array $options Options you want to pass to predis/flintstone |
|
36
|
|
|
* @return FlintRedisCache |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function create($realm, $strategy = false, $options = false) |
|
39
|
|
|
{ |
|
40
|
15 |
|
$key = self::getKey($realm, $strategy); |
|
41
|
15 |
|
$fullKey = self::getFullKey($key, $options); |
|
42
|
|
|
|
|
43
|
15 |
|
if ($strategy && $options && isset(self::$cachedInstances[$fullKey])) { |
|
44
|
3 |
|
return self::$cachedInstances[$fullKey]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
15 |
View Code Duplication |
if ($strategy && !$options && isset(self::$cachedInstances[$key])) { |
|
48
|
6 |
|
return self::$cachedInstances[$key]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
15 |
View Code Duplication |
if (!$strategy && !$options && isset(self::$cachedInstances[$realm])) { |
|
52
|
9 |
|
return self::$cachedInstances[$realm]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// No instance available so far... Lets create one with default values |
|
56
|
15 |
|
$strategy = $strategy ? $strategy : self::STRATEGY_REDIS; |
|
57
|
15 |
|
$options = $options ? $options : []; |
|
58
|
|
|
|
|
59
|
15 |
|
$key = self::getKey($realm, $strategy); |
|
60
|
15 |
|
$fullKey = self::getFullKey($key, $options); |
|
61
|
|
|
|
|
62
|
15 |
|
$instance = self::newCacheInstance($strategy, $realm, $options); |
|
63
|
|
|
|
|
64
|
15 |
|
self::$cachedInstances[$fullKey] = &$instance; |
|
65
|
15 |
|
self::$cachedInstances[$key] = &$instance; |
|
66
|
15 |
|
self::$cachedInstances[$realm] = &$instance; |
|
67
|
|
|
|
|
68
|
15 |
|
return $instance; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
private static function newCacheInstance($strategy, $realm, $options) |
|
72
|
|
|
{ |
|
73
|
15 |
|
if ($strategy === self::STRATEGY_REDIS) { |
|
74
|
15 |
|
return new FlintRedisCacheRedis($realm, $options); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
12 |
|
if ($strategy === self::STRATEGY_FLINTSTONE) { |
|
78
|
12 |
|
return new FlintRedisCacheFlintstone($realm, $options); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|