using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Filters; using System; using System.Collections.Generic; using System.Linq; using System.Text; using dodohold.core; using Dataoke; namespace molilian.core { public class ApiTokenDTO { public int id { get; set; } public string name { get; set; } = string.Empty; public string token { get; set; } = string.Empty; public string description { get; set; } = string.Empty; public DateTime create_time { get; set; } public DateTime last_time { get; set; } public bool status { get; set; } public int interval { get; set; } } public partial class ApiTokenCore { private static readonly object _lockObj = new(); private static IEnumerable _cached; public static ApiTokenDTO? GetOne(string token) { var list = List(); if (!list.Any()) return null; return list.Where(e => e.token == token).FirstOrDefault(); // var token = new DBContext.Table("api_token").Get("token=@token", new { token = tokenString }); } public static IEnumerable List(bool force = false) { if (!force && _cached != null) return _cached; string cache_key = $"cache:api_token"; var list = RedisHelper.Get>(cache_key); if (force || list == null) { lock (_lockObj) { list = new DBContext.Table("api_token").Select(); if (list == null) return default; RedisHelper.Set(cache_key, list, 1 * 86400); } } _cached = list; return list; } public static void Refresh() { _cached = null; _ = List(true); } public static void Disabled(string name) { string cache_key = $"cache:api_token:{name}:disabled"; long count = RedisHelper.IncrBy(cache_key); RedisHelper.Expire(cache_key, 10); if (count > 1) return; new DBContext.Table("api_token") .Add("status", 0) .Where("name=@name", new { name }) .Update(); _ = List(true); } } }