-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathImpressionCache.php
More file actions
53 lines (47 loc) · 1.61 KB
/
ImpressionCache.php
File metadata and controls
53 lines (47 loc) · 1.61 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
<?php
namespace SplitIO\Component\Cache;
use SplitIO\Component\Common\Context;
use SplitIO\Sdk\QueueMetadataMessage;
use SplitIO\Component\Cache\Pool;
class ImpressionCache
{
const IMPRESSIONS_QUEUE_KEY = "SPLITIO.impressions";
const IMPRESSION_KEY_DEFAULT_TTL = 3600;
/**
* @var \SplitIO\Component\Cache\Pool
*/
private $cache;
/**
* @param \SplitIO\Component\Cache\Pool $cache
*/
public function __construct(Pool $cache)
{
$this->cache = $cache;
}
public function logImpressions($impressions, QueueMetadataMessage $metadata)
{
$toStore = array_map(
function ($imp) use ($metadata) {
return json_encode(array(
'm' => $metadata->toArray(),
"i" => array(
"k" => $imp->getId(),
"b" => $imp->getBucketingKey(),
"f" => $imp->getFeature(),
"t" => $imp->getTreatment(),
"r" => $imp->getLabel(),
"c" => $imp->getChangeNumber(),
"m" => $imp->getTime(),
),
));
},
$impressions
);
Context::getLogger()->debug("Adding impressions into queue: ". implode(",", $toStore));
$count = $this->cache->rightPushInList(self::IMPRESSIONS_QUEUE_KEY, $toStore);
if ($count == count($impressions)) {
$this->cache->expireKey(self::IMPRESSIONS_QUEUE_KEY, self::IMPRESSION_KEY_DEFAULT_TTL);
}
return ($count >= count($impressions));
}
}