'int', 'user_id' => 'int', 'app_id' => 'string', 'template_hash' => 'string', 'cos_url' => 'string', 'file_size' => 'int', 'mime_type' => 'string', 'status' => 'int', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'expires_at' => 'datetime', 'platform' => 'string', ]; /** * 查找用户的有效海报缓存 * * @param int $userId 用户ID * @param string $templateHash 模板哈希 * @param string $appId 小程序APPID * @param string $platform 平台类型,默认为小程序 * @return array|null 缓存记录 */ public static function findValidCache($userId, $templateHash, $appId, $platform = 'MP-WEIXIN') { return self::where([ 'user_id' => $userId, 'template_hash' => $templateHash, 'app_id' => $appId, 'platform' => $platform, 'status' => 1 ]) ->where('expires_at', '>', date('Y-m-d H:i:s')) ->find(); } /** * 失效指定用户的所有其他模板海报 * * @param int $userId 用户ID * @param string $currentTemplateHash 当前模板哈希 * @param string $platform 平台类型,默认为小程序 * @return bool 操作结果 */ public static function invalidateOldCaches($userId, $currentTemplateHash, $platform = 'MP-WEIXIN') { return self::where('user_id', $userId) ->where('platform', $platform) ->where('template_hash', '<>', $currentTemplateHash) ->update(['status' => 0]); } /** * 清理过期的海报缓存记录 * * @param int $limit 最大处理记录数 * @return int 清理的记录数 */ public static function cleanExpiredCaches($limit = 100) { // 查询过期的缓存记录 $expiredCaches = self::where('expires_at', '<', date('Y-m-d H:i:s')) ->where('status', 1) ->limit($limit) ->select(); $cleanCount = 0; foreach ($expiredCaches as $cache) { $cache->status = 0; $cache->save(); $cleanCount++; } return $cleanCount; } }