2626class Q2A_Storage_FileCacheDriver implements Q2A_Storage_CacheDriver
2727{
2828 private $ enabled = false ;
29+ private $ keyPrefix = '' ;
2930 private $ error ;
3031 private $ cacheDir ;
3132
@@ -39,6 +40,10 @@ public function __construct($config)
3940 return ;
4041 }
4142
43+ if (isset ($ config ['keyprefix ' ])) {
44+ $ this ->keyPrefix = $ config ['keyprefix ' ];
45+ }
46+
4247 if (isset ($ config ['dir ' ])) {
4348 $ this ->cacheDir = realpath ($ config ['dir ' ]);
4449
@@ -67,14 +72,15 @@ public function get($key)
6772 return null ;
6873 }
6974
70- $ file = $ this ->getFilename ($ key );
75+ $ fullKey = $ this ->keyPrefix . $ key ;
76+ $ file = $ this ->getFilename ($ fullKey );
7177
7278 if (is_readable ($ file )) {
7379 $ lines = file ($ file , FILE_IGNORE_NEW_LINES );
7480 $ actualKey = array_shift ($ lines );
7581
7682 // double check this is the correct data
77- if ($ key === $ actualKey ) {
83+ if ($ fullKey === $ actualKey ) {
7884 $ expiry = array_shift ($ lines );
7985
8086 if (is_numeric ($ expiry ) && time () < $ expiry ) {
@@ -103,13 +109,14 @@ public function set($key, $data, $ttl)
103109 {
104110 $ success = false ;
105111 $ ttl = (int ) $ ttl ;
112+ $ fullKey = $ this ->keyPrefix . $ key ;
106113
107114 if ($ this ->enabled && $ ttl > 0 ) {
108115 $ encData = serialize ($ data );
109116 $ expiry = time () + ($ ttl * 60 );
110- $ cache = $ key . "\n" . $ expiry . "\n" . $ encData ;
117+ $ cache = $ fullKey . "\n" . $ expiry . "\n" . $ encData ;
111118
112- $ file = $ this ->getFilename ($ key );
119+ $ file = $ this ->getFilename ($ fullKey );
113120 $ dir = dirname ($ file );
114121 if (is_dir ($ dir ) || mkdir ($ dir , 0777 , true )) {
115122 $ success = @file_put_contents ($ file , $ cache ) !== false ;
@@ -127,10 +134,10 @@ public function set($key, $data, $ttl)
127134 */
128135 public function delete ($ key )
129136 {
130- if ($ this ->enabled ) {
131- $ file = $ this ->getFilename ($ key );
132- $ dir = dirname ($ key );
137+ $ fullKey = $ this ->keyPrefix . $ key ;
133138
139+ if ($ this ->enabled ) {
140+ $ file = $ this ->getFilename ($ fullKey );
134141 return $ this ->deleteFile ($ file );
135142 }
136143
@@ -208,6 +215,16 @@ public function getError()
208215 return $ this ->error ;
209216 }
210217
218+ /**
219+ * Get the prefix used for all cache keys.
220+ *
221+ * @return string
222+ */
223+ public function getKeyPrefix ()
224+ {
225+ return $ this ->keyPrefix ;
226+ }
227+
211228 /**
212229 * Get current statistics for the cache.
213230 *
@@ -255,13 +272,13 @@ private function deleteFile($file)
255272
256273 /**
257274 * Generates filename for cache key, of the form `1/23/123abc`
258- * @param string $key The unique cache key.
275+ * @param string $key The unique cache key (including prefix) .
259276 *
260277 * @return string
261278 */
262- private function getFilename ($ key )
279+ private function getFilename ($ fullKey )
263280 {
264- $ filename = sha1 ($ key );
281+ $ filename = sha1 ($ fullKey );
265282 return $ this ->cacheDir . '/ ' . substr ($ filename , 0 , 1 ) . '/ ' . substr ($ filename , 1 , 2 ) . '/ ' . $ filename ;
266283 }
267284}
0 commit comments