zoukankan      html  css  js  c++  java
  • 使用abp的 redis cache

    top

    使用abp的 redis cache

    -1. 在微软维护的github项目的release里找到redis的windows版本 64位 大约5M,安装,安装,然后在安装目录找到redis.windows.conf, 更改redis的密码 requirepassword 123456, 更改最大上限 200M 或自定; 启动redis-server.exe,默认监听6379端口; 启动redis-cli.exe进入reds,然后命令很简单,就是简单的get,set:

    > set akey avalue
    > get akey
    显示 avalue
    > del akey
    删除akey
    
    1. 在nuget里面搜索abp.redis,或者在nuget console里面Install-Package abp.redis在service层或者web层,使用 redis cache integration,在module层的preinitialize中使用
    //...other namespaces
    using Abp.Runtime.Caching.Redis;
    
    namespace MyProject.AbpZeroTemplate.Web
    {
        [DependsOn(
            //...other module dependencies
            typeof(AbpRedisCacheModule))]
        public class MyProjectWebModule : AbpModule
        {
            public override void PreInitialize()
            {
                //...other configurations
                
                Configuration.Caching.UseRedis();
            }
            
            //...other code
        }
    }
    
    1. 注入 ICacheManager
    public DemoAppService:ApplicationService{
                private IRepository<Simple> _simpleRepository;
            private ICacheManager _cacheManager;
    
            public SimpleAppService(IRepository<Simple> simpleRepository, ICacheManager cacheManager)
            {
                _simpleRepository = simpleRepository;
                _cacheManager = cacheManager;
            }
    
            public int GetSaveRedisId()
            {
                var simple = new Simple() { Name = "simple" };
                var id = _simpleRepository.InsertAndGetId(simple);
                var simpleFromCache = _cacheManager.GetCache("simple").Get(id, () => { return _simpleRepository.Get(id); });
                //每一个get方法其实就是set方法,如果缓存不存在就先创建,如果缓存不存在就先找到东西填充起来再返回结果
                return simpleFromCache.Id;
            }
    }
    
    1. 使用cache get
    ITypedCache<int,Item> myCache=_cacheManager.GetCache<int,Item>("myCqche");
    
    1. Config Cache
    //Configuration for all caches
    Configuration.Caching.ConfigureAll(cache =>
    {
        cache.DefaultSlidingExpireTime = TimeSpan.FromHours(2);
    });
    
    //Configuration for a specific cache
    Configuration.Caching.Configure("MyCache", cache =>
    {
        cache.DefaultSlidingExpireTime = TimeSpan.FromHours(8);
    });
    
    1. using redis visualizer
    download from  https://redisdesktop.com/download
    
    
    1. EntityCache in abp
    //1. create an entity
    public class Person:Entity{ public string Name{get;set;}}
    //2. create a cache item
    [AutoMapFrom(typeof(Person))]
    public class PersonCacheItem{
        public string Name{get;set;}
    }
    //3.create person cache interface
    public interface IPersonCache:IEntityCache<PersonCacheItem>{
    }
    //4.create cache class for Person with cacheManager and Person repository
    public class PersonCache : EntityCache<Person, PersonCacheItem>, IPersonCache, ITransientDependency
    {
        public PersonCache(ICacheManager cacheManager, IRepository<Person> repository)
            : base(cacheManager, repository)
        {
    
        }
    }
    //5.create personservice only with personCache
    public class MyPersonService : ITransientDependency
    {
        private readonly IPersonCache _personCache;
    
        public MyPersonService(IPersonCache personCache)
        {
            _personCache = personCache;
        }
    
        public string GetPersonNameById(int id)
        {
            return _personCache[id].Name; //alternative: _personCache.Get(id).Name;
        }
    }
    // now the person entity is everytime stored in cache
    
    1. 总结使用redis: 一般就是设定某个缓存的时间,或者总体设定所有缓存的时间, 然后通过api来执行相关的命令来操作缓存
    这样这篇文章就完成了redis客户端的安装,然后还有abp框架里面引入redis客户端,使用相关接口的方法,在application service的api里面就可以通过注入的cachemanager操作redis缓存了;
    还介绍了使用entitycache,把实体和cache的操作都放到entity cache的一套中间件层集成里面,在仓储里默认就有增删改查等一系列方法。

    GoToTop

  • 相关阅读:
    centos7.6 使用yum安装mysql5.7
    解决hadoop本地库问题
    docker-compose 启动警告
    docker 安装zabbix5.0 界面乱码问题解决
    docker 部署zabbix问题
    zookeeper 超时问题
    hbase regionserver异常宕机
    (转载)hadoop 滚动升级
    hadoop Requested data length 86483783 is longer than maximum configured RPC length
    zkfc 异常退出问题,报错Received stat error from Zookeeper. code:CONNECTIONLOSS
  • 原文地址:https://www.cnblogs.com/hualiu0/p/9566785.html
Copyright © 2011-2022 走看看