-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCache.php
168 lines (156 loc) · 4.67 KB
/
Cache.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
@file
@brief A Caching Interface for Filesystem or Memcache
@copyright 2005 Edoceo, Inc.
@package radix
@see http://pecl.php.net/memcached (aka: dev-php5/pecl-memcached)
@see http://php.net/manual/en/book.memcached.php
*/
namespace Edoceo\Radix;
/**
@brief A Caching Interface for File or Memory caching
*/
class Cache
{
private static $_kind; //! dir|mem
private static $_path;
private static $_mem; //! Handle for Memcache
private static $_host = 'locahost';
private static $_port = 11211;
private static $_time = 3600; // Default Storage Time
private static $_stat;
/**
@param $opt array of options: kind, path, host, port, time
*/
public static function init($opt=null)
{
// Default Kind of Cache
if (empty($opt['kind'])) {
$opt['kind'] = 'dir';
}
// Initialise Connetion
switch ($opt['kind']) {
case 'dir':
case 'file':
case 'path':
self::$_kind = 'dir';
if (!is_dir($opt['path'])) {
mkdir($opt['path']);
}
self::$_path = $opt['path'];
break;
case 'mem':
case 'memcache':
case 'memcached':
case 'ram':
if (empty($opt['host'])) $opt['host'] = 'localhost';
if (empty($pot['port'])) $opt['port'] = 11211;
self::$_kind = 'mem';
if (empty(self::$_mem)) {
self::$_mem = new \Memcached();
// self::$_mem->setOption(Memcached::SERIALIZER_IGBINARY,true);
self::$_mem->addServer($opt['host'],$opt['port']);
}
break;
}
// Update Default TTL
if (!empty($opt['time']) && intval($opt['time']) > 0 ) {
self::$_time = $opt['time'];
}
}
/**
@todo want to be able to make an instance of this thing too
*/
public function __construct()
{
}
/**
Reads the item from cache
@param $name of the item to fetch
@param $miss value to return on cache-miss
@todo do we want to log cache misses?
@return data|$miss
*/
public static function get($name,$miss=false)
{
$r = $miss;
switch (self::$_kind) {
case 'dir':
$file = self::_make_path($name);
if (is_file($file)) {
if (filemtime($file) > time()) {
$r = unserialize(file_get_contents($file));
} else {
unlink($file);
}
}
break;
case 'mem':
$r = self::$_mem->get(sha1($name));
}
return($r);
}
/**
Saves items in cache for at most one day (86400) (half 43200)
@param $name name of cache item
@param $data automatically serialized
@param $time duration to cache, in seconds, default 3600
@return true|false
*/
public static function put($name,$data,$time=false)
{
$r = false;
if ($time === false) $time = self::$_time;
switch (self::$_kind) {
case 'dir':
$file = self::_make_path($name);
if (!is_dir(dirname($file))) {
mkdir(dirname($file),0766,true);
}
$r = file_put_contents($file,serialize($data));
$r = touch($file,time()+$time);
break;
case 'mem':
// if (strpos($name,'RAND')) {
// $time = 30; // Results of RAND are cached only for 30s
// }
$r = self::$_mem->set(sha1($name),$data,$time);
if ($r !== 0) {
self::$_stat = sprintf('Error: %d %s',self::$_mem->getResultCode(),self::$_mem->getResultMessage());
}
}
return $r;
}
/**
Remove an Item from the Cache
*/
public static function del($name)
{
$r = false; // Return Value
switch (self::$_kind) {
case 'dir':
$file = self::_make_path($name);
if (is_file($file)) {
unlink($file);
$r = true;
}
break;
case 'mem':
$r = self::$_mem->delete(sha1($name));
if ($r !== 0) {
self::$_stat = sprintf('Error: %d %s',self::$_mem->getResultCode(),self::$_mem->getResultMessage());
}
}
return $r;
}
/**
Creates a directory path tree from the key
@param $x Cache Key
@return a full path
*/
private static function _make_path($x)
{
return self::$_path . '/' . preg_replace('/([0-9a-f]{4})/','/$1',sha1($x));
}
}