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

  • 相关阅读:
    三维动画形变算法(Mixed Finite Elements)
    点集配准技术(ICP、RPM、KC、CPD)
    自由变形技术(Free-Form Deformation)
    物体自由落体动态模拟(Linear Subspace)
    小球自由落体动态模拟(Position Based Simulation)
    基于谱聚类的三维网格分割算法(Spectral Clustering)
    基于均值漂移的三维网格分割算法(Mean Shift)
    基于模糊聚类和最小割的层次化三维网格分割算法(Hierarchical Mesh Decomposition)
    基于网格的分割线优化算法(Level Set)
    骨骼蒙皮动画算法(Linear Blending Skinning)
  • 原文地址:https://www.cnblogs.com/hualiu0/p/9566785.html
Copyright © 2011-2022 走看看