Skip to content

Commit 3d1b2b7

Browse files
committed
Ability to disable unixLoadUpperThreshold feature, and warning for Windows users that this feature will not work for them
1 parent 708640d commit 3d1b2b7

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ There are several different configuration variables you can override. The table
3535
| `cacheDirectory` | The directory in which you wish the cache files to be stored. We recommend you change this to a site-specific directory and ensure it is outside of the web root. You must include a trailing slash. | `/tmp/rwFileCacheStorage/` |
3636
| `gzipCompression` | Whether or not to compress cache files using gzip. Unless you are storing very small values in your cache files, we recommend you leave this enabled. | `true` |
3737
| `fileExtension` | The file extension that will be appended to all your cache files. | `cache` |
38-
| `unixLoadUpperThreshold` | If your server's load is greater than this value, cache files will be returned regardless of whether they have expired. This can be used to prevent cache files being regenerated when server load is high. If you do not wish to use this feature, set this option to an obscenely high value. | `4.00` |
38+
| `unixLoadUpperThreshold` | If your server's load is greater than this value, cache files will be returned regardless of whether they have expired. This can be used to prevent cache files being regenerated when server load is high. If you do not wish to use this feature, set this option to `-1`. | `4.00` |
3939

4040
### Setting a cache item
4141

src/RWFileCache.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace rapidweb\RWFileCache;
44

5+
use Exception;
6+
57
class RWFileCache
68
{
79
/**
@@ -112,7 +114,15 @@ public function get($key)
112114
return false;
113115
}
114116

115-
$unixLoad = sys_getloadavg();
117+
if (!function_exists('sys_getloadavg')) {
118+
throw new Exception('Your PHP installation does not support `sys_getloadavg` (Windows?). Please set `unixLoadUpperThreshold` to `-1` in your RWFileCache config.');
119+
}
120+
121+
if ($this->config['unixLoadUpperThreshold']==-1) {
122+
$unixLoad = [0 => PHP_INT_MAX, 1 => PHP_INT_MAX, 2 => PHP_INT_MAX];
123+
} else {
124+
$unixLoad = sys_getloadavg();
125+
}
116126

117127
if ($cacheObj->expiryTimestamp > time() || $unixLoad[0] >= $this->config['unixLoadUpperThreshold']) {
118128
// Cache item has not yet expired or system load is too high

0 commit comments

Comments
 (0)