/
caches_filesystem.php
113 lines (97 loc) · 2.83 KB
/
caches_filesystem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php /*
ocPortal
Copyright (c) ocProducts, 2004-2012
See text/EN/licence.txt for full licencing information.
NOTE TO PROGRAMMERS:
Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
**** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****
*/
/**
* @license http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
* @copyright ocProducts Ltd
* @package core
*/
/**
* Cache Driver.
* @package core
*/
class filecache
{
/**
* (Plug-in replacement for memcache API) Get data from the persistent cache.
*
* @param mixed Key
* @param ?TIME Minimum timestamp that entries from the cache may hold (NULL: don't care)
* @return ?mixed The data (NULL: not found / NULL entry)
*/
function get($key,$min_cache_date=NULL)
{
$myfile=@fopen(get_custom_file_base().'/persistent_cache/'.md5($key).'.gcd','rb');
if ($myfile===false) return NULL;
if (!is_null($min_cache_date)) // Code runs here as we know file exists at this point
{
if (filemtime(get_custom_file_base().'/persistent_cache/'.md5($key).'.gcd')<$min_cache_date)
{
fclose($myfile);
return NULL;
}
}
flock($myfile,LOCK_SH);
$contents='';
while (!feof($myfile)) $contents.=fread($myfile,1024);
$ret=@unserialize($contents);
flock($myfile,LOCK_UN);
fclose($myfile);
return $ret;
}
/**
* (Plug-in replacement for memcache API) Put data into the persistent cache.
*
* @param mixed Key
* @param mixed The data
* @param integer Various flags (parameter not used)
* @param integer The expiration time in seconds.
*/
function set($key,$data,$flags,$expire_secs)
{
unset($flags);
unset($expire_secs);
$to_write=serialize($data);
$path=get_custom_file_base().'/persistent_cache/'.md5($key).'.gcd';
$myfile=@fopen($path,'ab');
if ($myfile===false) return; // Failure
flock($myfile,LOCK_EX);
ftruncate($myfile,0);
if (fwrite($myfile,$to_write)!==false)
{
// Success
flock($myfile,LOCK_UN);
fclose($myfile);
fix_permissions($path);
} else
{
// Failure
flock($myfile,LOCK_UN);
fclose($myfile);
unlink($path);
}
}
/**
* (Plug-in replacement for memcache API) Delete data from the persistent cache.
*
* @param mixed Key name
*/
function delete($key)
{
// Ideally we'd lock whilst we delete, but it's not stable (and the workaround would be too slow for our efficiency context). So some people reading may get errors whilst we're clearing the cache. Fortunately this is a rare op to perform.
@unlink(get_custom_file_base().'/persistent_cache/'.md5($key).'.gcd');
}
/**
* (Plug-in replacement for memcache API) Remove all data from the persistent cache.
*/
function flush()
{
require_code('caches');
persistent_cache_empty();
}
}