Last active
August 29, 2015 13:57
-
-
Save sarelvdwalt/9396481 to your computer and use it in GitHub Desktop.
Demonstrates how to have a static cache variable (array) which is capped to a maximum count of entries, with last-in-first-out.
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
<?php | |
/** | |
* Demonstrate how to have a static cache variable that is capped to a maximum count. | |
* | |
* User: sarel | |
* Date: 2014/03/06 | |
* Time: 8:27 PM | |
*/ | |
class testShifting { | |
public static $cache = array(); | |
const CACHE_MAX = 5; | |
public function calculateSomething () { | |
if (count(self::$cache) >= self::CACHE_MAX) { | |
array_shift(self::$cache); | |
} | |
self::$cache[] = date('Y-m-d H:i:s'); | |
} | |
} | |
$bleh = new testShifting(); | |
for ($i = 0; $i < 7; $i++) { | |
$bleh->calculateSomething(); | |
print_r(testShifting::$cache); | |
sleep(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The cap is 5, so we only always want 5 entries in the array, this will thus yield: