c23851ee86d6310eaf3b8a6c0b49bc8c904e4a8f
[core.git] / framework / main / classes / cache / class_MemoryCache.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Cache\Memory;
4
5 // Import framework stuff
6 use CoreFramework\Cache\Cacheable;
7 use CoreFramework\Object\BaseFrameworkSystem;
8
9 /**
10  * A simple memory cache (similar to a registry)
11  *
12  * @author              Roland Haeder <webmaster@shipsimu.org>
13  * @version             0.0.0
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
18  *
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.
23  *
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.
28  *
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/>.
31  */
32 class MemoryCache extends BaseFrameworkSystem implements Cacheable {
33         /**
34          * The "memory cache" is simply a wrapped object array
35          */
36         private $dataCache = NULL;
37
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         protected function __construct () {
44                 // Call parent constructor
45                 parent::__construct(__CLASS__);
46         }
47
48         /**
49          * Creates an instance of this class
50          *
51          * @return      $cacheInstance  An instance of this cache class
52          */
53         public static final function createMemoryCache () {
54                 // Get a new instance
55                 $cacheInstance = new MemoryCache();
56
57                 // Initialize the cache
58                 $cacheInstance->initCache();
59
60                 // Return the prepared instance
61                 return $cacheInstance;
62         }
63
64         /**
65          * Initialize this cache by creating an object array
66          *
67          * @return      void
68          */
69         protected function initCache () {
70                 // Now create the "data cache"
71                 $this->dataCache = new FrameworkArrayObject('FakedDataCache');
72         }
73
74         /**
75          * Does the specified offset exist in cache?
76          *
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
81          */
82         public function offsetExists ($offset, $arrayElement = NULL, $minimumCount = 0) {
83                 // Is it there?
84                 $exists = $this->dataCache->offsetExists($offset);
85
86                 // So look for array element?
87                 if (($exists === TRUE) && (!is_null($arrayElement))) {
88                         // Get it
89                         $array = $this->offsetGet($offset);
90
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);
95                         } else {
96                                 // Not found
97                                 $exists = FALSE;
98                         }
99                 } // END - if
100
101                 // Return status
102                 return $exists;
103         }
104
105         /**
106          * Setter for cache offset
107          *
108          * @param       $offset         The offset we shall set
109          * @param       $data           Data to store in cache
110          * @return      void
111          */
112         public function offsetSet ($offset, $data) {
113                 $this->dataCache->offsetSet($offset, $data);
114         }
115
116         /**
117          * Getter for cache offset or "null" if not found
118          *
119          * @param       $offset         The offset we shall set
120          * @return      $data           Data to store in cache
121          */
122         public function offsetGet ($offset) {
123                 // Default is offset not found
124                 $data = NULL;
125
126                 // Is the offset there?
127                 if ($this->offsetExists($offset)) {
128                         // Then get the data from it
129                         $data = $this->dataCache->offsetGet($offset);
130                 } // END - if
131
132                 // Return data
133                 return $data;
134         }
135
136         /**
137          * Purges the given cache entry
138          *
139          * @param       $offset         The offset we shall set
140          * @return      void
141          */
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);
148                 } // END - if
149         }
150
151 }