3 namespace CoreFramework\Cache;
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
9 * A simple memory cache (similar to a registry)
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
16 * @todo Rename to InProgressCache
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class MemoryCache extends BaseFrameworkSystem implements Cacheable {
33 * The "memory cache" is simply a wrapped object array
35 private $dataCache = NULL;
38 * Protected constructor
42 protected function __construct () {
43 // Call parent constructor
44 parent::__construct(__CLASS__);
48 * Creates an instance of this class
50 * @return $cacheInstance An instance of this cache class
52 public static final function createMemoryCache () {
54 $cacheInstance = new MemoryCache();
56 // Initialize the cache
57 $cacheInstance->initCache();
59 // Return the prepared instance
60 return $cacheInstance;
64 * Initialize this cache by creating an object array
68 protected function initCache () {
69 // Now create the "data cache"
70 $this->dataCache = new FrameworkArrayObject('FakedDataCache');
74 * Does the specified offset exist in cache?
76 * @param $offset The offset we are looking for
77 * @param $arrayElement If type is array, then this element must be found
78 * @param $minimumCount If array element is found then this count must at least match
79 * @return $exists Whether the offset exists
81 public function offsetExists ($offset, $arrayElement = NULL, $minimumCount = 0) {
83 $exists = $this->dataCache->offsetExists($offset);
85 // So look for array element?
86 if (($exists === TRUE) && (!is_null($arrayElement))) {
88 $array = $this->offsetGet($offset);
90 // Is it an array and element is found?
91 if ((is_array($array)) && (isset($array[$arrayElement]))) {
92 // Is an array and element is found, so check count
93 $exists = (count($array[$arrayElement]) >= $minimumCount);
105 * Setter for cache offset
107 * @param $offset The offset we shall set
108 * @param $data Data to store in cache
111 public function offsetSet ($offset, $data) {
112 $this->dataCache->offsetSet($offset, $data);
116 * Getter for cache offset or "null" if not found
118 * @param $offset The offset we shall set
119 * @return $data Data to store in cache
121 public function offsetGet ($offset) {
122 // Default is offset not found
125 // Is the offset there?
126 if ($this->offsetExists($offset)) {
127 // Then get the data from it
128 $data = $this->dataCache->offsetGet($offset);
136 * Purges the given cache entry
138 * @param $offset The offset we shall set
141 public function purgeOffset ($offset) {
142 // Is the offset there?
143 if ($this->offsetExists($offset)) {
144 // Purge only existing keys
145 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHE: Unsetting cache ' . $offset);
146 $this->dataCache->offsetUnset($offset);