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)查看  

 

posted @ 2019-03-20 14:31  w1433189426  阅读(1457)  评论(1编辑  收藏  举报