forked from aws/aws-sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request aws#548 from aws/stream-wrapper-cache
Added support for injectable cache to stream wrapper.
- Loading branch information
Showing
5 changed files
with
290 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
namespace Aws; | ||
|
||
/** | ||
* Represents a simple cache interface. | ||
*/ | ||
interface CacheInterface | ||
{ | ||
/** | ||
* Get a cache item by key. | ||
* | ||
* @param string $key Key to retrieve. | ||
* | ||
* @return mixed|null Returns the value or null if not found. | ||
*/ | ||
public function get($key); | ||
|
||
/** | ||
* Set a cache key value. | ||
* | ||
* @param string $key Key to set | ||
* @param mixed $value Value to set. | ||
* @param int $ttl Number of seconds the item is allowed to live. Set | ||
* to 0 to allow an unlimited lifetime. | ||
*/ | ||
public function set($key, $value, $ttl = 0); | ||
|
||
/** | ||
* Remove a cache key. | ||
* | ||
* @param string $key Key to remove. | ||
*/ | ||
public function remove($key); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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]); | ||
} | ||
} |
Oops, something went wrong.