-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcache.php
100 lines (94 loc) · 2.94 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
<?php
// Remove expired cache files and empty directories
$directory_expirations = [
"/progress/" => PROGRESS_EXPIRATION,
"/auth/" => AUTH_EXPIRATION,
"/about/" => ABOUT_EXPIRATION,
"/hot_posts/" => HOT_POSTS_EXPIRATION,
"/top_posts/" => TOP_POSTS_EXPIRATION,
"/top_posts_day/" => TOP_DAILY_POSTS_EXPIRATION,
"/top_posts_month/" => TOP_MONTHLY_POSTS_EXPIRATION,
"/comments/" => COMMENTS_EXPIRATION,
"/communities/hacker_news/" => TOP_POSTS_EXPIRATION,
"/communities/lemmy/" => TOP_POSTS_EXPIRATION,
"/galleries/" => GALLERY_EXPIRATION,
"/rss/" => RSS_EXPIRATION,
"/webpages/" => WEBPAGE_EXPIRATION,
"/images/" => IMAGE_EXPIRATION
];
if (!is_dir("cache")) mkdir("cache", 0755, true);
for ($i = 0; $i < 10; $i++) {
if (is_dir("cache")) break;
usleep(100000);
}
$cache_directory_iterator = new RecursiveDirectoryIterator("cache") ?? [];
foreach (new RecursiveIteratorIterator($cache_directory_iterator) as $item) {
if (is_file($item)) {
foreach ($directory_expirations as $search => $expiration) {
if (
is_file($item) &&
strpos($item->getPathname(), $search) !== false &&
time() - filemtime($item) >= $expiration
) {
unlink($item->getPathname());
}
}
} elseif (
!empty($item) &&
is_dir($item) &&
is_array(scandir($item)) &&
count(scandir($item)) == 2 &&
$item->getPathname() !== 'cache'
) {
$parent = $item->getPath();
while (count(scandir($parent)) == 2 && $parent !== 'cache') {
if (!@rmdir($parent)) {
// Directory is busy, skip it
break;
}
$parent = dirname($parent);
}
}
}
/**
* Get cache directory size
* @return int Cache directory size in bytes
*/
function getCacheDirectorySize()
{
$cache_size = 0;
// Get size of cache directory if it exists
if (!is_dir("cache")) return $cache_size;
$cache_directory_iterator = new RecursiveDirectoryIterator("cache");
foreach (new RecursiveIteratorIterator($cache_directory_iterator) as $item) {
if (is_file($item)) $cache_size += $item->getSize();
}
return $cache_size;
}
/**
* Get total cache size
* @return string Cache size in human readable format
*/
function getCacheSize()
{
$cache_size = 0;
// Get Redis cache size if available
if (REDIS) {
$client = new Predis\Client(array(
'scheme' => 'tcp',
'host' => REDIS_HOST,
'port' => REDIS_PORT,
'timeout' => 0.5
));
$keys = $client->keys('upvote_rss*');
foreach ($keys as $key) {
if (!empty($key) && $client->exists($key)) {
$cache_size += strlen($key) + strlen($client->get($key));
}
}
}
// Get size of cache directory if it exists
$cache_size += getCacheDirectorySize();
return formatByteSize($cache_size);
}
define('CACHE_SIZE', getCacheSize());