Skip to content

Commit

Permalink
Reduce the total of cache files
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielnetoDotCom committed Oct 28, 2021
1 parent f948b06 commit bc5b621
Show file tree
Hide file tree
Showing 14 changed files with 2,338 additions and 306 deletions.
76 changes: 62 additions & 14 deletions objects/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static function getNowFromDB() {
return $row;
}

public static function setTimeZone() {
public static function setGlobalTimeZone() {
global $advancedCustom, $timezoneOriginal;
if (!isset($timezoneOriginal)) {
$timezoneOriginal = date_default_timezone_get();
Expand Down Expand Up @@ -251,6 +251,11 @@ public function save() {
// do nothing
} elseif (strtolower($value) == 'modified') {
$fields[] = " {$value} = now() ";
} elseif (strtolower($value) == 'timezone') {
if(empty($this->$value)){
$this->$value = date_default_timezone_get();
}
$fields[] = " `{$value}` = '{$this->$value}' ";
} elseif (is_numeric($this->$value)) {
$fields[] = " `{$value}` = {$this->$value} ";
} elseif (strtolower($this->$value) == 'null') {
Expand All @@ -268,6 +273,11 @@ public function save() {
foreach ($fieldsName as $value) {
if (strtolower($value) == 'created' || strtolower($value) == 'modified') {
$fields[] = " now() ";
} elseif (strtolower($value) == 'timezone') {
if(empty($this->$value)){
$this->$value = date_default_timezone_get();
}
$fields[] = " '{$this->$value}' ";
} elseif (!isset($this->$value) || strtolower($this->$value) == 'null') {
$fields[] = " NULL ";
} else if (is_string($this->$value) || is_numeric($this->$value)) {
Expand Down Expand Up @@ -320,18 +330,43 @@ public function delete() {
//_error_log("Delete Query: ".$sql);
return sqlDAL::writeSql($sql, "i", array($this->id));
}
_error_log("Id for table " . static::getTableName() . " not defined for deletion", AVideoLog::$ERROR);
_error_log("Id for table " . static::getTableName() . " not defined for deletion ". json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)), AVideoLog::$ERROR);
return false;
}

public static function setCache($name, $value) {
$cachefile = self::getCacheFileName($name);
make_path($cachefile);

static function shouldUseDatabase($content){
global $advancedCustom;
if(empty($advancedCustom)){
$advancedCustom = AVideoPlugin::getObjectData("CustomizeAdvanced");
}
if(empty($advancedCustom->doNotSaveCacheOnFilesystem) && class_exists('Cache')){
$content = _json_encode($content);
$len = strlen($content);
if(!empty($len) && $len<60000){
return true;
}else if(!empty($len)){
//_error_log('Object::setCache '.$len);
}
}
return false;
}

public static function setCache($name, $value) {
if(self::shouldUseDatabase($value)){
return Cache::_setCache($name, $value);
}

$content = _json_encode($value);
if (empty($content)) {
$content = $value;
}

if(empty($content)){
return false;
}

$cachefile = self::getCacheFileName($name);
//make_path($cachefile);

$bytes = @file_put_contents($cachefile, $content);
self::setSessionCache($name, $value);
Expand Down Expand Up @@ -366,14 +401,21 @@ public static function getCache($name, $lifetime = 60, $ignoreSessionCache = fal
}
global $getCachesProcessed, $_getCache;

if(self::shouldUseDatabase('')){
$cache = Cache::getCache($name, $lifetime);
if(!empty($cache)){
return $cache;
}
}

if (empty($_getCache)) {
$_getCache = array();
}

if (empty($getCachesProcessed)) {
$getCachesProcessed = array();
}
$cachefile = self::getCacheFileName($name);
$cachefile = self::getCacheFileName($name, false);
//var_dump($cachefile);//exit;
self::setLastUsedCacheFile($cachefile);
//_error_log('getCache: cachefile '.$cachefile);
Expand Down Expand Up @@ -433,7 +475,7 @@ public static function getCache($name, $lifetime = 60, $ignoreSessionCache = fal
//_error_log("YPTObject::getCache log error [{$name}] $cachefile filemtime = ".filemtime($cachefile));
return null;
}

private static function setLastUsedCacheMode($mode) {
global $_lastCacheMode;
$_lastCacheMode = $mode;
Expand All @@ -453,10 +495,13 @@ public static function deleteCache($name) {
if (empty($name)) {
return false;
}

Cache::deleteCache($name);

global $__getAVideoCache;
unset($__getAVideoCache);
//_error_log('deleteCache: '.json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)));
$cachefile = self::getCacheFileName($name);
$cachefile = self::getCacheFileName($name, false);
@unlink($cachefile);
self::deleteSessionCache($name);
ObjectYPT::deleteCacheFromPattern($name);
Expand All @@ -483,6 +528,7 @@ static function deleteCachePattern($pattern) {
}

public static function deleteALLCache() {
Cache::deleteAllCache();
self::deleteAllSessionCache();
$lockFile = getVideosDir() . '.deleteALLCache.lock';
if (file_exists($lockFile) && filectime($lockFile) > strtotime('-5 minutes')) {
Expand All @@ -494,7 +540,7 @@ public static function deleteALLCache() {
global $__getAVideoCache;
unset($__getAVideoCache);
//_error_log('deleteALLCache: '.json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)));
$tmpDir = self::getCacheDir();
$tmpDir = self::getCacheDir('', false);

$newtmpDir = rtrim($tmpDir, DIRECTORY_SEPARATOR) . uniqid();
_error_log("deleteALLCache rename($tmpDir, $newtmpDir) ");
Expand All @@ -514,7 +560,7 @@ public static function deleteALLCache() {
return true;
}

public static function getCacheDir($filename = '') {
public static function getCacheDir($filename = '', $createDir=true) {
global $_getCacheDir, $global;

if (!isset($_getCacheDir)) {
Expand Down Expand Up @@ -557,7 +603,9 @@ public static function getCacheDir($filename = '') {
}
}
$tmpDir = fixPath($tmpDir);
make_path($tmpDir);
if($createDir){
make_path($tmpDir);
}
if (!file_exists($tmpDir . "index.html") && is_writable($tmpDir)) {// to avoid search into the directory
_file_put_contents($tmpDir . "index.html", time());
}
Expand All @@ -566,9 +614,9 @@ public static function getCacheDir($filename = '') {
return $tmpDir;
}

public static function getCacheFileName($name) {
public static function getCacheFileName($name, $createDir=true) {
global $global;
$tmpDir = self::getCacheDir($name);
$tmpDir = self::getCacheDir($name, $createDir);
$uniqueHash = md5($name . $global['salt']); // add salt for security reasons
return $tmpDir . $uniqueHash . '.cache';
}
Expand Down
8 changes: 8 additions & 0 deletions objects/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,7 @@ function make_path($path) {
$path = pathinfo($path, PATHINFO_DIRNAME);
}
if (!is_dir($path)) {
//if(preg_match('/getvideoinfo/i', $path)){var_dump(debug_backtrace());}
$created = mkdir($path, 0755, true);
/*
if (!$created) {
Expand Down Expand Up @@ -6659,6 +6660,7 @@ function deleteStatsNotifications() {

function getStatsNotifications($force_recreate = false) {
$cacheName = "getStats" . DIRECTORY_SEPARATOR . "getStatsNotifications";
unset($_POST['sort']);
if ($force_recreate) {
Live::deleteStatsCache();
} else {
Expand Down Expand Up @@ -7452,6 +7454,12 @@ function seconds2human($ss) {
return implode(', ', $times);
}

/**
* convert a time in a timezone into my time
* @param type $time
* @param type $timezone
* @return type
*/
function getTimeInTimezone($time, $timezone) {
if (!is_numeric($time)) {
$time = strtotime($time);
Expand Down
Loading

0 comments on commit bc5b621

Please sign in to comment.