]> git.mxchange.org Git - hub.git/blobdiff - inc/classes/main/cache/class_MemoryCache.php
Code merge from latest Ship-Simu code
[hub.git] / inc / classes / main / cache / class_MemoryCache.php
diff --git a/inc/classes/main/cache/class_MemoryCache.php b/inc/classes/main/cache/class_MemoryCache.php
new file mode 100644 (file)
index 0000000..7da2e16
--- /dev/null
@@ -0,0 +1,120 @@
+<?php
+/**
+ * A simple memory cache (similar to a registry)
+ *
+ * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @version            0.3.0
+ * @copyright  Copyright(c) 2007, 2008 Roland Haeder, this is free software
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.mxchange.org
+ *
+ * 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;
+
+       /**
+        * Private constructor
+        *
+        * @return      void
+        */
+       protected function __construct () {
+               // Call parent constructor
+               parent::__construct(__CLASS__);
+
+               // Set part description
+               $this->setObjectDescription("Memory cache");
+
+               // Create unique ID number
+               $this->createUniqueID();
+
+               // Clean up a little
+               $this->removeNumberFormaters();
+               $this->removeSystemArray();
+       }
+
+       /**
+        * Creates an instance of this class
+        *
+        * @return      $cacheInstance  An instance of this cache class
+        */
+       public final static 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();
+       }
+
+       /**
+        * Does the specified offset exist in cache?
+        *
+        * @param       $offset         The offset we are looking for
+        * @return      $exists         Wether the offset exists
+        */
+       public final function offsetExists ($offset) {
+               $exists = $this->dataCache->offsetExists($offset);
+               return $exists;
+       }
+
+       /**
+        * Setter for cache offset
+        *
+        * @param       $offset         The offset we shall set
+        * @param       $data           Data to store in the cache
+        * @return      void
+        */
+       public final 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 the cache
+        */
+       public final 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);
+               }
+
+               // Return data
+               return $data;
+       }
+}
+
+// [EOF]
+?>