文档章节

abp框架里使用Redis

o
 osc_y8yehimr
发布于 2019/03/20 14:31
字数 350
阅读 8
收藏 0

首先引用 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)查看  

 

o
粉丝 0
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。

暂无文章

如何在Ruby中生成随机字符串 - How to generate a random string in Ruby

问题: I'm currently generating an 8-character pseudo-random uppercase string for "A" .. "Z": 我目前正在为“ A” ..“ Z”生成一个8个字符的伪随机大写字符串: value = ""; 8.times{......

法国红酒甜
今天
20
0
Python中的mkdir -p功能[重复] - mkdir -p functionality in Python [duplicate]

问题: This question already has an answer here: 这个问题在这里已有答案: How can I safely create a nested directory? 如何安全地创建嵌套目录? 25 answers 25个答案 Is there a way...

技术盛宴
今天
21
0
原价500元的认证证书,限时免费考取!

本文作者:y****n 百度云智学院致力于为百度ABC战略(人工智能、大数据、云计算)提供人才生态体系建设,包括基于百度ABC、IoT的课程体系,整合百度优势技术能力的深度学习技术、Apollo无人车...

百度开发者中心
昨天
17
0
在virtualenv中使用Python 3 - Using Python 3 in virtualenv

问题: Using virtualenv , I run my projects with the default version of Python (2.7). 使用virtualenv ,我使用默认版本的Python(2.7)运行项目。 On one project, I need to use Pyth......

富含淀粉
今天
16
0
Python的__init__和self是做什么的? - What __init__ and self do on Python?

问题: I'm learning the Python programming language and I've came across something I don't fully understand. 我正在学习Python编程语言,遇到了一些我不太了解的东西。 In a method ......

javail
今天
15
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部