• EasyCaching——redis


    EasyCaching

    EasyCaching作者大大博客

    官方文档

    GitHub地址

    个人实例

    1.安装包

    EasyCaching.Core
    EasyCaching.Redis
    EasyCaching.Serialization.MessagePack
    这里是以1.4.0为例

    2.服务注册

    netcore3.0,5.0是在startup,net6是在program设置

    builder.Services.AddEasyCaching(option =>
    {
        option.UseRedis(t =>
        {
        	//当需要连接redis集群时,只需要连接一个redis,其他会自动关联上,只需要 t.DBConfig.Endpoints.Add(new EasyCaching.Core.Configurations.ServerEndPoint("服务器地址",6379) )
            //43.138.88.44服务器地址,6379端口
            t.DBConfig.Endpoints.Add(new EasyCaching.Core.Configurations.ServerEndPoint("服务器地址",6379) );
            //连接那个数据库
            t.DBConfig.Database = 1;
            //数据库密码
            t.DBConfig.Password = "服务器密码";
        });
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3.使用

    Person辅助类

        public class Person
        {
            public string Name { get; set; }
            public string Sex { get; set; }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    ValuesController.cs

    using EasyCaching.Core;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;
    using Newtonsoft.Json;
    
    namespace RedisTest.Controllers
    {
        [Route("api/[controller]")]
        [ApiController]
        public class ValuesController : ControllerBase
        {
            private readonly IRedisCachingProvider _redisCachingProvider;
            public ValuesController(IRedisCachingProvider redisCachingProvider)
            {
                _redisCachingProvider = redisCachingProvider;
            }
            [HttpPost]
            [Route("select-cache")]
            public async Task<string>  SelectCache()
            {
                return await _redisCachingProvider.StringGetAsync("name");
            }
            //这里在key不变的情况下,将上面的性别更改了
            [HttpPost]
            [Route("update-cache-sex")]
            public async Task UpdateCache()
            {
                Person person = new Person
                {
                    Name = "lty",
                    Sex = "woman"
                };
                await _redisCachingProvider.StringSetAsync("name", JsonConvert.SerializeObject(person),
                            TimeSpan.FromMinutes(1));
            }
    
    		//这里在修改了缓存的key,尽管其他没有改变,但是会重新创建一个缓存
            [HttpPost]
            [Route("update-cache-sex-key")]
            public async Task UpdateCacheSexKey()
            {
                Person person = new Person
                {
                    Name = "lty",
                    Sex = "woman"
                };
                await _redisCachingProvider.StringSetAsync("womanname", JsonConvert.SerializeObject(person),
                            TimeSpan.FromMinutes(1));
            }
            [HttpPost]
            [Route("remove-cache")]
            public async Task RemoveCache()
            {
                Person person = new Person
                {
                    Name = "lty",
                    Sex = "man"
                };
                await _redisCachingProvider.KeyDelAsync("name");
                           
            }
            [HttpPost]
            [Route("create-cache")]
            public async Task CreateCache()
            {
                Person person = new Person
                {
                    Name = "lty",
                    Sex = "man"
                };
                await _redisCachingProvider.StringSetAsync("name", JsonConvert.SerializeObject(person),
                            TimeSpan.FromMinutes(1));
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
  • 相关阅读:
    Codeforces Round #809 (Div. 2)
    git的下载与初始配置
    基于虚拟同步发电机控制的双机并联Simulink仿真模型
    AI:141-利用自然语言处理改进医疗信息提取与分类
    4.整合第三方技术【整合JUnit】
    【计算机网络】虚拟路由冗余(VRRP)协议原理与配置
    6.1 KMP算法搜索机器码
    [从零开始学习FPGA编程-57]:视野篇-异构系统、异构芯片、大小核、芯片互联网、UCIe标准
    Python集合魔法:解锁数据去重技巧
    【NPM】vuex 数据持久化库 vuex-persistedstate
  • 原文地址:https://blog.csdn.net/wsnbbdbbdbbdbb/article/details/127673415