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