【问题标题】:.net 6 and Redis cache integration using sets , hashes.net 6 和 Redis 缓存集成使用集合、哈希
【发布时间】:2022-02-10 16:05:04
【问题描述】:

.net 6 和 Redis 缓存集成使用集合、散列。 请为此提供源代码。 使用最新的堆栈交换库。

【问题讨论】:

    标签: asp.net-core-webapi .net-6.0 stackexchange.redis


    【解决方案1】:

    这是一个简单的存档演示,大家可以看看:

    程序.cs:

    builder.Services.AddStackExchangeRedisCache(options => {
        options.Configuration = "127.0.0.1:6379";
    });
    builder.Services.AddSession(); 
    
    app.UseSession();
    

    控制器:

    [ApiController]
        public class TestController : Controller
        {
            private readonly IDistributedCache _distributedCache;
    
            public TestController(IDistributedCache distributedCache)
            {
                _distributedCache = distributedCache;
            }
            [HttpGet]
            [Route("test")]
            public void Test()
            {      
                const string key = "message";
                const string value = "hello";
                _distributedCache.SetString(key, value);
    
    
            }
        }
    

    结果:

    更多例子可以参考这篇文章:

    Distributed caching in ASP.NET Core

    【讨论】:

    • 如果我想使用SETADD方法。是否可以使用 IDistributedCache ?
    猜你喜欢
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    相关资源
    最近更新 更多