-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSplitCache.php
More file actions
112 lines (96 loc) · 2.88 KB
/
Copy pathSplitCache.php
File metadata and controls
112 lines (96 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace SplitIO\Component\Cache;
class SplitCache implements SplitCacheInterface
{
const KEY_TILL_CACHED_ITEM = 'SPLITIO.splits.till';
const KEY_SPLIT_CACHED_ITEM = 'SPLITIO.split.{splitName}';
const KEY_TRAFFIC_TYPE_CACHED = 'SPLITIO.trafficType.{trafficTypeName}';
/**
* @var \SplitIO\Component\Cache\Pool
*/
private $cache;
/**
* @param \SplitIO\Component\Cache\Pool $cache
*/
public function __construct(Pool $cache)
{
$this->cache = $cache;
}
private static function getCacheKeyForSinceParameter()
{
return self::KEY_TILL_CACHED_ITEM;
}
private static function getCacheKeySearchPattern()
{
return self::getCacheKeyForSplit('*');
}
private static function getCacheKeyForSplit($splitName)
{
return str_replace('{splitName}', $splitName, self::KEY_SPLIT_CACHED_ITEM);
}
private static function getSplitNameFromCacheKey($key)
{
$cacheKeyPrefix = self::getCacheKeyForSplit('');
return str_replace($cacheKeyPrefix, '', $key);
}
/**
* @return int
*/
public function getChangeNumber()
{
$since = $this->cache->get(self::getCacheKeyForSinceParameter());
// empty check for nullable value
return (empty($since)) ? -1 : $since;
}
/**
* @param string $splitName
* @return string JSON representation
*/
public function getSplit($splitName)
{
return $this->cache->get(self::getCacheKeyForSplit($splitName));
}
/**
* @param array $splitNames
* @return array
*/
public function getSplits($splitNames)
{
$cacheItems = $this->cache->fetchMany(array_map('self::getCacheKeyForSplit', $splitNames));
$toReturn = array();
foreach ($cacheItems as $key => $value) {
$toReturn[self::getSplitNameFromCacheKey($key)] = $value;
}
return $toReturn;
}
/**
* @return array(string) List of split names
*/
public function getSplitNames()
{
$splitKeys = $this->cache->getKeys(self::getCacheKeySearchPattern());
return array_map('self::getSplitNameFromCacheKey', $splitKeys);
}
/**
* @return array(string) List of all split JSON strings
*/
public function getAllSplits()
{
$splitNames = $this->getSplitNames();
return $this->getSplits($splitNames);
}
private static function getCacheKeyForTrafficType($trafficType)
{
return str_replace('{trafficTypeName}', $trafficType, self::KEY_TRAFFIC_TYPE_CACHED);
}
/**
* @param string $trafficType
* @return bool
*/
public function trafficTypeExists($trafficType)
{
$count = $this->cache->get(self::getCacheKeyForTrafficType($trafficType));
// empty check for nullable value
return (empty($count) || $count < 1) ? false : true;
}
}