root/trunk/src/main/php/net/stubbles/util/cache/stubAbstractCacheContainer.php

Revision 1307, 4.6 kB (checked in by mikey, 4 months ago)

fixed several class loading issues

Line 
1 <?php
2 /**
3  * Abstract base class for cache containers.
4  *
5  * @author      Frank Kleine <mikey@stubbles.net>
6  * @package     stubbles
7  * @subpackage  util_cache
8  */
9 stubClassLoader::load('net::stubbles::util::cache::stubCacheContainer');
10 /**
11  * Abstract base class for cache containers.
12  *
13  * @package     stubbles
14  * @subpackage  util_cache
15  */
16 abstract class stubAbstractCacheContainer extends stubSerializableObject implements stubCacheContainer
17 {
18     /**
19      * the strategy used for decisions about caching
20      *
21      * @var  stubCacheStrategy
22      */
23     protected $strategy;
24     /**
25      * id of the container
26      *
27      * @var  string
28      */
29     protected $id;
30     /**
31      * unix timestamp when last run of garbage collection happened
32      *
33      * @var  int
34      */
35     protected $lastGcRun = null;
36
37     /**
38      * constructor
39      *
40      * @param  string             $id        id of the container
41      */
42     public function __construct($id)
43     {
44         $this->id = $id;
45     }
46
47     /**
48      * sets the id of the container
49      *
50      * @param  string  $id
51      */
52     public function setId($id)
53     {
54         $this->id = $id;
55     }
56
57     /**
58      * returns the id of the container
59      *
60      * @return  string
61      */
62     public function getId()
63     {
64         return $this->id;
65     }
66
67     /**
68      * sets the strategy the container should use
69      *
70      * @param  stubCacheStrategy  $strategy
71      */
72     public function setStrategy(stubCacheStrategy $strategy)
73     {
74         $this->strategy = $strategy;
75     }
76
77     /**
78      * puts date into the cache
79      *
80      * Returns amount of cached bytes or false if caching failed.
81      *
82      * @param   string    $key   key under which the data should be stored
83      * @param   string    $data  data that should be cached
84      * @return  int|bool
85      */
86     public function put($key, $data)
87     {
88         if ($this->strategy->isCachable($this, $key, $data) == false) {
89             return false;
90         }
91         
92         return $this->doPut($key, $data);
93     }
94
95     /**
96      * puts date into the cache
97      *
98      * Returns amount of cached bytes or false if caching failed.
99      *
100      * @param   string    $key   key under which the data should be stored
101      * @param   string    $data  data that should be cached
102      * @return  int|bool
103      */
104     protected abstract function doPut($key, $data);
105
106     /**
107      * checks whether data is cached under the given key
108      *
109      * @param   string  $key
110      * @return  bool
111      */
112     public function has($key)
113     {
114         if ($this->strategy->isExpired($this, $key) == true) {
115             return false;
116         }
117         
118         return $this->doHas($key);
119     }
120
121     /**
122      * checks whether data is cached under the given key
123      *
124      * @param   string  $key
125      * @return  bool
126      */
127     protected abstract function doHas($key);
128
129     /**
130      * checks whether cache data is expired
131      *
132      * @param   string  $key   key under which the data is stored
133      * @return  bool
134      */
135     public function isExpired($key)
136     {
137         return $this->strategy->isExpired($this, $key);
138     }
139
140     /**
141      * fetches data from the cache
142      *
143      * Returns null if no data is cached under the given key.
144      *
145      * @param   string  $key
146      * @return  string
147      */
148     public function get($key)
149     {
150         if ($this->strategy->isExpired($this, $key) == true) {
151             return null;
152         }
153         
154         return $this->doGet($key);
155     }
156
157     /**
158      * fetches data from the cache
159      *
160      * Returns null if no data is cached under the given key.
161      *
162      * @param   string  $key
163      * @return  string
164      */
165     protected abstract function doGet($key);
166
167     /**
168      * returns the allocated space of the data associated with $key in bytes
169      *
170      * @param   string  $key
171      * @return  int
172      */
173     public function getSize($key)
174     {
175         if ($this->has($key) == true) {
176             return $this->doGetSize($key);
177         }
178         
179         return 0;
180     }
181     
182     /**
183      * returns the allocated space of the data associated with $key in bytes
184      *
185      * @param   string  $key
186      * @return  int
187      */
188     protected abstract function doGetSize($key);
189
190     /**
191      * returns the unix timestamp of the last run of the garbage collection
192      *
193      * @return  int
194      */
195     public function lastGcRun()
196     {
197         return $this->lastGcRun;
198     }
199
200     /**
201      * runs the garbage collection
202      */
203     public function gc()
204     {
205         if ($this->strategy->shouldRunGc($this) == true) {
206             $this->doGc();
207             $this->lastGcRun = time();
208         }
209     }
210
211     /**
212      * runs the garbage collection
213      */
214     protected abstract function doGc();
215 }
216 ?>
Note: See TracBrowser for help on using the browser.