| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Container for all file-based cache methods. Inherits additional methods from <CacheCore>. Adheres
|
|
|
4 |
* to the ICacheCore interface.
|
|
|
5 |
*
|
|
|
6 |
* @version 2012.01.28
|
|
|
7 |
* @copyright 2006-2012 Ryan Parman
|
|
|
8 |
* @copyright 2006-2010 Foleeo, Inc.
|
|
|
9 |
* @copyright 2012 Amazon.com, Inc. or its affiliates.
|
|
|
10 |
* @copyright 2008-2010 Contributors
|
|
|
11 |
* @license http://opensource.org/licenses/bsd-license.php Simplified BSD License
|
|
|
12 |
* @link http://github.com/skyzyx/cachecore CacheCore
|
|
|
13 |
* @link http://getcloudfusion.com CloudFusion
|
|
|
14 |
*/
|
|
|
15 |
class CacheFile extends CacheCore implements ICacheCore
|
|
|
16 |
{
|
|
|
17 |
|
|
|
18 |
/*%******************************************************************************************%*/
|
|
|
19 |
// CONSTRUCTOR
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Constructs a new instance of this class.
|
|
|
23 |
*
|
|
|
24 |
* @param string $name (Required) A name to uniquely identify the cache object.
|
|
|
25 |
* @param string $location (Required) The location to store the cache object in. This may vary by cache method.
|
|
|
26 |
* @param integer $expires (Required) The number of seconds until a cache object is considered stale.
|
|
|
27 |
* @param boolean $gzip (Optional) Whether data should be gzipped before being stored. Defaults to true.
|
|
|
28 |
* @return object Reference to the cache object.
|
|
|
29 |
*/
|
|
|
30 |
public function __construct($name, $location, $expires, $gzip = true)
|
|
|
31 |
{
|
|
|
32 |
parent::__construct($name, $location, $expires, $gzip);
|
|
|
33 |
$this->id = $this->location . '/' . $this->name . '.cache';
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Creates a new cache.
|
|
|
38 |
*
|
|
|
39 |
* @param mixed $data (Required) The data to cache.
|
|
|
40 |
* @return boolean Whether the operation was successful.
|
|
|
41 |
*/
|
|
|
42 |
public function create($data)
|
|
|
43 |
{
|
|
|
44 |
if (file_exists($this->id))
|
|
|
45 |
{
|
|
|
46 |
return false;
|
|
|
47 |
}
|
|
|
48 |
elseif (realpath($this->location) && file_exists($this->location) && is_writeable($this->location))
|
|
|
49 |
{
|
|
|
50 |
$data = serialize($data);
|
|
|
51 |
$data = $this->gzip ? gzcompress($data) : $data;
|
|
|
52 |
|
|
|
53 |
return (bool) file_put_contents($this->id, $data);
|
|
|
54 |
}
|
|
|
55 |
elseif (realpath($this->location) && file_exists($this->location))
|
|
|
56 |
{
|
|
|
57 |
throw new CacheFile_Exception('The file system location "' . $this->location . '" is not writable. Check the file system permissions for this directory.');
|
|
|
58 |
}
|
|
|
59 |
else
|
|
|
60 |
{
|
|
|
61 |
throw new CacheFile_Exception('The file system location "' . $this->location . '" does not exist. Create the directory, or double-check any relative paths that may have been set.');
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
return false;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Reads a cache.
|
|
|
69 |
*
|
|
|
70 |
* @return mixed Either the content of the cache object, or boolean `false`.
|
|
|
71 |
*/
|
|
|
72 |
public function read()
|
|
|
73 |
{
|
|
|
74 |
if (file_exists($this->id) && is_readable($this->id))
|
|
|
75 |
{
|
|
|
76 |
$data = file_get_contents($this->id);
|
|
|
77 |
$data = $this->gzip ? gzuncompress($data) : $data;
|
|
|
78 |
$data = unserialize($data);
|
|
|
79 |
|
|
|
80 |
if ($data === false)
|
|
|
81 |
{
|
|
|
82 |
/*
|
|
|
83 |
This should only happen when someone changes the gzip settings and there is
|
|
|
84 |
existing data or someone has been mucking about in the cache folder manually.
|
|
|
85 |
Delete the bad entry since the file cache doesn't clean up after itself and
|
|
|
86 |
then return false so fresh data will be retrieved.
|
|
|
87 |
*/
|
|
|
88 |
$this->delete();
|
|
|
89 |
return false;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
return $data;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
return false;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Updates an existing cache.
|
|
|
100 |
*
|
|
|
101 |
* @param mixed $data (Required) The data to cache.
|
|
|
102 |
* @return boolean Whether the operation was successful.
|
|
|
103 |
*/
|
|
|
104 |
public function update($data)
|
|
|
105 |
{
|
|
|
106 |
if (file_exists($this->id) && is_writeable($this->id))
|
|
|
107 |
{
|
|
|
108 |
$data = serialize($data);
|
|
|
109 |
$data = $this->gzip ? gzcompress($data) : $data;
|
|
|
110 |
|
|
|
111 |
return (bool) file_put_contents($this->id, $data);
|
|
|
112 |
}
|
|
|
113 |
else
|
|
|
114 |
{
|
|
|
115 |
throw new CacheFile_Exception('The file system location is not writeable. Check your file system permissions and ensure that the cache directory exists.');
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
return false;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
/**
|
|
|
122 |
* Deletes a cache.
|
|
|
123 |
*
|
|
|
124 |
* @return boolean Whether the operation was successful.
|
|
|
125 |
*/
|
|
|
126 |
public function delete()
|
|
|
127 |
{
|
|
|
128 |
if (file_exists($this->id))
|
|
|
129 |
{
|
|
|
130 |
return unlink($this->id);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
return false;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* Checks whether the cache object is expired or not.
|
|
|
138 |
*
|
|
|
139 |
* @return boolean Whether the cache is expired or not.
|
|
|
140 |
*/
|
|
|
141 |
public function is_expired()
|
|
|
142 |
{
|
|
|
143 |
if ($this->timestamp() + $this->expires < time())
|
|
|
144 |
{
|
|
|
145 |
return true;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
return false;
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
* Retrieves the timestamp of the cache.
|
|
|
153 |
*
|
|
|
154 |
* @return mixed Either the Unix time stamp of the cache creation, or boolean `false`.
|
|
|
155 |
*/
|
|
|
156 |
public function timestamp()
|
|
|
157 |
{
|
|
|
158 |
clearstatcache();
|
|
|
159 |
|
|
|
160 |
if (file_exists($this->id))
|
|
|
161 |
{
|
|
|
162 |
$this->timestamp = filemtime($this->id);
|
|
|
163 |
return $this->timestamp;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
return false;
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
/**
|
|
|
170 |
* Resets the freshness of the cache.
|
|
|
171 |
*
|
|
|
172 |
* @return boolean Whether the operation was successful.
|
|
|
173 |
*/
|
|
|
174 |
public function reset()
|
|
|
175 |
{
|
|
|
176 |
if (file_exists($this->id))
|
|
|
177 |
{
|
|
|
178 |
return touch($this->id);
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
return false;
|
|
|
182 |
}
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
|
|
|
186 |
/*%******************************************************************************************%*/
|
|
|
187 |
// EXCEPTIONS
|
|
|
188 |
|
|
|
189 |
class CacheFile_Exception extends CacheCore_Exception {}
|