增加清除缓存

This commit is contained in:
zpc 2024-08-05 04:47:35 +08:00
parent 37a7c95ada
commit 0c95613ed4
4 changed files with 95 additions and 2 deletions

View File

@ -17,8 +17,19 @@ namespace HuanMeng.MiaoYu.Code.Cache
/// <summary>
///
/// </summary>
public abstract class CommonDataEntityCache<T>(object lockObj, int cacheTime = 36000) : ICacheClearData, ICacheReloadData where T : class
public abstract class CommonDataEntityCache<T> : ICacheClearData, ICacheReloadData where T : class
{
//(object lockObj, int cacheTime = 36000)
protected object lockObj;
protected int cacheTime;
protected CommonDataEntityCache(object lockObj, int cacheTime = 36000)
{
this.lockObj = lockObj;
this.cacheTime = cacheTime;
}
/// <summary>
///
/// </summary>

View File

@ -2,9 +2,13 @@
using AutoMapper;
using HuanMeng.MiaoYu.Code.Cache.Contract;
using HuanMeng.MiaoYu.Code.Cache.Special;
using HuanMeng.MiaoYu.Code.DataAccess;
using HuanMeng.MiaoYu.Model.Dto.Character;
using System.Linq.Expressions;
namespace HuanMeng.MiaoYu.Code.Cache
{
/// <summary>
@ -217,7 +221,7 @@ namespace HuanMeng.MiaoYu.Code.Cache
object cacheLock;
if (!CacheLockList.TryGetValue(typeof(T), out cacheLock))
{
Console.WriteLine("没有找到锁对象==>"+typeof(T).Name);
Console.WriteLine("没有找到锁对象==>" + typeof(T).Name);
// 创建一个新的锁对象,并添加到 CacheLockList 中
cacheLock = new object();
CacheLockList[typeof(T)] = cacheLock;
@ -235,6 +239,27 @@ namespace HuanMeng.MiaoYu.Code.Cache
var cache = GetMiaoYuDataEntityCache<T>(dao);
return cache?.DataList ?? new List<T>();
}
/// <summary>
/// 清除全部缓存
/// </summary>
/// <param name="dao"></param>
public static void ClareMiaoYuDataEntityCache(DAO dao)
{
foreach (var item in CacheLockList)
{
var t = item.Key;
Type cacheType = typeof(MiaoYuDataEntityCache<>).MakeGenericType(t);
var shujuduixiang = Activator.CreateInstance(cacheType, dao, item.Value,36000);
var x = shujuduixiang as ICacheClearData;
if (x != null)
{
x.ClearData();
}
//new MiaoYuDataEntityCache<T>(dao, cacheLock);
//Activator.CreateInstance(t);
}
}
#endregion
#region

View File

@ -0,0 +1,27 @@
using HuanMeng.MiaoYu.Code.Cache;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HuanMeng.MiaoYu.Code.Other
{
/// <summary>
/// 杂项
/// </summary>
/// <param name="serviceProvider"></param>
public class OtherBLL(IServiceProvider serviceProvider) : MiaoYuBase(serviceProvider)
{
/// <summary>
/// 清除缓存
/// </summary>
/// <returns></returns>
public Task CleraCache()
{
MiaoYuCacheExtend.ClareMiaoYuDataEntityCache(Dao);
return Task.CompletedTask;
}
}
}

View File

@ -0,0 +1,30 @@
using HuanMeng.MiaoYu.Code.Other;
using HuanMeng.MiaoYu.WebApi.Base;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace HuanMeng.MiaoYu.WebApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class OtherController : MiaoYuControllerBase
{
public OtherController(IServiceProvider _serviceProvider) : base(_serviceProvider)
{
}
/// <summary>
/// 清除缓存
/// </summary>
/// <returns></returns>
[HttpGet("cache/clear")]
[AllowAnonymous]
public async Task Clear()
{
OtherBLL otherBLL = new OtherBLL(ServiceProvider);
await otherBLL.CleraCache();
}
}
}