Skip to content

Commit

Permalink
Update the cache, so the cache will be cleared async to speed up process
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Neto committed Jun 6, 2024
1 parent aede8d0 commit ba0d0f7
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 33 deletions.
67 changes: 49 additions & 18 deletions objects/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,12 +444,39 @@ public function delete()
return false;
}


public static function truncateTable()
{
global $global;

if (!self::ignoreTableSecurityCheck() && isUntrustedRequest("TRUNCATE " . static::getTableName())) {
return false;
}

// Attempt to truncate the table
$sql = "TRUNCATE TABLE " . static::getTableName();
$global['lastQuery'] = $sql;

$result = sqlDAL::writeSql($sql);
if ($result === false) {
// If truncation fails, delete all records
$sql = "DELETE FROM " . static::getTableName() . " WHERE id > 0";
$global['lastQuery'] = $sql;
return sqlDAL::writeSql($sql);
}

return $result;
}


static function ignoreTableSecurityCheck()
{

$ignoreArray = [
'vast_campaigns_logs',
'videos', 'CachesInDB',
'videos',
'CachesInDB',
'cache_schedule_delete',
'plugins',
'users_login_history',
'live_transmitions_history',
Expand Down Expand Up @@ -1131,31 +1158,35 @@ public function getCache($suffix, $lifetime = 60)
return $cache;
}

public function deleteCache($clearFirstPageCache = false)
public function deleteCache($clearFirstPageCache = false, $schedule=true)
{
$timeLog = __FILE__ . "::deleteCache ";
TimeLogStart($timeLog);
$prefix = $this->getCacheSubdir();
if (class_exists('CachesInDB')) {
_error_log("deleteCache CachesInDB");
CacheDB::deleteCacheStartingWith($prefix);
CacheDB::deleteCacheStartingWith($prefix, $schedule);
}
TimeLogEnd($timeLog, __LINE__);
_session_start();
TimeLogEnd($timeLog, __LINE__);
unset($_SESSION['user']['sessionCache']);
TimeLogEnd($timeLog, __LINE__);
if ($clearFirstPageCache) {
_error_log("deleteCache clearFirstPageCache");
clearCache(true);
if(!$schedule){
TimeLogEnd($timeLog, __LINE__);
_session_start();
TimeLogEnd($timeLog, __LINE__);
unset($_SESSION['user']['sessionCache']);
TimeLogEnd($timeLog, __LINE__);
if ($clearFirstPageCache) {
_error_log("deleteCache clearFirstPageCache");
clearCache(true);
}
TimeLogEnd($timeLog, __LINE__);
$dir = ObjectYPT::getTmpCacheDir() . $prefix;

$resp = exec("rm -R {$dir}");
TimeLogEnd($timeLog, __LINE__);

return $resp;
}else{
return false;
}
TimeLogEnd($timeLog, __LINE__);
$dir = ObjectYPT::getTmpCacheDir() . $prefix;

$resp = exec("rm -R {$dir}");
TimeLogEnd($timeLog, __LINE__);

return $resp;
}

public function setSuffix($suffix)
Expand Down
4 changes: 2 additions & 2 deletions objects/functionsBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@ function get_browser_name($user_agent = "")
strpos($t, 'bot') || strpos($t, 'archive') ||
strpos($t, 'info') || strpos($t, 'data')
) {
return '[Bot] Other';
return '[Bot] Other '.$user_agent;
}
//_error_log("Unknow user agent ($t) IP=" . getRealIpAddr() . " URI=" . getRequestURI());
return 'Other (Unknown)';
return 'Other (Unknown) '.$user_agent;
}

/**
Expand Down
19 changes: 16 additions & 3 deletions objects/video.php
Original file line number Diff line number Diff line change
Expand Up @@ -5549,7 +5549,20 @@ public static function deleteGifAndWebp($filename)
return true;
}

public static function clearCache($videos_id, $deleteThumbs = false, $clearFirstPageCache = false)
public static function clearCache($videos_id, $deleteThumbs = false, $clearFirstPageCache = false, $async = true)
{
global $global;
if($async){
$videos_id = intval($videos_id);
$deleteThumbs = !empty($deleteThumbs)?'true':'false';
$clearFirstPageCache = !empty($clearFirstPageCache)?'true':'false';
execAsync("php {$global['systemRootPath']}plugin/Cache/deleteVideo.json.php $videos_id $deleteThumbs $clearFirstPageCache");
}else{
return self::_clearCache($videos_id, $deleteThumbs, $clearFirstPageCache, false);
}
}

public static function _clearCache($videos_id, $deleteThumbs = false, $clearFirstPageCache = false, $schedule=true)
{
//_error_log("Video:clearCache($videos_id)");
$video = new Video("", "", $videos_id);
Expand All @@ -5563,10 +5576,10 @@ public static function clearCache($videos_id, $deleteThumbs = false, $clearFirst
}

$videoCache = new VideoCacheHandler($filename);
$videoCache->deleteCache($clearFirstPageCache);
$videoCache->deleteCache($clearFirstPageCache, $schedule);

$videosListCache = new VideosListCacheHandler();
$videosListCache->deleteCache();
$videosListCache->deleteCache(false, $schedule);

ObjectYPT::setLastDeleteALLCacheTime();
return true;
Expand Down
19 changes: 17 additions & 2 deletions plugin/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_once $global['systemRootPath'] . 'plugin/Plugin.abstract.php';
require_once $global['systemRootPath'] . 'plugin/Cache/Objects/CacheDB.php';
require_once $global['systemRootPath'] . 'plugin/Cache/Objects/Cache_schedule_delete.php';

class Cache extends PluginAbstract {

Expand Down Expand Up @@ -29,7 +30,7 @@ public function getUUID() {
}

public function getPluginVersion() {
return "7.0";
return "8.0";
}

public function getEmptyDataObject() {
Expand Down Expand Up @@ -419,7 +420,13 @@ public static function deleteAllCache() {

public static function deleteFirstPageCache() {
clearCache(true);
return CacheDB::deleteCacheStartingWith('firstPage');
return CacheDB::deleteCacheStartingWith('firstPage', false);
}

public static function deleteCacheDir($prefix){;
$dir = ObjectYPT::getTmpCacheDir() . $prefix;
$resp = exec("rm -R {$dir}");
return $resp;
}

public static function deleteOldCache($days, $limit = 5000) {
Expand All @@ -438,10 +445,18 @@ public static function deleteOldCache($days, $limit = 5000) {
}
return false;
}

function executeEveryMinute() {
global $global;
$global['systemRootPath'] . 'plugin/Cache/deleteStatistics.json.php';
self::deleteOldCache(1);

$rows = Cache_schedule_delete::getAll();
Cache_schedule_delete::truncateTable();
foreach ($rows as $row) {
CacheDB::deleteCacheStartingWith($row['name'], false);
self::deleteCacheDir($row['name']);
}
}

}
Expand Down
22 changes: 14 additions & 8 deletions plugin/Cache/Objects/CacheDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ public static function encodeContent($content)
return CachesInDB::encodeContent($content);
}

public static function deleteCacheStartingWith($name)
public static function deleteCacheStartingWith($name, $schedule=true)
{
try {
if(self::$cacheType == self::$CACHE_ON_MEMORY){
return CachesInDBMem::_deleteCacheStartingWith($name);
}else{
return CachesInDB::_deleteCacheStartingWith($name);
if($schedule){
$newSchedule = new Cache_schedule_delete(0);
$newSchedule->setName($name);
return $newSchedule->save();
}else{
try {
if(self::$cacheType == self::$CACHE_ON_MEMORY){
return CachesInDBMem::_deleteCacheStartingWith($name);
}else{
return CachesInDB::_deleteCacheStartingWith($name);
}
} catch (\Throwable $th) {
_error_log("CacheDB::deleteCacheStartingWith($name)");
}
} catch (\Throwable $th) {
_error_log("CacheDB::deleteCacheStartingWith($name)");
}
}

Expand Down
35 changes: 35 additions & 0 deletions plugin/Cache/Objects/Cache_schedule_delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

require_once dirname(__FILE__) . '/../../../videos/configuration.php';

class Cache_schedule_delete extends ObjectYPT {

protected $id,$name;

static function getSearchFieldsNames() {
return array('name');
}

static function getTableName() {
return 'cache_schedule_delete';
}

function setId($id) {
$this->id = intval($id);
}

function setName($name) {
$this->name = $name;
}


function getId() {
return intval($this->id);
}

function getName() {
return $this->name;
}


}
29 changes: 29 additions & 0 deletions plugin/Cache/deleteVideo.json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
header('Content-Type: application/json');
require_once '../../videos/configuration.php';

$obj = new stdClass();
$obj->error = true;
$obj->msg = "";
$obj->result = "";

if (!isCommandLineInterface()) {
forbiddenPage('Command line only');
}

// Get command line arguments
if ($argc < 4) {
$obj->msg = "Insufficient arguments. Usage: php script.php <videos_id> <deleteThumbs> <clearFirstPageCache>";
die(json_encode($obj));
}

$videos_id = intval($argv[1]);
$deleteThumbs = filter_var($argv[2], FILTER_VALIDATE_BOOLEAN);
$clearFirstPageCache = filter_var($argv[3], FILTER_VALIDATE_BOOLEAN);

// Call the clearCache function with the provided arguments
$obj->clearCache = Video::_clearCache($videos_id, $deleteThumbs, $clearFirstPageCache, false);
$obj->error = false;
$obj->msg = "Cache cleared successfully";

echo json_encode($obj);
7 changes: 7 additions & 0 deletions plugin/Cache/install/install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ ALTER TABLE CachesInDB ADD FULLTEXT(name);

ALTER TABLE CachesInDB
ADD UNIQUE `unique_cache_index`(`name`(250), `domain`(50), `ishttps`, `user_location`(50), `loggedType`);

CREATE TABLE IF NOT EXISTS `cache_schedule_delete` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(500) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `name_UNIQUE` (`name` ASC))
ENGINE = InnoDB;
6 changes: 6 additions & 0 deletions plugin/Cache/install/updateV8.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS `cache_schedule_delete` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(500) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `name_UNIQUE` (`name` ASC))
ENGINE = InnoDB;

0 comments on commit ba0d0f7

Please sign in to comment.