aboutsummaryrefslogtreecommitdiff
path: root/engine/classes/ElggLRUCache.php
blob: add78ecb4fb38ed6c5dcd5dc317b4b078e647c3f (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php

/**
 * Least Recently Used Cache
 *
 * A fixed sized cache that removes the element used last when it reaches its
 * size limit.
 * 
 * Based on https://github.com/cash/LRUCache
 * 
 * @access private
 * 
 * @package    Elgg.Core
 * @subpackage Cache
 */
class ElggLRUCache {
	/** @var int */
	protected $maximumSize;

	/**
	 * The front of the array contains the LRU element
	 *
	 * @var array
	 */
	protected $data = array();

	/**
	 * Create a LRU Cache
	 *
	 * @param int $size The size of the cache
	 * @throws InvalidArgumentException
	 */
	public function __construct($size) {
		if (!is_int($size) || $size <= 0) {
			throw new InvalidArgumentException();
		}
		$this->maximumSize = $size;
	}

	/**
	 * Get the value cached with this key
	 *
	 * @param int|string $key     The key. Strings that are ints are cast to ints.
	 * @param mixed      $default The value to be returned if key not found. (Optional)
	 * @return mixed
	 */
	public function get($key, $default = null) {
		if (isset($this->data[$key])) {
			$this->recordAccess($key);
			return $this->data[$key];
		} else {
			return $default;
		}
	}

	/**
	 * Put something in the cache
	 *
	 * @param int|string $key   The key. Strings that are ints are cast to ints.
	 * @param mixed      $value The value to cache
	 * @return void
	 */
	public function put($key, $value) {
		if (isset($this->data[$key])) {
			$this->data[$key] = $value;
			$this->recordAccess($key);
		} else {
			$this->data[$key] = $value;
			if ($this->size() > $this->maximumSize) {
				// remove least recently used element (front of array)
				reset($this->data);
				unset($this->data[key($this->data)]);
			}
		}
	}

	/**
	 * Get the number of elements in the cache
	 *
	 * @return int
	 */
	public function size() {
		return count($this->data);
	}

	/**
	 * Does the cache contain an element with this key
	 *
	 * @param int|string $key The key
	 * @return boolean
	 */
	public function containsKey($key) {
		return isset($this->data[$key]);
	}

	/**
	 * Remove the element with this key.
	 *
	 * @param int|string $key The key
	 * @return mixed Value or null if not set
	 */
	public function remove($key) {
		if (isset($this->data[$key])) {
			$value = $this->data[$key];
			unset($this->data[$key]);
			return $value;
		} else {
			return null;
		}
	}

	/**
	 * Clear the cache
	 * 
	 * @return void
	 */
	public function clear() {
		$this->data = array();
	}

	/**
	 * Moves the element from current position to end of array
	 * 
	 * @param int|string $key The key
	 * @return void
	 */
	protected function recordAccess($key) {
		$value = $this->data[$key];
		unset($this->data[$key]);
		$this->data[$key] = $value;
	}
}