mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 00:55:28 +00:00
implement cache features
This commit is contained in:
parent
e06e247580
commit
089bf67a5b
1 changed files with 124 additions and 0 deletions
|
|
@ -41,6 +41,16 @@ class Database
|
|||
);
|
||||
|
||||
// Init tables
|
||||
$this->_database->query('
|
||||
CREATE TABLE IF NOT EXISTS `cache`
|
||||
(
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`time` INTEGER NOT NULL,
|
||||
`request` VARCHAR(1024),
|
||||
`data` BLOB
|
||||
);
|
||||
');
|
||||
|
||||
$this->_database->query('
|
||||
CREATE TABLE IF NOT EXISTS `history`
|
||||
(
|
||||
|
|
@ -70,6 +80,120 @@ class Database
|
|||
}
|
||||
}
|
||||
|
||||
// Cache
|
||||
public function addCache(
|
||||
?string $request = null,
|
||||
?string $data = null,
|
||||
?int $time = null
|
||||
): int
|
||||
{
|
||||
$query = $this->_database->prepare(
|
||||
'INSERT INTO `cache` (`time`, `request`, `data`) VALUES (:time, :request, :data)'
|
||||
);
|
||||
|
||||
$query->execute(
|
||||
[
|
||||
':time' => $time ? $time : time(),
|
||||
':request' => $request,
|
||||
':data' => $data
|
||||
]
|
||||
);
|
||||
|
||||
return intval(
|
||||
$this->_database->lastInsertId()
|
||||
);
|
||||
}
|
||||
|
||||
public function findCache(
|
||||
string $request = '',
|
||||
int $start = 0,
|
||||
int $limit = 1000
|
||||
): array
|
||||
{
|
||||
$query = $this->_database->prepare(
|
||||
sprintf(
|
||||
'SELECT * FROM `cache`
|
||||
WHERE `request` LIKE :request
|
||||
ORDER BY `id` DESC
|
||||
LIMIT %d,%d',
|
||||
$start,
|
||||
$limit
|
||||
)
|
||||
);
|
||||
|
||||
$query->execute(
|
||||
[
|
||||
':request' => sprintf(
|
||||
'%%%s%%',
|
||||
$request
|
||||
)
|
||||
]
|
||||
);
|
||||
|
||||
return $query->fetchAll();
|
||||
}
|
||||
|
||||
|
||||
public function deleteCache(
|
||||
int $id
|
||||
): int
|
||||
{
|
||||
$query = $this->_database->query(
|
||||
sprintf(
|
||||
'DELETE FROM `cache` WHERE `id` = %d',
|
||||
$id
|
||||
)
|
||||
);
|
||||
|
||||
return $query->rowCount();
|
||||
}
|
||||
|
||||
public function cleanCache(
|
||||
int $timeout = 0
|
||||
): int
|
||||
{
|
||||
$query = $this->_database->query(
|
||||
sprintf(
|
||||
'DELETE FROM `cache` WHERE `time` + %d < %d',
|
||||
$timeout,
|
||||
time()
|
||||
)
|
||||
);
|
||||
|
||||
return $query->rowCount();
|
||||
}
|
||||
|
||||
public function renewCache(
|
||||
string $request,
|
||||
?string $title = null
|
||||
): void
|
||||
{
|
||||
// Find same records match URL
|
||||
$query = $this->_database->prepare(
|
||||
'SELECT * FROM `cache` WHERE `request` LIKE :request'
|
||||
);
|
||||
|
||||
$query->execute(
|
||||
[
|
||||
':request' => $request
|
||||
]
|
||||
);
|
||||
|
||||
// Drop previous records
|
||||
foreach ($query->fetchAll() as $record)
|
||||
{
|
||||
$this->deleteCache(
|
||||
$record->id
|
||||
);
|
||||
}
|
||||
|
||||
// Add new record
|
||||
$this->addCache(
|
||||
$request,
|
||||
$data
|
||||
);
|
||||
}
|
||||
|
||||
// History
|
||||
public function addHistory(
|
||||
string $url,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue