要在ASP.NET Core中删除缓存条目,你可以使用内置的缓存管理器来完成。以下是一个示例代码:
using Microsoft.Extensions.Caching.Memory;
using System;
public class MyCacheService
{
private readonly IMemoryCache _cache;
public MyCacheService(IMemoryCache cache)
{
_cache = cache;
}
public void RemoveCacheEntry(string key)
{
_cache.Remove(key);
}
}
public class MyController : Controller
{
private readonly MyCacheService _cacheService;
public MyController(MyCacheService cacheService)
{
_cacheService = cacheService;
}
public IActionResult RemoveCacheEntry()
{
string cacheKey = "myCacheKey";
_cacheService.RemoveCacheEntry(cacheKey);
return Ok("Cache entry removed.");
}
}
在上面的示例中,我们首先注入了一个IMemoryCache
实例作为缓存管理器。然后,我们创建了一个MyCacheService
类,它接受IMemoryCache
实例作为依赖项,并提供一个方法来删除缓存条目。
在MyController
中,我们注入了MyCacheService
,并在RemoveCacheEntry
方法中调用了RemoveCacheEntry
方法来删除指定的缓存条目。
你可以根据自己的需要在控制器中调用RemoveCacheEntry
方法,传递相应的缓存键。