|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DDTrace\FeatureFlags; |
| 4 | + |
| 5 | +/** |
| 6 | + * LRU-based exposure event deduplication cache. |
| 7 | + * |
| 8 | + * Mirrors the canonical Java implementation (LRUExposureCache). |
| 9 | + * Key = (flagKey, subjectId), Value = (variantKey, allocationKey). |
| 10 | + * Returns true from add() when the event is new or its value changed, |
| 11 | + * false when it's an exact duplicate. Even duplicates update the LRU |
| 12 | + * position to keep hot entries from being evicted. |
| 13 | + */ |
| 14 | +class ExposureCache |
| 15 | +{ |
| 16 | + /** @var LRUCache */ |
| 17 | + private $cache; |
| 18 | + |
| 19 | + /** |
| 20 | + * @param int $capacity Maximum number of entries |
| 21 | + */ |
| 22 | + public function __construct($capacity = 65536) |
| 23 | + { |
| 24 | + $this->cache = new LRUCache($capacity); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Add an exposure event to the cache. |
| 29 | + * |
| 30 | + * @param string $flagKey |
| 31 | + * @param string $subjectId |
| 32 | + * @param string $variantKey |
| 33 | + * @param string $allocationKey |
| 34 | + * @return bool true if the event is new or value changed, false if exact duplicate |
| 35 | + */ |
| 36 | + public function add($flagKey, $subjectId, $variantKey, $allocationKey) |
| 37 | + { |
| 38 | + $key = self::makeKey($flagKey, $subjectId); |
| 39 | + $newValue = self::makeValue($variantKey, $allocationKey); |
| 40 | + |
| 41 | + // Always put (updates LRU position even for duplicates) |
| 42 | + $oldValue = $this->cache->put($key, $newValue); |
| 43 | + |
| 44 | + return $oldValue === null || $oldValue !== $newValue; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Get the cached value for a (flag, subject) pair. |
| 49 | + * |
| 50 | + * @param string $flagKey |
| 51 | + * @param string $subjectId |
| 52 | + * @return array|null [variantKey, allocationKey] or null if not found |
| 53 | + */ |
| 54 | + public function get($flagKey, $subjectId) |
| 55 | + { |
| 56 | + $key = self::makeKey($flagKey, $subjectId); |
| 57 | + $value = $this->cache->get($key); |
| 58 | + if ($value === null) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + return self::parseValue($value); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Return the number of entries in the cache. |
| 66 | + * |
| 67 | + * @return int |
| 68 | + */ |
| 69 | + public function size() |
| 70 | + { |
| 71 | + return $this->cache->size(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Clear all entries. |
| 76 | + */ |
| 77 | + public function clear() |
| 78 | + { |
| 79 | + $this->cache->clear(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Build a composite key that avoids collision. |
| 84 | + * Uses length-prefixing: "<len>:<flag>:<subject>" |
| 85 | + */ |
| 86 | + private static function makeKey($flagKey, $subjectId) |
| 87 | + { |
| 88 | + $f = $flagKey !== null ? $flagKey : ''; |
| 89 | + $s = $subjectId !== null ? $subjectId : ''; |
| 90 | + return strlen($f) . ':' . $f . ':' . $s; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Build a composite value string. |
| 95 | + */ |
| 96 | + private static function makeValue($variantKey, $allocationKey) |
| 97 | + { |
| 98 | + $v = $variantKey !== null ? $variantKey : ''; |
| 99 | + $a = $allocationKey !== null ? $allocationKey : ''; |
| 100 | + return strlen($v) . ':' . $v . ':' . $a; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Parse a composite value string back into [variantKey, allocationKey]. |
| 105 | + */ |
| 106 | + private static function parseValue($value) |
| 107 | + { |
| 108 | + $colonPos = strpos($value, ':'); |
| 109 | + if ($colonPos === false) { |
| 110 | + return [$value, '']; |
| 111 | + } |
| 112 | + $len = (int) substr($value, 0, $colonPos); |
| 113 | + $variant = substr($value, $colonPos + 1, $len); |
| 114 | + $allocation = substr($value, $colonPos + 1 + $len + 1); |
| 115 | + return [$variant, $allocation]; |
| 116 | + } |
| 117 | +} |
0 commit comments