概念什么是緩存,在項(xiàng)目中,為了提高數(shù)據(jù)的讀取速度,我們會(huì)對(duì)不經(jīng)常變更但訪問(wèn)頻繁的數(shù)據(jù)做緩存處理,我們常用的緩存有:
- 本地緩存
- 內(nèi)存緩存:IMemoryCache
- 分布式緩存
- Redis: StackExchange.Redis
MasaFramework為我們提供了以下能力- IDistributedCacheClient: 分布式緩存
- Masa.Contrib.Caching.Distributed.StackExchangeRedis: 基于StackExchange.Redis實(shí)現(xiàn)的分布式緩存
- IMultilevelCacheClient: 多級(jí)緩存
- Masa.Contrib.Caching.MultilevelCache: 基于內(nèi)存緩存以及分布式緩存實(shí)現(xiàn)的多級(jí)緩存,支持監(jiān)控緩存變更,分布式緩存更新后相應(yīng)的內(nèi)存緩存也會(huì)同步更新,避免命中過(guò)時(shí)的內(nèi)存緩存導(dǎo)致獲取錯(cuò)誤的數(shù)據(jù),同時(shí)也盡可能的將多個(gè)副本的內(nèi)存緩存保持同步
- 前提條件:安裝.NET 6.0
- 新建ASP.NET Core 空項(xiàng)目
Assignment.DistributedCache,并安裝Masa.Contrib.Caching.Distributed.StackExchangeRedis
dotnet new web -o Assignment.DistributedCachecd Assignment.DistributedCachedotnet add package Masa.Contrib.Caching.Distributed.StackExchangeRedis --version 0.6.0-rc.5- 配置
Redis配置信息
{"RedisConfig":{"Servers":[{"Host":"localhost","Port":6379}],"DefaultDatabase":3,"ConnectionPoolSize":10}}- 注冊(cè)分布式緩存,并使用
Redis緩存,修改Program.cs
var builder = WebApplication.CreateBuilder(args);//注冊(cè)分布式緩存builder.Services.AddDistributedCache(distributedCacheOptions =>{distributedCacheOptions.UseStackExchangeRedisCache();//使用分布式Redis緩存, 默認(rèn)使用本地`RedisConfig`下的配置});使用分布式緩存的數(shù)據(jù)來(lái)源默認(rèn)為IOptionsMonitor<RedisConfigurationOptions>,如果本地未正確在RedisConfig節(jié)點(diǎn)配置緩存信息,且項(xiàng)目中也沒(méi)有通過(guò)其它方式配置使其支持選項(xiàng)模式,則默認(rèn)使用的Redis配置為: 地址: localhost、端口:6379,密碼:空,數(shù)據(jù)庫(kù):db0
- 新建
User類,用于接收用戶信息
public class User{public string Name { get; set; }public int Age { get; set; }}- 如何使用
IDistributedCacheClient,修改Program.cs
// 設(shè)置緩存app.MapPost("/set/{id}", async (IDistributedCacheClient distributedCacheClient, [FromRoute] string id, [FromBody] User user) =>{await distributedCacheClient.SetAsync(id, user);return Results.Accepted();});// 獲取緩存app.MapGet("/get/{id}", async (IDistributedCacheClient distributedCacheClient, [FromRoute] string id) =>{var value = https://www.huyubaike.com/biancheng/await distributedCacheClient.GetAsync(id);return Results.Ok(value);}); 多級(jí)緩存- 新建ASP.NET Core 空項(xiàng)目
Assignment.DistributedCache,并安裝Masa.Contrib.Caching.MultilevelCache、Masa.Contrib.Caching.Distributed.StackExchangeRedis
dotnet new web -o Assignment.MultilevelCachecd Assignment.MultilevelCachedotnet add package Masa.Contrib.Caching.MultilevelCache --version 0.6.0-rc.5dotnet add package Masa.Contrib.Caching.Distributed.StackExchangeRedis --version 0.6.0-rc.5- 注冊(cè)多級(jí)緩存,并使用分布式
Redis緩存,修改Program.cs
var builder = WebApplication.CreateBuilder(args);//注冊(cè)多級(jí)緩存builder.Services.AddMultilevelCache(distributedCacheOptions =>{distributedCacheOptions.UseStackExchangeRedisCache();//使用分布式Redis緩存});- 新建
User類,用于接收用戶信息
public class User{public string Name { get; set; }public int Age { get; set; }}
經(jīng)驗(yàn)總結(jié)擴(kuò)展閱讀
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 直播帶貨小白如何入門(mén)
- 【番外篇】Rust環(huán)境搭建+基礎(chǔ)開(kāi)發(fā)入門(mén)+Rust與.NET6、C++的基礎(chǔ)運(yùn)算性能比較
- disco diffusionAI繪畫(huà)保姆級(jí)入門(mén)教程
- 一 JPA入門(mén)學(xué)習(xí)集合springboot
- 原神室內(nèi)派考古入門(mén)任務(wù)怎么做
- 七 SpringBoot - Redis 緩存
- Flink WordCount入門(mén)
- 一篇文章帶你了解網(wǎng)頁(yè)框架——Vue簡(jiǎn)單入門(mén)
- 【C++】spdlog光速入門(mén),C++logger最簡(jiǎn)單最快的庫(kù)
- 小白轉(zhuǎn)行入門(mén)STM32----手機(jī)藍(lán)牙控制STM32單片機(jī)點(diǎn)亮LED
