improve paid torrent + hit and run

This commit is contained in:
xiaomlove
2025-06-17 20:54:18 +07:00
parent ee4757cdc9
commit 3db4537153
21 changed files with 326 additions and 70 deletions
+4 -6
View File
@@ -252,11 +252,11 @@ class BonusRepository extends BaseRepository
}
public function consumeToBuyTorrent($uid, $torrentId, $channel = 'Web'): bool
public function consumeToBuyTorrent($uid, $torrentId, $channel = 'Web'): TorrentBuyLog
{
$torrent = Torrent::query()->findOrFail($torrentId, Torrent::$commentFields);
$requireBonus = $torrent->price;
NexusDB::transaction(function () use ($requireBonus, $torrent, $channel, $uid) {
return NexusDB::transaction(function () use ($requireBonus, $torrent, $channel, $uid) {
$userQuery = User::query();
if ($requireBonus > 0) {
$userQuery = $userQuery->lockForUpdate();
@@ -269,7 +269,7 @@ class BonusRepository extends BaseRepository
], $buyerLocale);
do_log("comment: $comment");
$this->consumeUserBonus($user, $requireBonus, BonusLogs::BUSINESS_TYPE_BUY_TORRENT, $comment);
TorrentBuyLog::query()->create([
$buyLog = TorrentBuyLog::query()->create([
'uid' => $user->id,
'torrent_id' => $torrent->id,
'price' => $requireBonus,
@@ -314,10 +314,8 @@ class BonusRepository extends BaseRepository
], $buyerLocale),
];
Message::add($buyTorrentSuccessMessage);
return $buyLog;
});
return true;
}
public function consumeUserBonus($user, $requireBonus, $logBusinessType, $logComment = '', array $userUpdates = [])
+3
View File
@@ -257,6 +257,9 @@ LUA;
"lastcleantime5" => "five",
];
$avps = Avp::query()->get()->keyBy("arg");
if ($avps->isEmpty()) {
return;
}
foreach ($arvToLevel as $arg => $level) {
/** @var NexusModel $value */
$value = $avps->get($arg);
+41 -8
View File
@@ -3,6 +3,7 @@
namespace App\Repositories;
use App\Auth\Permission;
use App\Enums\ModelEventEnum;
use App\Exceptions\InsufficientPermissionException;
use App\Exceptions\NexusException;
use App\Http\Resources\TorrentResource;
@@ -843,20 +844,51 @@ HTML;
}
/**
* 购买成功缓存,保存为 hash,一个种子一个 hash,永久有效
* 购买成功缓存 30 天并更新到 snatched 上
* @param $uid
* @param $torrentId
* @return void
* @throws \RedisException
*/
public function addBuySuccessCache($uid, $torrentId): void
public function addBuySuccessCache($uid, $torrentId, $buyLogId): void
{
NexusDB::redis()->hSet($this->getBoughtUserCacheKey($torrentId), $uid, 1);
NexusDB::redis()->set($this->getBoughtUserCacheKey($torrentId, $uid), 1, ['NX', 'EX' => 86400*30]);
$record = Snatch::query()
->where("torrentid", $torrentId)
->where("userid", $uid)
->first();
if ($record) {
$record->buy_log_id = $buyLogId;
$record->save();
publish_model_event(ModelEventEnum::SNATCHED_UPDATED, $record->id);
} else {
do_log("addBuySuccessCache, uid: $uid, torrentId: $torrentId, buyLogId: $buyLogId, snatched not exists", 'error');
}
}
public function hasBuySuccessCache($uid, $torrentId): bool
{
return NexusDB::redis()->hGet($this->getBoughtUserCacheKey($torrentId), $uid) == 1;
$key = $this->getBoughtUserCacheKey($torrentId, $uid);
if (NexusDB::redis()->exists($key)) {
return true;
}
return false;
}
public function hasBuySuccess($uid, $torrentId): bool
{
if ($this->hasBuySuccessCache($uid, $torrentId)) {
return true;
}
$buyLog = TorrentBuyLog::query()
->where("torrent_id", $torrentId)
->where("uid", $uid)
->first();
if ($buyLog) {
$this->addBuySuccessCache($uid, $torrentId, $buyLog->id);
}
return $buyLog != null;
}
/**
@@ -868,8 +900,8 @@ HTML;
*/
public function getBuyStatus($uid, $torrentId): int
{
//查询是否已经购买
if ($this->hasBuySuccessCache($uid, $torrentId)) {
//从缓存中判断是否购买
if ($this->hasBuySuccess($uid, $torrentId)) {
return self::BUY_STATUS_SUCCESS;
}
//是否购买失败过
@@ -913,12 +945,13 @@ HTML;
/**
* 购买成功缓存 key
* @update 改为使用字符串判断键是否存在即可
* @param $torrentId
* @return string
*/
public function getBoughtUserCacheKey($torrentId): string
public function getBoughtUserCacheKey($torrentId, $userId): string
{
return sprintf("%s:%s", self::BOUGHT_USER_CACHE_KEY_PREFIX, $torrentId);
return sprintf("%s:%s:%s", self::BOUGHT_USER_CACHE_KEY_PREFIX, $torrentId, $userId);
}
/**