]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/cache/class_MemoryCache.php
Continued with renaming-season:
[core.git] / framework / main / classes / cache / class_MemoryCache.php
diff --git a/framework/main/classes/cache/class_MemoryCache.php b/framework/main/classes/cache/class_MemoryCache.php
new file mode 100644 (file)
index 0000000..fb926f4
--- /dev/null
@@ -0,0 +1,150 @@
+<?php
+// Own namespace
+namespace CoreFramework\Cache;
+
+// Import framework stuff
+use CoreFramework\Object\BaseFrameworkSystem;
+
+/**
+ * A simple memory cache (similar to a registry)
+ *
+ * @author             Roland Haeder <webmaster@shipsimu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.shipsimu.org
+ * @todo               Rename to InProgressCache
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+class MemoryCache extends BaseFrameworkSystem implements Cacheable {
+       /**
+        * The "memory cache" is simply a wrapped object array
+        */
+       private $dataCache = NULL;
+
+       /**
+        * Protected constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+       }
+
+       /**
+        * Creates an instance of this class
+        *
+        * @return      $cacheInstance  An instance of this cache class
+        */
+       public static final function createMemoryCache () {
+               // Get a new instance
+               $cacheInstance = new MemoryCache();
+
+               // Initialize the cache
+               $cacheInstance->initCache();
+
+               // Return the prepared instance
+               return $cacheInstance;
+       }
+
+       /**
+        * Initialize this cache by creating an object array
+        *
+        * @return      void
+        */
+       protected function initCache () {
+               // Now create the "data cache"
+               $this->dataCache = new FrameworkArrayObject('FakedDataCache');
+       }
+
+       /**
+        * Does the specified offset exist in cache?
+        *
+        * @param       $offset                 The offset we are looking for
+        * @param       $arrayElement   If type is array, then this element must be found
+        * @param       $minimumCount   If array element is found then this count must at least match
+        * @return      $exists                 Whether the offset exists
+        */
+       public function offsetExists ($offset, $arrayElement = NULL, $minimumCount = 0) {
+               // Is it there?
+               $exists = $this->dataCache->offsetExists($offset);
+
+               // So look for array element?
+               if (($exists === TRUE) && (!is_null($arrayElement))) {
+                       // Get it
+                       $array = $this->offsetGet($offset);
+
+                       // Is it an array and element is found?
+                       if ((is_array($array)) && (isset($array[$arrayElement]))) {
+                               // Is an array and element is found, so check count
+                               $exists = (count($array[$arrayElement]) >= $minimumCount);
+                       } else {
+                               // Not found
+                               $exists = FALSE;
+                       }
+               } // END - if
+
+               // Return status
+               return $exists;
+       }
+
+       /**
+        * Setter for cache offset
+        *
+        * @param       $offset         The offset we shall set
+        * @param       $data           Data to store in cache
+        * @return      void
+        */
+       public function offsetSet ($offset, $data) {
+               $this->dataCache->offsetSet($offset, $data);
+       }
+
+       /**
+        * Getter for cache offset or "null" if not found
+        *
+        * @param       $offset         The offset we shall set
+        * @return      $data           Data to store in cache
+        */
+       public function offsetGet ($offset) {
+               // Default is offset not found
+               $data = NULL;
+
+               // Is the offset there?
+               if ($this->offsetExists($offset)) {
+                       // Then get the data from it
+                       $data = $this->dataCache->offsetGet($offset);
+               } // END - if
+
+               // Return data
+               return $data;
+       }
+
+       /**
+        * Purges the given cache entry
+        *
+        * @param       $offset         The offset we shall set
+        * @return      void
+        */
+       public function purgeOffset ($offset) {
+               // Is the offset there?
+               if ($this->offsetExists($offset)) {
+                       // Purge only existing keys
+                       //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CACHE: Unsetting cache ' . $offset);
+                       $this->dataCache->offsetUnset($offset);
+               } // END - if
+       }
+
+}