3 * A simple memory cache (similar to a registry)
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class MemoryCache extends BaseFrameworkSystem implements Cacheable {
26 * The "memory cache" is simply a wrapped object array
28 private $dataCache = NULL;
31 * Protected constructor
35 protected function __construct () {
36 // Call parent constructor
37 parent::__construct(__CLASS__);
41 * Creates an instance of this class
43 * @return $cacheInstance An instance of this cache class
45 public static final function createMemoryCache () {
47 $cacheInstance = new MemoryCache();
49 // Initialize the cache
50 $cacheInstance->initCache();
52 // Return the prepared instance
53 return $cacheInstance;
57 * Initialize this cache by creating an object array
61 protected function initCache () {
62 // Now create the "data cache"
63 $this->dataCache = new FrameworkArrayObject('FakedDataCache');
67 * Does the specified offset exist in cache?
69 * @param $offset The offset we are looking for
70 * @return $exists Whether the offset exists
72 public function offsetExists ($offset) {
73 $exists = $this->dataCache->offsetExists($offset);
78 * Setter for cache offset
80 * @param $offset The offset we shall set
81 * @param $data Data to store in cache
84 public function offsetSet ($offset, $data) {
85 $this->dataCache->offsetSet($offset, $data);
89 * Getter for cache offset or "null" if not found
91 * @param $offset The offset we shall set
92 * @return $data Data to store in cache
94 public function offsetGet ($offset) {
95 // Default is offset not found
98 // Is the offset there?
99 if ($this->offsetExists($offset)) {
100 // Then get the data from it
101 $data = $this->dataCache->offsetGet($offset);
109 * Purges the given cache entry
111 * @param $offset The offset we shall set
114 public function purgeOffset ($offset) {
115 // Is the offset there?
116 if ($this->offsetExists($offset)) {
117 // Purge only existing keys
118 //* DEBUG: */ $this->debugOutput('CACHE: Unsetting cache ' . $offset);
119 $this->dataCache->offsetUnset($offset);