forked from unlcms/UNL-CMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewrite.php
executable file
·65 lines (51 loc) · 1.61 KB
/
rewrite.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
#!/usr/bin/php
<?php
$stdin = fopen('php://stdin', 'r');
$cache = new RewriteCache();
// Each mapping request is on its own line.
while ($line = fgets($stdin)) {
// Remove the trailing newline
$line = trim($line, "\n");
// Check for this result in the cache
if (!($route = $cache->get($line))) {
$output = array();
exec('/usr/bin/php ' . __DIR__ . '/rewrite_miss.php ' . escapeshellarg($line), $output, $return_var);
// Set default route
$route = 'NULL';
if (!$return_var && isset($output[0])) {
//Success! a route was found
$route = $output[0];
}
$cache->set($line, $route);
}
echo $route . PHP_EOL;
}
// A basic in-memory cache that with a Least Recently Used expiration policy with a limitted number of entries.
class RewriteCache {
protected $_storage;
protected $_cache_size;
protected $_lifetime;
function __construct($cache_size = 1000, $lifetime = 30) {
$this->_storage = array();
$this->_cache_size = $cache_size;
$this->_lifetime = $lifetime;
}
function get($key) {
if (isset($this->_storage[$key])) {
$entry = $this->_storage[$key];
unset($this->_storage[$key]);
// If the entry isn't expired, promote it to the front and return it.
if ($entry['time'] + $this->_lifetime > time()) {
$this->_storage[$key] = $entry;
return $entry['data'];
}
}
return FALSE;
}
function set($key, $value) {
if (count($this->_storage) >= $this->_cache_size) {
array_shift($this->_storage);
}
$this->_storage[$key] = array('time' => time(), 'data' => $value);
}
}