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, this is free software
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__);
40 $this->removeNumberFormaters();
41 $this->removeSystemArray();
45 * Creates an instance of this class
47 * @return $cacheInstance An instance of this cache class
49 public final static function createMemoryCache () {
51 $cacheInstance = new MemoryCache();
53 // Initialize the cache
54 $cacheInstance->initCache();
56 // Return the prepared instance
57 return $cacheInstance;
61 * Initialize this cache by creating an object array
65 protected function initCache () {
66 // Now create the "data cache"
67 $this->dataCache = new FrameworkArrayObject('FakedDataCache');
71 * Does the specified offset exist in cache?
73 * @param $offset The offset we are looking for
74 * @return $exists Wether the offset exists
76 public final function offsetExists ($offset) {
77 $exists = $this->dataCache->offsetExists($offset);
82 * Setter for cache offset
84 * @param $offset The offset we shall set
85 * @param $data Data to store in cache
88 public final function offsetSet ($offset, $data) {
89 $this->dataCache->offsetSet($offset, $data);
93 * Getter for cache offset or "null" if not found
95 * @param $offset The offset we shall set
96 * @return $data Data to store in cache
98 public final function offsetGet ($offset) {
99 // Default is offset not found
102 // Is the offset there?
103 if ($this->offsetExists($offset)) {
104 // Then get the data from it
105 $data = $this->dataCache->offsetGet($offset);