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;
10 * A simple memory cache (similar to a registry)
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 <<<<<<< HEAD:framework/main/classes/cache/class_MemoryCache.php
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
18 >>>>>>> Some updates::inc/main/classes/cache/class_MemoryCache.php
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.shipsimu.org
21 * @todo Rename to InProgressCache
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class MemoryCache extends BaseFrameworkSystem implements Cacheable {
38 * The "memory cache" is simply a wrapped object array
40 private $dataCache = NULL;
43 * Protected constructor
47 protected function __construct () {
48 // Call parent constructor
49 parent::__construct(__CLASS__);
53 * Creates an instance of this class
55 * @return $cacheInstance An instance of this cache class
57 public static final function createMemoryCache () {
59 $cacheInstance = new MemoryCache();
61 // Initialize the cache
62 $cacheInstance->initCache();
64 // Return the prepared instance
65 return $cacheInstance;
69 * Initialize this cache by creating an object array
73 protected function initCache () {
74 // Now create the "data cache"
75 $this->dataCache = new FrameworkArrayObject('FakedDataCache');
79 * Does the specified offset exist in cache?
81 * @param $offset The offset we are looking for
82 * @param $arrayElement If type is array, then this element must be found
83 * @param $minimumCount If array element is found then this count must at least match
84 * @return $exists Whether the offset exists
86 public function offsetExists ($offset, $arrayElement = NULL, $minimumCount = 0) {
88 $exists = $this->dataCache->offsetExists($offset);
90 // So look for array element?
91 if (($exists === true) && (!is_null($arrayElement))) {
93 $array = $this->offsetGet($offset);
95 // Is it an array and element is found?
96 if ((is_array($array)) && (isset($array[$arrayElement]))) {
97 // Is an array and element is found, so check count
98 $exists = (count($array[$arrayElement]) >= $minimumCount);
110 * Setter for cache offset
112 * @param $offset The offset we shall set
113 * @param $data Data to store in cache
116 public function offsetSet ($offset, $data) {
117 $this->dataCache->offsetSet($offset, $data);
121 * Getter for cache offset or "null" if not found
123 * @param $offset The offset we shall set
124 * @return $data Data to store in cache
126 public function offsetGet ($offset) {
127 // Default is offset not found
130 // Is the offset there?
131 if ($this->offsetExists($offset)) {
132 // Then get the data from it
133 $data = $this->dataCache->offsetGet($offset);
141 * Purges the given cache entry
143 * @param $offset The offset we shall set
146 public function purgeOffset ($offset) {
147 // Is the offset there?
148 if ($this->offsetExists($offset)) {
149 // Purge only existing keys
150 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHE: Unsetting cache ' . $offset);
151 $this->dataCache->offsetUnset($offset);