| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using dodohold.core;
- using System.Data;
- using System.Text.RegularExpressions;
- using YunhuiKit;
- namespace molilian.core
- {
- public partial class OverrideRuleCore
- {
- private static IEnumerable<OverrideRuleDTO> _cached;
- private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
- public static async Task<IEnumerable<OverrideRuleDTO>> ListAsync(bool force = false)
- {
- if (!force && _cached != null) return _cached;
- try
- {
- await _semaphore.WaitAsync();
- // 如果缓存存在且未强制刷新,直接返回
- if (!force && _cached != null) return _cached;
- string cache_key = $"cache:tk_override_rules";
- IEnumerable<OverrideRuleDTO>? list = null;
- // 尝试从Redis获取数据
- try
- {
- list = await RedisKit.GetAsync<IEnumerable<OverrideRuleDTO>>(cache_key);
- }
- catch (Exception ex)
- {
- // 记录Redis错误
- _ = new LoggerLibrary("OverrideRule", "Redis").Info(ex.Message, ex.StackTrace).SaveAsync();
- }
- // 如果Redis获取失败或需要强制刷新
- if (force || list == null)
- {
- try
- {
- list = new DBContext.Table("tk_override_rules")
- .Where("status=@status", new { status = 1 })
- .Order("sort DESC, id DESC")
- .Select<OverrideRuleDTO>();
- if (list != null && list.Any())
- {
- // 尝试更新Redis缓存
- try
- {
- await RedisKit.SetAsync(cache_key, list, 30 * 86400);
- }
- catch (Exception ex)
- {
- // 记录Redis更新错误
- _ = new LoggerLibrary("OverrideRule", "Redis").Info(ex.Message, ex.StackTrace).SaveAsync();
- }
- }
- }
- catch (Exception ex)
- {
- // 记录数据库查询错误
- _ = new LoggerLibrary("OverrideRule", "Database").Info(ex.Message, ex.StackTrace).SaveAsync();
- // 如果数据库查询失败但缓存还在,继续使用缓存
- if (_cached != null) return _cached;
- throw; // 如果没有任何可用数据,则抛出异常
- }
- }
- _cached = list;
- return list ?? [];
- }
- finally
- {
- _semaphore.Release();
- }
- }
- public static async Task<OverrideRuleDTO> ProcessAsync(string content)
- {
- var list = await ListAsync();
- foreach (var item in list)
- {
- if (!"tk".Equals(item.platform)) continue;
- if (string.IsNullOrEmpty(item.original_text)) continue;
- switch (item.rule)
- {
- case "regex":
- Match match = Regex.Match(content, item.original_text);
- if (!match.Success) continue;
- return item;
- default:
- if (!content.Contains(item.original_text)) continue;
- return item;
- }
- }
- return null;
- }
- public static void Refresh()
- {
- _cached = null;
- _ = ListAsync(true);
- }
- public static int Update(OverrideRuleDTO data, IDbConnection conn)
- {
- var result = (int)conn.Update<OverrideRuleDTO>(data, new { data.id });
- _ = ListAsync(true);
- #if DEBUG
- #else
- EndPointCore.NotifyReload();
- #endif
- return result;
- }
- public static int Create(OverrideRuleDTO data, IDbConnection conn)
- {
- var result = (int)conn.Insert(data);
- _ = ListAsync(true);
- #if DEBUG
- #else
- EndPointCore.NotifyReload();
- #endif
- return result;
- }
- }
- }
|