Skip to content

Commit

Permalink
Add setHashName method
Browse files Browse the repository at this point in the history
  • Loading branch information
carry0987 committed Oct 24, 2023
1 parent 0cc322b commit e460c5f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/DBController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class DBController
{
private $db = null;
private static $table = 'template';

public function __construct(mixed $dbSettings)
{
Expand All @@ -25,9 +26,16 @@ public function isConnected()
return $this->db !== null;
}

public function setTableName(string $table)
{
self::$table = $table;
return $this;
}

public function getVersion(string $tpl_path, string $tpl_name, string $tpl_type)
{
$tpl_query = 'SELECT tpl_md5, tpl_expire_time, tpl_verhash FROM template WHERE tpl_path = :path AND tpl_name = :name AND tpl_type = :type';
$tpl_query = 'SELECT tpl_md5, tpl_expire_time, tpl_verhash FROM '.self::$table.'
WHERE tpl_path = :path AND tpl_name = :name AND tpl_type = :type';
try {
$tpl_stmt = $this->db->prepare($tpl_query);
$tpl_stmt->execute([':path' => $tpl_path, ':name' => $tpl_name, ':type' => $tpl_type]);
Expand All @@ -44,7 +52,8 @@ public function getVersion(string $tpl_path, string $tpl_name, string $tpl_type)

public function createVersion($tpl_path, $tpl_name, $tpl_type, $tpl_md5, $tpl_expire_time, $tpl_verhash)
{
$tpl_query = 'INSERT INTO template (tpl_path, tpl_name, tpl_type, tpl_md5, tpl_expire_time, tpl_verhash) VALUES (:path, :name, :type, :md5, :expire_time, :verhash)';
$tpl_query = 'INSERT INTO '.self::$table.' (tpl_path, tpl_name, tpl_type, tpl_md5, tpl_expire_time, tpl_verhash)
VALUES (:path, :name, :type, :md5, :expire_time, :verhash)';
try {
$tpl_stmt = $this->db->prepare($tpl_query);
$tpl_stmt->execute([
Expand All @@ -63,7 +72,9 @@ public function createVersion($tpl_path, $tpl_name, $tpl_type, $tpl_md5, $tpl_ex

public function updateVersion($tpl_path, $tpl_name, $tpl_type, $tpl_md5, $tpl_expire_time, $tpl_verhash)
{
$tpl_query = 'UPDATE template SET tpl_md5 = :md5, tpl_expire_time = :expire_time, tpl_verhash = :verhash WHERE tpl_path = :path AND tpl_name = :name AND tpl_type = :type';
$tpl_query = 'UPDATE '.self::$table.'
SET tpl_md5 = :md5, tpl_expire_time = :expire_time, tpl_verhash = :verhash
WHERE tpl_path = :path AND tpl_name = :name AND tpl_type = :type';
try {
$tpl_stmt = $this->db->prepare($tpl_query);
$tpl_stmt->execute([
Expand Down
11 changes: 9 additions & 2 deletions src/RedisController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class RedisController
{
private $redis = null;
private $hash = 'tpl';

public function __construct(mixed $redisConfig)
{
Expand All @@ -23,11 +24,17 @@ public function isConnected()
return $this->redis !== null;
}

public function setHashName(string $hash)
{
$this->hash = $hash;
return $this;
}

public function getVersion($get_tpl_path, $get_tpl_name, $get_tpl_type)
{
if ($this->redis === null) return false;
$tpl_key = "template::$get_tpl_path::$get_tpl_name::$get_tpl_type";
$result = $this->redis->getHashValue('tpl', $tpl_key);
$result = $this->redis->getHashValue($this->hash, $tpl_key);
if (!empty($result)) {
return unserialize($result);
}
Expand All @@ -43,7 +50,7 @@ public function createVersion($tpl_path, $tpl_name, $tpl_type, $tpl_md5, $tpl_ex
"tpl_expire_time" => $tpl_expire_time,
"tpl_verhash" => $tpl_verhash,
];
return $this->redis->setHashValue('tpl', $tpl_key, serialize($tpl_data));
return $this->redis->setHashValue($this->hash, $tpl_key, serialize($tpl_data));
}

public function updateVersion($tpl_path, $tpl_name, $tpl_type, $tpl_md5, $tpl_expire_time, $tpl_verhash)
Expand Down

0 comments on commit e460c5f

Please sign in to comment.