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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
 * Base_Cache will cache variables/arrays
 *
 * Base_Cache is a Base because it is called in the base_class and 
 * we don't want recursive inheritance.
 *
 * @package Studio98 Framework
 * @since 1.0
 */
class Base_Cache {
	/**
	 * Holds the cached objects
	 *
	 * @var array
	 * @access private
	 * @since 1.0
	 */
	private $cache = array ();

	/**
	 * Cache objects that do not exist in the cache
	 *
	 * @var array
	 * @access private
	 * @since 1.0
	 */
	private $non_existant_objects = array ();

	/**
	 * The amount of times the cache data was already stored in the cache.
	 *
	 * @since 1.0
	 * @access private
	 * @var int
	 */
	private $cache_hits = 0;

	/**
	 * Amount of times the cache did not have the request in cache
	 *
	 * @var int
	 * @access public
	 * @since 1.0
	 */
	public $cache_misses = 0;

	/**
	 * Adds data to the cache if it doesn't already exist.
	 *
	 * @uses Base_Cache->get Checks to see if the cache already has data.
	 * @uses Base_Cache->set Sets the data after the checking the cache
	 *		contents existance.
	 *
	 * @since 1.0
	 *
	 * @param int|string $id What to call the contents in the cache
	 * @param mixed $data The contents to store in the cache
	 * @param string $group Where to group the cache contents
	 * @return bool False if cache ID and group already exists, true on success
	 */
	public function add( $id, $data, $group = 'default' ) {
		if( empty( $group ) )
			$group = 'default';

		if( false !== $this->get( $id, $group, false ) )
			return false;
		
		$this->set( $id, $data, $group );
		
		return $data;
	}

	/**
	 * Remove the contents of the cache key in the group
	 *
	 * @since 1.0
	 *
	 * @param int|string $id What the contents in the cache are called
	 * @param string $group Where the cache contents are grouped
	 * @param bool $force Optional. Whether to force the unsetting of the cache
	 *		ID in the group
	 * @return bool False if the contents weren't deleted and true on success
	 */
	public function delete( $id, $group = 'default', $force = false ) {
		if( empty( $group ) )
			$group = 'default';

		if( !$force && false === $this->get( $id, $group, false ) )
			return false;

		unset( $this->cache[$group][$id] );
		$this->non_existant_objects[$group][$id] = true;
		return true;
	}

	/**
	 * Clears the object cache of all data
	 *
	 * @since 1.0.0
	 *
	 * @return bool Always returns true
	 */
	function flush() {
		$this->cache = array();

		return true;
	}

	/**
	 * Retrieves the cache contents, if it exists
	 *
	 *
	 * @since 1.0.0
	 *
	 * @param int|string $id What the contents in the cache are called
	 * @param string $group Where the cache contents are grouped
	 * @return bool|mixed False on failure to retrieve contents or the cache
	 *		contents on success
	 */
	function get( $id, $group = 'default' ) {
		if( empty( $group ) )
			$group = 'default';

		if( isset( $this->cache[$group][$id] ) ) {
			$this->cache_hits++;
			
			return ( is_object( $this->cache[$group][$id] ) ) ? bc_clone( $this->cache[$group][$id] ) : $this->cache[$group][$id];
		}

		if ( isset( $this->non_existant_objects[$group][$id] ) )
			return false;

		$this->non_existant_objects[$group][$id] = true;
		$this->cache_misses++;
		return false;
	}

	/**
	 * Replace the contents in the cache, if contents already exist
	 *
	 * @since 1.0.0
	 * @see Base_Cache->set()
	 *
	 * @param int|string $id What to call the contents in the cache
	 * @param mixed $data The contents to store in the cache
	 * @param string $group Where to group the cache contents
	 * @return bool False if not exists, true if contents were replaced
	 */
	function replace( $id, $data, $group = 'default' ) {
		if( empty( $group ) )
			$group = 'default';

		if( false === $this->get( $id, $group, false ) )
			return false;

		return $this->set( $id, $data, $group );
	}

	/**
	 * Sets the data contents into the cache
	 *
	 * @since 1.0.0
	 *
	 * @param int|string $id What to call the contents in the cache
	 * @param mixed $data The contents to store in the cache
	 * @param string $group Where to group the cache contents
	 * @return bool Always returns true
	 */
	function set( $id, $data, $group = 'default' ) {
		if( empty( $group ) )
			$group = 'default';

		if( NULL === $data )
			$data = '';

		if ( is_object( $data ) )
			$data = clone( $data );
		
		$this->cache[$group][$id] = $data;

		if( isset( $this->non_existant_objects[$group][$id] ) )
			unset( $this->non_existant_objects[$group][$id] );

		return true;
	}
}