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