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