Continued with renaming-season:
[core.git] / framework / main / classes / cache / class_MemoryCache.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Cache;
4
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
7
8 /**
9  * A simple memory cache (similar to a registry)
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
13  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14  * @license             GNU GPL 3.0 or any newer version
15  * @link                http://www.shipsimu.org
16  * @todo                Rename to InProgressCache
17  *
18  * This program is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program. If not, see <http://www.gnu.org/licenses/>.
30  */
31 class MemoryCache extends BaseFrameworkSystem implements Cacheable {
32         /**
33          * The "memory cache" is simply a wrapped object array
34          */
35         private $dataCache = NULL;
36
37         /**
38          * Protected constructor
39          *
40          * @return      void
41          */
42         protected function __construct () {
43                 // Call parent constructor
44                 parent::__construct(__CLASS__);
45         }
46
47         /**
48          * Creates an instance of this class
49          *
50          * @return      $cacheInstance  An instance of this cache class
51          */
52         public static final function createMemoryCache () {
53                 // Get a new instance
54                 $cacheInstance = new MemoryCache();
55
56                 // Initialize the cache
57                 $cacheInstance->initCache();
58
59                 // Return the prepared instance
60                 return $cacheInstance;
61         }
62
63         /**
64          * Initialize this cache by creating an object array
65          *
66          * @return      void
67          */
68         protected function initCache () {
69                 // Now create the "data cache"
70                 $this->dataCache = new FrameworkArrayObject('FakedDataCache');
71         }
72
73         /**
74          * Does the specified offset exist in cache?
75          *
76          * @param       $offset                 The offset we are looking for
77          * @param       $arrayElement   If type is array, then this element must be found
78          * @param       $minimumCount   If array element is found then this count must at least match
79          * @return      $exists                 Whether the offset exists
80          */
81         public function offsetExists ($offset, $arrayElement = NULL, $minimumCount = 0) {
82                 // Is it there?
83                 $exists = $this->dataCache->offsetExists($offset);
84
85                 // So look for array element?
86                 if (($exists === TRUE) && (!is_null($arrayElement))) {
87                         // Get it
88                         $array = $this->offsetGet($offset);
89
90                         // Is it an array and element is found?
91                         if ((is_array($array)) && (isset($array[$arrayElement]))) {
92                                 // Is an array and element is found, so check count
93                                 $exists = (count($array[$arrayElement]) >= $minimumCount);
94                         } else {
95                                 // Not found
96                                 $exists = FALSE;
97                         }
98                 } // END - if
99
100                 // Return status
101                 return $exists;
102         }
103
104         /**
105          * Setter for cache offset
106          *
107          * @param       $offset         The offset we shall set
108          * @param       $data           Data to store in cache
109          * @return      void
110          */
111         public function offsetSet ($offset, $data) {
112                 $this->dataCache->offsetSet($offset, $data);
113         }
114
115         /**
116          * Getter for cache offset or "null" if not found
117          *
118          * @param       $offset         The offset we shall set
119          * @return      $data           Data to store in cache
120          */
121         public function offsetGet ($offset) {
122                 // Default is offset not found
123                 $data = NULL;
124
125                 // Is the offset there?
126                 if ($this->offsetExists($offset)) {
127                         // Then get the data from it
128                         $data = $this->dataCache->offsetGet($offset);
129                 } // END - if
130
131                 // Return data
132                 return $data;
133         }
134
135         /**
136          * Purges the given cache entry
137          *
138          * @param       $offset         The offset we shall set
139          * @return      void
140          */
141         public function purgeOffset ($offset) {
142                 // Is the offset there?
143                 if ($this->offsetExists($offset)) {
144                         // Purge only existing keys
145                         //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHE: Unsetting cache ' . $offset);
146                         $this->dataCache->offsetUnset($offset);
147                 } // END - if
148         }
149
150 }