← All changes
|
jetpack_vendor/automattic/jetpack-status/src/class-cache.php
+54
-0
16.2-beta
→
16.3-beta
View file →
| @@ -1,0 +1,54 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * A static in-process cache for blog data. | |
| 4 | + * | |
| 5 | + * @package automattic/jetpack-status | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace Automattic\Jetpack\Status; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * A static in-process cache for blog data. | |
| 12 | + * | |
| 13 | + * For internal use only. Do not use this externally. | |
| 14 | + */ | |
| 15 | +class Cache { | |
| 16 | + /** | |
| 17 | + * Cached data; | |
| 18 | + * | |
| 19 | + * @var array[] | |
| 20 | + */ | |
| 21 | + private static $cache = array(); | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * Get a value from the cache. | |
| 25 | + * | |
| 26 | + * @param string $key Key to fetch. | |
| 27 | + * @param mixed $default Default value to return if the key is not set. | |
| 28 | + * @return mixed Data. | |
| 29 | + */ | |
| 30 | + public static function get( $key, $default = null ) { | |
| 31 | + $blog_id = get_current_blog_id(); | |
| 32 | + return isset( self::$cache[ $blog_id ] ) && array_key_exists( $key, self::$cache[ $blog_id ] ) ? self::$cache[ $blog_id ][ $key ] : $default; | |
| 33 | + } | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Set a value in the cache. | |
| 37 | + * | |
| 38 | + * @param string $key Key to set. | |
| 39 | + * @param mixed $value Value to store. | |
| 40 | + */ | |
| 41 | + public static function set( $key, $value ) { | |
| 42 | + $blog_id = get_current_blog_id(); | |
| 43 | + self::$cache[ $blog_id ][ $key ] = $value; | |
| 44 | + } | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * Clear the cache. | |
| 48 | + * | |
| 49 | + * This is intended for use in unit tests. | |
| 50 | + */ | |
| 51 | + public static function clear() { | |
| 52 | + self::$cache = array(); | |
| 53 | + } | |
| 54 | +} | |