/usr/share/php/Aws/LruArrayCache.php is in php-aws-sdk 3.15.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <?php
namespace Aws;
/**
* Simple in-memory LRU cache that limits the number of cached entries.
*
* The LRU cache is implemented using PHP's ordered associative array. When
* accessing an element, the element is removed from the hash and re-added to
* ensure that recently used items are always at the end of the list while
* least recently used are at the beginning. When a value is added to the
* cache, if the number of cached items exceeds the allowed number, the first
* N number of items are removed from the array.
*/
class LruArrayCache implements CacheInterface
{
/** @var int */
private $maxItems;
/** @var array */
private $items;
/**
* @param int $maxItems Maximum number of allowed cache items.
*/
public function __construct($maxItems = 1000)
{
$this->maxItems = $maxItems;
}
public function get($key)
{
if (!isset($this->items[$key])) {
return null;
}
$entry = $this->items[$key];
// Ensure the item is not expired.
if (!$entry[1] || time() < $entry[1]) {
// LRU: remove the item and push it to the end of the array.
unset($this->items[$key]);
$this->items[$key] = $entry;
return $entry[0];
}
unset($this->items[$key]);
return null;
}
public function set($key, $value, $ttl = 0)
{
// Only call time() if the TTL is not 0/false/null
$ttl = $ttl ? time() + $ttl : 0;
$this->items[$key] = [$value, $ttl];
// Determine if there are more items in the cache than allowed.
$diff = count($this->items) - $this->maxItems;
// Clear out least recently used items.
if ($diff > 0) {
// Reset to the beginning of the array and begin unsetting.
reset($this->items);
for ($i = 0; $i < $diff; $i++) {
unset($this->items[key($this->items)]);
next($this->items);
}
}
}
public function remove($key)
{
unset($this->items[$key]);
}
}
|