Updated domain without a dash :(
[core.git] / inc / classes / main / cache / class_MemoryCache.php
1 <?php
2 /**
3  * A simple memory cache (similar to a registry)
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class MemoryCache extends BaseFrameworkSystem implements Cacheable {
25         /**
26          * The "memory cache" is simply a wrapped object array
27          */
28         private $dataCache = NULL;
29
30         /**
31          * Protected constructor
32          *
33          * @return      void
34          */
35         protected function __construct () {
36                 // Call parent constructor
37                 parent::__construct(__CLASS__);
38         }
39
40         /**
41          * Creates an instance of this class
42          *
43          * @return      $cacheInstance  An instance of this cache class
44          */
45         public static final function createMemoryCache () {
46                 // Get a new instance
47                 $cacheInstance = new MemoryCache();
48
49                 // Initialize the cache
50                 $cacheInstance->initCache();
51
52                 // Return the prepared instance
53                 return $cacheInstance;
54         }
55
56         /**
57          * Initialize this cache by creating an object array
58          *
59          * @return      void
60          */
61         protected function initCache () {
62                 // Now create the "data cache"
63                 $this->dataCache = new FrameworkArrayObject('FakedDataCache');
64         }
65
66         /**
67          * Does the specified offset exist in cache?
68          *
69          * @param       $offset                 The offset we are looking for
70          * @param       $arrayElement   If type is array, then this element must be found
71          * @param       $minimumCount   If array element is found then this count must at least match
72          * @return      $exists                 Whether the offset exists
73          */
74         public function offsetExists ($offset, $arrayElement = NULL, $minimumCount = 0) {
75                 // Is it there?
76                 $exists = $this->dataCache->offsetExists($offset);
77
78                 // So look for array element?
79                 if (($exists === TRUE) && (!is_null($arrayElement))) {
80                         // Get it
81                         $array = $this->offsetGet($offset);
82
83                         // Is it an array and element is found?
84                         if ((is_array($array)) && (isset($array[$arrayElement]))) {
85                                 // Is an array and element is found, so check count
86                                 $exists = (count($array[$arrayElement]) >= $minimumCount);
87                         } else {
88                                 // Not found
89                                 $exists = FALSE;
90                         }
91                 } // END - if
92
93                 // Return status
94                 return $exists;
95         }
96
97         /**
98          * Setter for cache offset
99          *
100          * @param       $offset         The offset we shall set
101          * @param       $data           Data to store in cache
102          * @return      void
103          */
104         public function offsetSet ($offset, $data) {
105                 $this->dataCache->offsetSet($offset, $data);
106         }
107
108         /**
109          * Getter for cache offset or "null" if not found
110          *
111          * @param       $offset         The offset we shall set
112          * @return      $data           Data to store in cache
113          */
114         public function offsetGet ($offset) {
115                 // Default is offset not found
116                 $data = NULL;
117
118                 // Is the offset there?
119                 if ($this->offsetExists($offset)) {
120                         // Then get the data from it
121                         $data = $this->dataCache->offsetGet($offset);
122                 } // END - if
123
124                 // Return data
125                 return $data;
126         }
127
128         /**
129          * Purges the given cache entry
130          *
131          * @param       $offset         The offset we shall set
132          * @return      void
133          */
134         public function purgeOffset ($offset) {
135                 // Is the offset there?
136                 if ($this->offsetExists($offset)) {
137                         // Purge only existing keys
138                         //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CACHE: Unsetting cache ' . $offset);
139                         $this->dataCache->offsetUnset($offset);
140                 } // END - if
141         }
142 }
143
144 // [EOF]
145 ?>