-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCache.class.php
More file actions
173 lines (145 loc) · 3.95 KB
/
Copy pathCache.class.php
File metadata and controls
173 lines (145 loc) · 3.95 KB
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
169
170
171
172
173
<?
/**
* Generic wemo metadata cache stub class
*
* This does nothing, but can be used to disable caching
*/
class Cache {
public static function get(
$key
){
return false;
}
public static function getAll(){
return false;
}
public static function set(
$key,
$value
){
return false;
}
}
/**
* A class to handle caching of values
*
* Designed to use a keyvalue store (redis, memcache), but will use a flatfile for now
*/
class FlatFileCache {
const CACHE_FILE = "/tmp/wemo-cache";
const DELIMITER = "\t";
public static function createCacheFile (){
if (!file_exists(self::CACHE_FILE))
touch(self::CACHE_FILE);
}
/**
* Get a value for the specified key
* @param key The key to get the value for
* @return The value stored in the cache for key
*/
public static function get(
$key
){
//echo "FlatFileCache::get($key)\n";
self::createCacheFile();
foreach (file(self::CACHE_FILE) as $line){
$pieces = explode(self::DELIMITER, $line);
$cacheKey = $pieces[0];
$cacheValue = $pieces[1];
if ($cacheKey == $key){
//echo "Cache hit for " . $key . "\n";
return $cacheValue;
}
}
return false;
}
/**
* Get a all records from the cache
* @return An array with all records in the cache in format array[KEY] = VALUE
*/
public static function getAll(){
//echo "FlatFileCache::getAll()\n";
self::createCacheFile();
$ret = array();
foreach (file(self::CACHE_FILE) as $line){
$pieces = explode(self::DELIMITER, $line);
$cacheKey = $pieces[0];
$cacheValue = $pieces[1];
$ret[$cacheKey] = $cacheValue;
}
return $ret;
}
/**
* Get all the keys from the cache
* @return An array with all keys from the cache
*/
public static function getAllKeys(){
return array_keys(self::getAll());
}
/**
* Set the value for the provided key
* @param key The key to set (must be unique)
* @param value The value to set for key
*
* @return true if the write is successful, false otherwise
*/
public static function set(
$key,
$value
){
$debug = new Debug();
$debug->log("FlatFileCache::set(" . $key . ", " . $value . ")");
$out = $key . self::DELIMITER . $value . "\n";
$readKeys[] = $key;
foreach (file(self::CACHE_FILE) as $line){
$pieces = explode(self::DELIMITER, $line);
$cacheKey = $pieces[0];
$cacheValue = $pieces[1];
if (in_array($cacheKey, $readKeys)){
continue;
} else {
$out .= $line;
$readKeys[] = $cacheKey;
}
}
$debug->log(array("writing to cache:", $out));
$fh = fopen(self::CACHE_FILE, "w");
flock($fh, LOCK_EX);
$res = fwrite($fh, $out);
flock($fh, LOCK_UN);
if ($res > 0)
return true;
else
return false;
}
}
class MemcachedCache {
const SERVER = "localhost";
const HOST = 11211;
private static function connect(){
$memcache = new Memcache;
if ($memcache->connect(self::SERVER, self::HOST))
return $memcache;
else
return false;
}
public static function get(
$key
){
if ($memcache = self::connect())
return $memcache->get($key);
}
public static function getAll(){
foreach ($ips as $ip) // use config IPs
$ret[$ip] = self::get($ip);
return $ret;
}
public static function set(
$key,
$value
){
if ($memcache = self::connect())
return $memcache->set($key, $value);
}
}
?>