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 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
17 * @todo Rename to InProgressCache
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class MemoryCache extends BaseFrameworkSystem implements Cacheable {
34 * The "memory cache" is simply a wrapped object array
36 private $dataCache = NULL;
39 * Protected constructor
43 protected function __construct () {
44 // Call parent constructor
45 parent::__construct(__CLASS__);
49 * Creates an instance of this class
51 * @return $cacheInstance An instance of this cache class
53 public static final function createMemoryCache () {
55 $cacheInstance = new MemoryCache();
57 // Initialize the cache
58 $cacheInstance->initCache();
60 // Return the prepared instance
61 return $cacheInstance;
65 * Initialize this cache by creating an object array
69 protected function initCache () {
70 // Now create the "data cache"
71 $this->dataCache = new FrameworkArrayObject('FakedDataCache');
75 * Does the specified offset exist in cache?
77 * @param $offset The offset we are looking for
78 * @param $arrayElement If type is array, then this element must be found
79 * @param $minimumCount If array element is found then this count must at least match
80 * @return $exists Whether the offset exists
82 public function offsetExists ($offset, $arrayElement = NULL, $minimumCount = 0) {
84 $exists = $this->dataCache->offsetExists($offset);
86 // So look for array element?
87 if (($exists === true) && (!is_null($arrayElement))) {
89 $array = $this->offsetGet($offset);
91 // Is it an array and element is found?
92 if ((is_array($array)) && (isset($array[$arrayElement]))) {
93 // Is an array and element is found, so check count
94 $exists = (count($array[$arrayElement]) >= $minimumCount);
106 * Setter for cache offset
108 * @param $offset The offset we shall set
109 * @param $data Data to store in cache
112 public function offsetSet ($offset, $data) {
113 $this->dataCache->offsetSet($offset, $data);
117 * Getter for cache offset or "null" if not found
119 * @param $offset The offset we shall set
120 * @return $data Data to store in cache
122 public function offsetGet ($offset) {
123 // Default is offset not found
126 // Is the offset there?
127 if ($this->offsetExists($offset)) {
128 // Then get the data from it
129 $data = $this->dataCache->offsetGet($offset);
137 * Purges the given cache entry
139 * @param $offset The offset we shall set
142 public function purgeOffset ($offset) {
143 // Is the offset there?
144 if ($this->offsetExists($offset)) {
145 // Purge only existing keys
146 //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHE: Unsetting cache ' . $offset);
147 $this->dataCache->offsetUnset($offset);