abp框架里使用Redis

首先引用 nuget Abp.RedisCache

在 appsettings.json加上Redis服务器配置

  "RedisCache": {
      "ConnectionString": "xx.xx.xx.xx:5001,password=xxx",
      "DatabaseId": "-1"

    }

  

然后在项目的依赖文件 AbpModule里加上Redis相关配置

 1 using Abp.AutoMapper;
 2 using Abp.Modules;
 3 using Abp.Reflection.Extensions;
 4 using Abp.Runtime.Caching.Redis;
 5 using System;
 6 
 7 namespace BossHelper
 8 {
 9     [DependsOn(
10         typeof(BossHelperCoreModule),
11         typeof(AbpAutoMapperModule),
12         typeof(AbpRedisCacheModule)
13         )]
14 
15     public class BossHelperApplicationModule : AbpModule
16     {
17         public override void PreInitialize()
18         {
19             int DatabaseId = -1;
20             int.TryParse(ConfigHelper.GetAppSetting("App", "RedisCache:DatabaseId"), out DatabaseId);
21             //配置使用Redis缓存
22             Configuration.Caching.UseRedis(options =>
23             {
24                 options.ConnectionString = ConfigHelper.GetAppSetting("App", "RedisCache:ConnectionString");
25                 options.DatabaseId = DatabaseId;
26             });
27 
28             //配置所有Cache的默认过期时间为2小时
29             Configuration.Caching.ConfigureAll(cache =>
30             {
31                 cache.DefaultSlidingExpireTime = TimeSpan.FromHours(2);
32             });
33 
34             //配置指定的Cache过期时间为10分钟
35             Configuration.Caching.Configure("BossAssistant", cache =>
36             {
37                 cache.DefaultSlidingExpireTime = TimeSpan.FromMinutes(10);
38             });
39 
40             Configuration.Authorization.Providers.Add<BossHelperAuthorizationProvider>();
41         }
42 
43         public override void Initialize()
44         {
45             var thisAssembly = typeof(BossHelperApplicationModule).GetAssembly();
46 
47             IocManager.RegisterAssemblyByConvention(thisAssembly);
48 
49             Configuration.Modules.AbpAutoMapper().Configurators.Add(
50                 // Scan the assembly for classes which inherit from AutoMapper.Profile
51                 cfg => cfg.AddProfiles(thisAssembly)
52             );
53         }
54     }
55 }
 
 
最后使用

1)实例化

 public ILogger Logger { get; set; }
        private readonly string AppId = ConfigHelper.GetAppSetting("App", "AppID");
        private readonly string AppSecret = ConfigHelper.GetAppSetting("App", "AppSecret");
        private readonly ICacheManager _cacheManage;


        private readonly IRepository<wxapp_user, long> _wxappUserRepository;
        private readonly IRepository<boss_admin, long> _bossAdminRepository;
        private readonly IRepository<BossQrcode, long> _bossQrCodeRepository;
        private readonly IRepository<boss_role, long> _bossRoleRepository;
        private readonly IRepository<shopinfo, long> _shopInfoRepository;


        public WxappUserService(IRepository<wxapp_user, long> wxappUserRepository, IRepository<boss_admin, long> bossAdminRepository, IRepository<BossQrcode, long> bossQrCodeRepository, IRepository<boss_role, long> bossRoleRepository, IRepository<shopinfo, long> shopInfoRepository, ICacheManager cacheManage)
        {
            _wxappUserRepository = wxappUserRepository;
            _bossAdminRepository = bossAdminRepository;
            _bossQrCodeRepository = bossQrCodeRepository;
            _bossRoleRepository = bossRoleRepository;
            _shopInfoRepository = shopInfoRepository;
            Logger = NullLogger.Instance;
            _cacheManage = cacheManage;
        }
View Code

2)SET

   _cacheManage.GetCache("BossAssistant").Set(codeResult.openid, codeResult.session_key);

  

3)GET

   var sessionKey = _cacheManage.GetCache("BossAssistant").Get(model.openid, (val) =>
                {
                    return val;
                }) as string;

4)查看  

 

转载于:https://www.cnblogs.com/WQ1992/p/10564754.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值