Tracker URl

This commit is contained in:
xiaomlove
2025-06-20 22:59:02 +07:00
parent a0ff441f03
commit 73b4183d56
22 changed files with 333 additions and 16 deletions

View File

@@ -87,7 +87,7 @@ class PluginStore extends Model
{
$log = "listAll, withoutCache: $withoutCache";
$cacheKey = "nexus_plugin_store_all";
$cacheTime = 86400*100;
$cacheTime = 86400;
if (is_null(self::$rows)) {
$log .= ", is_null";
if ($withoutCache) {

67
app/Models/TrackerUrl.php Normal file
View File

@@ -0,0 +1,67 @@
<?php
namespace App\Models;
use Nexus\Database\NexusDB;
class TrackerUrl extends NexusModel
{
protected $fillable = ['url', 'enabled', 'is_default', 'priority'];
public $timestamps = true;
const TRACKER_URL_CACHE_KEY = "TRACKER_URL";
const TRACKER_URL_DEFAULT_CACHE_KEY = "TRACKER_URL_DEFAULT";
protected static function booted(): void
{
static::saved(function (TrackerUrl $model) {
if ($model->is_default == 1) {
self::query()->where("id", "!=", $model->id)->update(["is_default" => 0]);
}
//添加 id 与 URL 映射
$redis = NexusDB::redis();
$redis->del(self::TRACKER_URL_CACHE_KEY);
$list = self::listAll();
$first = $list->first();
$hasDefault = false;
foreach ($list as $item) {
$redis->hset(self::TRACKER_URL_CACHE_KEY, $item->id, $item->url);
if ($item->is_default == 1) {
$hasDefault = true;
$redis->set(self::TRACKER_URL_DEFAULT_CACHE_KEY, $item->url);
}
}
if (!$hasDefault && $first) {
$redis->set(self::TRACKER_URL_DEFAULT_CACHE_KEY, $first->url);
}
});
static::saving(function (TrackerUrl $model) {
if ($model->is_default == 1) {
$model->enabled = 1;
}
});
}
public static function listAll()
{
return self::query()
->where("enabled", 1)
->orderBy("is_default", "desc")
->orderBy("priority", "desc")
->get();
}
public static function getById(int $trackerUrlId)
{
$redis = NexusDB::redis();
if ($trackerUrlId == 0) {
return $redis->get(self::TRACKER_URL_DEFAULT_CACHE_KEY);
}
$result = $redis->hget(self::TRACKER_URL_CACHE_KEY, $trackerUrlId);
if (!$result) {
$result = $redis->get(self::TRACKER_URL_DEFAULT_CACHE_KEY);
}
return $result;
}
}