7da2e1642835ec2a2a86f2ab3811d58dd6f88d8c
[mailer.git] / inc / classes / main / cache / class_MemoryCache.php
1 <?php
2 /**
3  * A simple memory cache (similar to a registry)
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.3.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.mxchange.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 class MemoryCache extends BaseFrameworkSystem implements Cacheable {
25         /**
26          * The "memory cache" is simply a wrapped object array
27          */
28         private $dataCache = null;
29
30         /**
31          * Private constructor
32          *
33          * @return      void
34          */
35         protected function __construct () {
36                 // Call parent constructor
37                 parent::__construct(__CLASS__);
38
39                 // Set part description
40                 $this->setObjectDescription("Memory cache");
41
42                 // Create unique ID number
43                 $this->createUniqueID();
44
45                 // Clean up a little
46                 $this->removeNumberFormaters();
47                 $this->removeSystemArray();
48         }
49
50         /**
51          * Creates an instance of this class
52          *
53          * @return      $cacheInstance  An instance of this cache class
54          */
55         public final static function createMemoryCache () {
56                 // Get a new instance
57                 $cacheInstance = new MemoryCache();
58
59                 // Initialize the cache
60                 $cacheInstance->initCache();
61
62                 // Return the prepared instance
63                 return $cacheInstance;
64         }
65
66         /**
67          * Initialize this cache by creating an object array
68          *
69          * @return      void
70          */
71         protected function initCache () {
72                 // Now create the "data cache"
73                 $this->dataCache = new FrameworkArrayObject();
74         }
75
76         /**
77          * Does the specified offset exist in cache?
78          *
79          * @param       $offset         The offset we are looking for
80          * @return      $exists         Wether the offset exists
81          */
82         public final function offsetExists ($offset) {
83                 $exists = $this->dataCache->offsetExists($offset);
84                 return $exists;
85         }
86
87         /**
88          * Setter for cache offset
89          *
90          * @param       $offset         The offset we shall set
91          * @param       $data           Data to store in the cache
92          * @return      void
93          */
94         public final function offsetSet ($offset, $data) {
95                 $this->dataCache->offsetSet($offset, $data);
96         }
97
98         /**
99          * Getter for cache offset or "null" if not found
100          *
101          * @param       $offset         The offset we shall set
102          * @return      $data           Data to store in the cache
103          */
104         public final function offsetget ($offset) {
105                 // Default is offset not found
106                 $data = null;
107
108                 // Is the offset there?
109                 if ($this->offsetExists($offset)) {
110                         // Then get the data from it
111                         $data = $this->dataCache->offsetGet($offset);
112                 }
113
114                 // Return data
115                 return $data;
116         }
117 }
118
119 // [EOF]
120 ?>