summaryrefslogtreecommitdiff
blob: 9ca54593680a3929ddd94373e0b8135d7024ed9f (plain)
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
<?php
/**
 * A static in-process cache for blog data.
 *
 * @package automattic/jetpack-status
 */

namespace Automattic\Jetpack\Status;

/**
 * A static in-process cache for blog data.
 *
 * For internal use only. Do not use this externally.
 */
class Cache {
	/**
	 * Cached data;
	 *
	 * @var array[]
	 */
	private static $cache = array();

	/**
	 * Get a value from the cache.
	 *
	 * @param string $key Key to fetch.
	 * @param mixed  $default Default value to return if the key is not set.
	 * @returns mixed Data.
	 */
	public static function get( $key, $default = null ) {
		$blog_id = get_current_blog_id();
		return isset( self::$cache[ $blog_id ] ) && array_key_exists( $key, self::$cache[ $blog_id ] ) ? self::$cache[ $blog_id ][ $key ] : $default;
	}

	/**
	 * Set a value in the cache.
	 *
	 * @param string $key Key to set.
	 * @param mixed  $value Value to store.
	 */
	public static function set( $key, $value ) {
		$blog_id                         = get_current_blog_id();
		self::$cache[ $blog_id ][ $key ] = $value;
	}

	/**
	 * Clear the cache.
	 *
	 * This is intended for use in unit tests.
	 */
	public static function clear() {
		self::$cache = array();
	}

}