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

Revision 1534, 6.6 kB (checked in by mikey, 1 month ago)

refactoring #139, part 1: moved net::stubbles::util::stubRegistry to net::stubbles::lang::stubRegistry

Line 
1 <?php
2 /**
3  * Cache containers using files.
4  *
5  * @author      Frank Kleine <mikey@stubbles.net>
6  * @package     stubbles
7  * @subpackage  util_cache
8  */
9 stubClassLoader::load('net::stubbles::lang::stubRegistry',
10                       'net::stubbles::util::cache::stubAbstractCacheContainer',
11                       'net::stubbles::util::cache::stubCacheContainer'
12 );
13 /**
14  * Cache containers using files.
15  *
16  * @package     stubbles
17  * @subpackage  util_cache
18  */
19 class stubFileCacheContainer extends stubAbstractCacheContainer implements stubCacheContainer
20 {
21     /**
22      * the directory to store the cache files in
23      *
24      * @var  string
25      */
26     protected $cacheDirectory = './';
27     /**
28      * list of keys
29      *
30      * @var  array<string,string>
31      */
32     protected $keys           = null;
33     /**
34      * size of cache entries
35      *
36      * @var  array<string,int>
37      */
38     protected $size           = null;
39
40     /**
41      * sets the directory to store the cache files in
42      *
43      * @param  string  $directory
44      */
45     public function setCacheDirectory($directory)
46     {
47         $this->cacheDirectory = $directory . DIRECTORY_SEPARATOR . $this->id;
48         if (file_exists($this->cacheDirectory) === false) {
49             mkdir($this->cacheDirectory, stubRegistry::getConfig('net.stubbles.filemode', 0700), true);
50         }
51     }
52
53     /**
54      * puts date into the cache
55      *
56      * Returns amount of cached bytes or false if caching failed.
57      *
58      * @param   string    $key   key under which the data should be stored
59      * @param   string    $data  data that should be cached
60      * @return  int|bool
61      */
62     protected function doPut($key, $data)
63     {
64         $bytes = file_put_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->escapeKey($key) . '.cache', $data);
65         if (false === $bytes) {
66             return false;
67         }
68         
69         if (null !== $this->keys) {
70             $this->keys[$key] = $key;
71         }
72         
73         if (null !== $this->size) {
74             $this->size[$key] = $bytes;
75         }
76         
77         return $bytes;
78     }
79
80     /**
81      * checks whether data is cached under the given key
82      *
83      * @param   string  $key
84      * @return  bool
85      */
86     protected function doHas($key)
87     {
88         return file_exists($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->escapeKey($key) . '.cache');
89     }
90
91     /**
92      * fetches data from the cache
93      *
94      * Returns null if no data is cached under the given key.
95      *
96      * @param   string  $key
97      * @return  string
98      */
99     protected function doGet($key)
100     {
101         if ($this->doHas($key) == true) {
102             return file_get_contents($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->escapeKey($key) . '.cache');
103         }
104         
105         return null;
106     }
107
108     /**
109      * returns the time in seconds how long the data associated with $key is cached
110      *
111      * @param   string  $key
112      * @return  int
113      */
114     public function getLifeTime($key)
115     {
116         if ($this->doHas($key) == true) {
117             return (time() - filemtime($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->escapeKey($key) . '.cache'));
118         }
119         
120         return 0;
121     }
122
123     /**
124      * returns the timestamp when data associated with $key is cached
125      *
126      * @param   string  $key
127      * @return  int
128      */
129     public function getStoreTime($key)
130     {
131         if ($this->doHas($key) == true) {
132             return filemtime($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->escapeKey($key) . '.cache');
133         }
134         
135         return 0;
136     }
137
138     /**
139      * returns the allocated space of the data associated with $key in bytes
140      *
141      * @param   string  $key
142      * @return  int
143      */
144     protected function doGetSize($key)
145     {
146         if (null !== $this->size) {
147             return $this->size[$key];
148         }
149         
150         return filesize($this->cacheDirectory . DIRECTORY_SEPARATOR . $this->escapeKey($key) . '.cache');
151     }
152
153     /**
154      * returns the amount of bytes the cache data requires
155      *
156      * @return  int
157      */
158     public function getUsedSpace()
159     {
160         if (null === $this->size) {
161             $this->size = array();
162             $dirIt      = new DirectoryIterator($this->cacheDirectory);
163             foreach ($dirIt as $file) {
164                 if ($file->isDot() == true || $file->isDir() == true) {
165                     continue;
166                 }
167                 
168                 $key              = str_replace('.cache', '', $file->getFilename());
169                 $this->size[$key] = filesize($this->cacheDirectory . DIRECTORY_SEPARATOR . $key . '.cache');
170             }
171         }
172         
173         return array_sum($this->size);
174     }
175
176     /**
177      * returns a list of all keys that are stored in the cache
178      *
179      * @return  array<string>
180      */
181     public function getKeys()
182     {
183         if (null === $this->keys) {
184             $this->keys  = array();
185             $dirIt = new DirectoryIterator($this->cacheDirectory);
186             foreach ($dirIt as $file) {
187                 if ($file->isDot() == true || $file->isDir() == true) {
188                     continue;
189                 }
190                 
191                 $key = str_replace('.cache', '', $file->getFilename());
192                 if ($this->strategy->isExpired($this, $key) == true) {
193                     continue;
194                 }
195                 
196                 $this->keys[$key] = $key;
197             }
198         } else {
199             foreach ($this->keys as $key) {
200                 if ($this->strategy->isExpired($this, $key) == true) {
201                     unset($this->keys[$key]);
202                     if (null !== $this->size) {
203                         unset($this->size[$key]);
204                     }
205                 }
206             }
207         }
208         
209         return $this->keys;
210     }
211
212     /**
213      * runs the garbage collection
214      */
215     protected function doGc()
216     {
217         $dirIt = new DirectoryIterator($this->cacheDirectory);
218         foreach ($dirIt as $file) {
219             if ($file->isDot() == true || $file->isDir() == true) {
220                 continue;
221             }
222             
223             $key = str_replace('.cache', '', $file->getFilename());
224             if ($this->strategy->isExpired($this, $key) == true) {
225                 unlink($this->cacheDirectory . DIRECTORY_SEPARATOR . $key . '.cache');
226                 if (null !== $this->size) {
227                     unset($this->size[$key]);
228                 }
229             }
230         }
231     }
232
233     /**
234      * escapes the cache key
235      *
236      * @param   string  $key
237      * @return  string
238      */
239     protected function escapeKey($key)
240     {
241         return str_replace(DIRECTORY_SEPARATOR, '', $key);
242     }
243 }
244 ?>
Note: See TracBrowser for help on using the browser.