ASP.Net Core缓存
创始人
2024-09-15 21:00:45
0

ASP.NET Core提供了多种缓存选项,包括内存缓存、分布式缓存和响应缓存。下面是一些常见的代码示例:

  1. 内存缓存:
// 在Startup.cs文件中的ConfigureServices方法中添加以下代码

public void ConfigureServices(IServiceCollection services)
{
    services.AddMemoryCache();
    // ...
}

// 在Controller或其他地方使用内存缓存

private readonly IMemoryCache _memoryCache;

public MyController(IMemoryCache memoryCache)
{
    _memoryCache = memoryCache;
}

public IActionResult MyAction()
{
    string cacheKey = "myCacheKey";
    string cachedValue;

    if (!_memoryCache.TryGetValue(cacheKey, out cachedValue))
    {
        // 缓存中没有该键对应的值,执行获取数据的操作
        cachedValue = GetDataFromDataSource();
        _memoryCache.Set(cacheKey, cachedValue, TimeSpan.FromMinutes(10)); // 设置缓存,并指定过期时间
    }

    return View(cachedValue);
}
  1. 分布式缓存(使用Redis作为缓存提供程序):
// 在Startup.cs文件中的ConfigureServices方法中添加以下代码

public void ConfigureServices(IServiceCollection services)
{
    services.AddStackExchangeRedisCache(options =>
    {
        options.Configuration = "localhost:6379"; // Redis服务器的连接字符串
        options.InstanceName = "MyCache"; // 缓存实例的名称
    });
    // ...
}

// 在Controller或其他地方使用分布式缓存

private readonly IDistributedCache _distributedCache;

public MyController(IDistributedCache distributedCache)
{
    _distributedCache = distributedCache;
}

public IActionResult MyAction()
{
    string cacheKey = "myCacheKey";
    string cachedValue;

    byte[] cachedBytes = _distributedCache.Get(cacheKey);
    if (cachedBytes == null)
    {
        // 缓存中没有该键对应的值,执行获取数据的操作
        cachedValue = GetDataFromDataSource();
        _distributedCache.Set(cacheKey, Encoding.UTF8.GetBytes(cachedValue), new DistributedCacheEntryOptions
        {
            AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10) // 设置过期时间
        });
    }
    else
    {
        cachedValue = Encoding.UTF8.GetString(cachedBytes);
    }

    return View(cachedValue);
}
  1. 响应缓存:
// 在Controller的Action中使用响应缓存

[ResponseCache(Duration = 3600)] // 缓存响应结果1小时
public IActionResult MyAction()
{
    string cachedValue = GetCachedData(); // 获取缓存数据的方法

    if (cachedValue != null)
    {
        return Ok(cachedValue);
    }

    // 缓存中没有数据,执行获取数据的操作
    string newData = GetDataFromDataSource();
    SetCachedData(newData); // 将数据存入缓存的方法

    return Ok(newData);
}

以上是一些常见的ASP.NET Core缓存的代码示例。根据具体需求和情况,可以选择适合的缓存选项和配置。

相关内容

热门资讯

透视美元局(wepoker)外... 1、透视美元局(wepoker)外挂透明挂辅助插件(wepoke辅助德之星)其实有挂(详细透视科技教...
透视软件(Wepoke)外挂透... 透视软件(Wepoke)外挂透明挂辅助app(wepokeai辅助)好像是真的有挂(详细透视辅助教程...
透视规律(wepoke计算辅助... 透视规律(wepoke计算辅助)聚星扑克德州有假吗(详细辅助黑科技教程)其实是真的有挂1)wepok...
德州ai辅助!菠萝德州app挂... 德州ai辅助!菠萝德州app挂哪里买,(wEPOKE)本来是真的有挂(详细透视新版2025教程);1...
透视app(wepoker)透... 1、透视app(wepoker)透视辅助挂(wepoke真的有挂)本来真的有挂(详细透视线上教程)。...
透视存在(德州ai人工智能)a... 透视存在(德州ai人工智能)aapoker透明挂多久被封(详细辅助微扑克教程)最初是真的有挂1.德州...
透视真的(wEpoKe)透明挂... 自定义wepoke辅助插件系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,...
德州ai机器人!微扑克机器人代... 1、德州ai机器人!微扑克机器人代打俱乐部,(wEPOKE)原先存在有挂(详细辅助德州教程);详细教...
透视透视(we辅助poker德... 透视透视(we辅助poker德之星)轰趴大菠萝十三水辅助(详细辅助插件教程)一贯存在有挂是一款可以让...
智星德州菠萝开挂!we辅助po... 1、智星德州菠萝开挂!we辅助poker德之星,(WepOke)原本是有挂(详细辅助透牌教程)(UU...