expire_cached_files to DiskCache::expire()

This commit is contained in:
Andrew Dolgov 2019-08-13 14:13:42 +03:00
parent 133c2b482b
commit 19b9b27662
2 changed files with 39 additions and 53 deletions

View File

@ -1,76 +1,63 @@
<?php <?php
class DiskCache class DiskCache {
{
private $dir; private $dir;
public function __construct($dir) public function __construct($dir) {
{
$this->dir = basename($dir); $this->dir = basename($dir);
} }
public function getDir() public function getDir() {
{
return $this->dir; return $this->dir;
} }
public function isWritable() public function isWritable() {
{
return is_dir($this->dir) && is_writable($this->dir); return is_dir($this->dir) && is_writable($this->dir);
} }
public function exists($filename) public function exists($filename) {
{
return file_exists($this->getFullPath($filename)); return file_exists($this->getFullPath($filename));
} }
public function getSize($filename) public function getSize($filename) {
{
if ($this->exists($filename)) if ($this->exists($filename))
return filesize($this->getFullPath($filename)); return filesize($this->getFullPath($filename));
else else
return -1; return -1;
} }
public function getFullPath($filename) public function getFullPath($filename) {
{
$filename = basename($filename); $filename = basename($filename);
return CACHE_DIR . "/" . $this->dir . "/" . $filename; return CACHE_DIR . "/" . $this->dir . "/" . $filename;
} }
public function put($filename, $data) public function put($filename, $data) {
{
return file_put_contents($this->getFullPath($filename), $data); return file_put_contents($this->getFullPath($filename), $data);
} }
public function touch($filename) public function touch($filename) {
{
return touch($this->getFullPath($filename)); return touch($this->getFullPath($filename));
} }
public function get($filename) public function get($filename) {
{
if ($this->exists($filename)) if ($this->exists($filename))
return file_get_contents($this->getFullPath($filename)); return file_get_contents($this->getFullPath($filename));
else else
return null; return null;
} }
public function getMimeType($filename) public function getMimeType($filename) {
{
if ($this->exists($filename)) if ($this->exists($filename))
return mime_content_type($this->getFullPath($filename)); return mime_content_type($this->getFullPath($filename));
else else
return null; return null;
} }
public function send($filename) public function send($filename) {
{
return send_local_file($this->getFullPath($filename)); return send_local_file($this->getFullPath($filename));
} }
static public function getUrl($filename) static public function getUrl($filename) {
{
return get_self_url_prefix() . "/public.php?op=cached_url&file=" . $filename; return get_self_url_prefix() . "/public.php?op=cached_url&file=" . $filename;
} }
@ -120,4 +107,28 @@ class DiskCache
} }
return $res; return $res;
} }
static function expire() {
$dirs = array_filter(glob(CACHE_DIR . "/*"), "is_dir");
foreach ($dirs as $cache_dir) {
$num_deleted = 0;
if (is_writable($cache_dir)) {
$files = glob("$cache_dir/*");
if ($files) {
foreach ($files as $file) {
if (time() - filemtime($file) > 86400*CACHE_MAX_DAYS) {
unlink($file);
++$num_deleted;
}
}
}
}
Debug::log("Expired $cache_dir: removed $num_deleted files.");
}
}
} }

View File

@ -1284,32 +1284,6 @@ class RSSUtils {
Debug::log("Removed $num_deleted old lock files."); Debug::log("Removed $num_deleted old lock files.");
} }
static function expire_cached_files() {
foreach (array("feeds", "images", "export", "upload") as $dir) {
$cache_dir = CACHE_DIR . "/$dir";
Debug::log("Expiring $cache_dir", Debug::$LOG_VERBOSE);
$num_deleted = 0;
if (is_writable($cache_dir)) {
$files = glob("$cache_dir/*");
if ($files) {
foreach ($files as $file) {
if (time() - filemtime($file) > 86400*CACHE_MAX_DAYS) {
unlink($file);
++$num_deleted;
}
}
}
}
Debug::log("$cache_dir: removed $num_deleted files.");
}
}
/** /**
* Source: http://www.php.net/manual/en/function.parse-url.php#104527 * Source: http://www.php.net/manual/en/function.parse-url.php#104527
* Returns the url query as associative array * Returns the url query as associative array
@ -1498,7 +1472,8 @@ class RSSUtils {
} }
static function housekeeping_common() { static function housekeeping_common() {
RSSUtils::expire_cached_files(); DiskCache::expire();
RSSUtils::expire_lock_files(); RSSUtils::expire_lock_files();
RSSUtils::expire_error_log(); RSSUtils::expire_error_log();
RSSUtils::expire_feed_archive(); RSSUtils::expire_feed_archive();