在ABP框架中存在一个缓存机制,使用ICache的继承类来存储最终需要缓存的数据,可以吧ICache看成一个字典对象,使用Key作为真实数据的具有唯一性的表示。使用上与字典对象完全相同,Get方法传递Key,还有数据工厂作为参数就能返回最终的值,Set方法用于存储。包含一个TimeSpan属性用于指定最长不使用数据的过期时间。

/// <summary>
/// Defines a cache that can be store and get items by keys.
/// </summary>
public interface ICache : IDisposable
{
/// <summary>
/// Unique name of the cache.
/// </summary>
string Name { get; } /// <summary>
/// Default sliding expire time of cache items.
/// Default value: 60 minutes. Can be changed by configuration.
/// </summary>
TimeSpan DefaultSlidingExpireTime { get; set; } /// <summary>
/// Gets an item from the cache.
/// </summary>
/// <param name="key">Key</param>
/// <param name="factory">Factory method to create cache item if not exists</param>
/// <returns>Cached item</returns>
object Get(string key, Func<string, object> factory); /// <summary>
/// Gets an item from the cache.
/// </summary>
/// <param name="key">Key</param>
/// <param name="factory">Factory method to create cache item if not exists</param>
/// <returns>Cached item</returns>
Task<object> GetAsync(string key, Func<string, Task<object>> factory); /// <summary>
/// Gets an item from the cache or null if not found.
/// </summary>
/// <param name="key">Key</param>
/// <returns>Cached item or null if not found</returns>
object GetOrDefault(string key); /// <summary>
/// Gets an item from the cache or null if not found.
/// </summary>
/// <param name="key">Key</param>
/// <returns>Cached item or null if not found</returns>
Task<object> GetOrDefaultAsync(string key); /// <summary>
/// Saves/Overrides an item in the cache by a key.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <param name="slidingExpireTime">Sliding expire time</param>
void Set(string key, object value, TimeSpan? slidingExpireTime = null); /// <summary>
/// Saves/Overrides an item in the cache by a key.
/// </summary>
/// <param name="key">Key</param>
/// <param name="value">Value</param>
/// <param name="slidingExpireTime">Sliding expire time</param>
Task SetAsync(string key, object value, TimeSpan? slidingExpireTime = null); /// <summary>
/// Removes a cache item by it's key.
/// </summary>
/// <param name="key">Key</param>
void Remove(string key); /// <summary>
/// Removes a cache item by it's key (does nothing if given key does not exists in the cache).
/// </summary>
/// <param name="key">Key</param>
Task RemoveAsync(string key); /// <summary>
/// Clears all items in this cache.
/// </summary>
void Clear(); /// <summary>
/// Clears all items in this cache.
/// </summary>
Task ClearAsync();
}

  框架中提供了一个ICache的抽象实现类CacheBase,作为所有具体缓存方式的基类,实现了所有基础逻辑,最终的Cache对象只需继承CacheBase,并重写object GetOrDefault(string key),void Set(string key, object value, TimeSpan? slidingExpireTime = null),void Clear()等三个抽象方法就可以完成一个真实可用的缓存Cache(另外Dispose方法通常是需要重写)。同时CacheBase的所有其他公共方法也是可重写的。ABP中默认提供了一个以内存方式缓存数据的实现即AbpMemoryCache,只是简单实用微软的MemoryCache类作为内部缓存容器。

  ICacheManager使用单例模式用于管理所有的ICache,其中包含一个传递Key返回ICache的GetCache方法,还有一个返回所有ICache的GetAllCaches方法,在调用GetCache传递Key的时候如果不存在就会创建一个新的保存起来,对于ICacheManager框架中已经存在一个名为CacheManagerBase的抽象实现了,其内部有一个线程安全的字典用于存放所有的ICache,用于需要继承CacheManagerBase实现自身的CacheManager,只需要实现抽象方法CreateCacheImplementation就行了,最简单的方式就是在方法内部返回一个AbpMemoryCache实例就行了

/// <summary>
/// An upper level container for <see cref="ICache"/> objects.
/// A cache manager should work as Singleton and track and manage <see cref="ICache"/> objects.
/// </summary>
public interface ICacheManager : IDisposable
{
/// <summary>
/// Gets all caches.
/// </summary>
/// <returns>List of caches</returns>
IReadOnlyList<ICache> GetAllCaches(); /// <summary>
/// Gets (or creates) a cache.
/// </summary>
/// <param name="name">Unique name of the cache</param>
/// <returns>The cache reference</returns>
ICache GetCache(string name);
}

  缓存机制中存在一个ICachingConfiguration的配置类,用户可以在自定义的模块中添加ICache的初始化处理逻辑,通常是一个Action<ICache>的委托,添加到配置类的Configurators属性中,每次被创建一个新的ICache对象的时候都会遍历Configurators属性中名称与新建ICache相同,或者无名称的全局性Action<ICache>,用来初始化ICache。

/// <summary>
/// Used to configure caching system.
/// </summary>
public interface ICachingConfiguration
{
/// <summary>
/// List of all registered configurators.
/// </summary>
IReadOnlyList<ICacheConfigurator> Configurators { get; } /// <summary>
/// Used to configure all caches.
/// </summary>
/// <param name="initAction">
/// An action to configure caches
/// This action is called for each cache just after created.
/// </param>
void ConfigureAll(Action<ICache> initAction); /// <summary>
/// Used to configure a specific cache.
/// </summary>
/// <param name="cacheName">Cache name</param>
/// <param name="initAction">
/// An action to configure the cache.
/// This action is called just after the cache is created.
/// </param>
void Configure(string cacheName, Action<ICache> initAction);
}
/// <summary>
/// A registered cache configurator.
/// </summary>
public interface ICacheConfigurator
{
/// <summary>
/// Name of the cache.
/// It will be null if this configurator configures all caches.
/// </summary>
string CacheName { get; } /// <summary>
/// Configuration action. Called just after the cache is created.
/// </summary>
Action<ICache> InitAction { get; }
}

  另外因为所有数据都是以字符串为Key,值为Object的方式存储的,所以对于一些依赖强类型的使用不太方便,作者也添加了ITypedCache<TKey,TValue>的方式,用于返回强类型的数据,虽然使用方便的,但是ICache所能缓存的对象也有了限制。

  另外在WebApi端的存储,作者新建一个独立的AbpCacheController控制器,新建了几个Clear方法方便用户(通常是系统管理员级别的)通过远程调用WebApi方法的方式来清理缓存,为了安全起见需要提供一个安全验证码的,原生的是在SettingManager中写死一个密码参数和值,只要告诉管理员密码就行了,在访问AbpCacheController的Clear方法的时候提供预先知道的密码就有权限修改了,这个是非常不方便的,所以建议为清理缓存设置一个单独的Permission,并且添加一个AOP(WebApi的ActionFilter或者直接在AbpApiAuthorieAttribute中)根据当前用户的IAbpSession查看是否有权限清理Cache。

  

ABP框架详解(七)Caching的更多相关文章

  1. ABP框架详解(二)AbpKernelModule

    AbpKernelModule类是Abp框架自己的Module,它也跟所有其他的Module一样继承自AbpModule,重写PreInitialize,Initialize,PostInitiali ...

  2. ABP框架详解(八)动态ApiController的生成和访问机制

    在ABP框架中提供了一套动态生成ApiController的机制(依然支持原生ApiController的使用方式),虽然说是动态生成ApiController但是实际上并没有真正在启动程序的时候生成 ...

  3. ABP框架详解(五)Navigation

    ABP框架中的Navigation功能用于管理业务系统中所有可用的菜单导航控件,通常在业务系统的首页会有一个全局性的导航菜单,JD商城,天猫,猪八戒网莫不如是.所以为方便起见,Navigation功能 ...

  4. ABP框架详解(四)Feature

    ABP框架中存在一个Feature的特性,功能和设计思路非常类似于框架中的Authorization功能,都是来控制用户是否能够继续操作某项功能,不同点在于Authorization默认是应用在IAp ...

  5. ABP框架详解(三)Domain

    此处的Domain主要指Abp类库根目录下Domain文件夹.顾名思义该目录下是用来存放与领域实体,领域逻辑执行,存储,领域服务相关的内容. 1.Entities (1)为整个Abp框架后期开发的所有 ...

  6. ABP框架详解(一)ABPBootstrapper

    在ABP框架的AbpBootstrapper主要用于框架的基本配置的注册和初始化,在Web应用启动阶段实例化一个AbpBootstrapper并调用Initialize方法初始化,该类主要包含两个公有 ...

  7. ABP框架详解(六)Aspects

    这种AOP式的设计非常类似于Asp.net MVC和WebApi中过滤器(Filter)机制,感觉没有太多可讲述的,只能谈谈设计思路. 框架中AspectAttribute特性用于设置到需要被拦截的T ...

  8. mina框架详解

     转:http://blog.csdn.net/w13770269691/article/details/8614584 mina框架详解 分类: web2013-02-26 17:13 12651人 ...

  9. Spark2.1.0——内置RPC框架详解

    Spark2.1.0——内置RPC框架详解 在Spark中很多地方都涉及网络通信,比如Spark各个组件间的消息互通.用户文件与Jar包的上传.节点间的Shuffle过程.Block数据的复制与备份等 ...

随机推荐

  1. windows无法停用“Android Composite Interface”设备的解决方法

    遇到这个问题时,解决方法如下: 打开设备管理器 -> 进程 -> 找到 adb.exe -> 右击选择结束进程 -> 然后重新弹出Android设备

  2. Asp.Net MVC4入门指南(8):给数据模型添加校验器

    在本节中将会给Movie模型添加验证逻辑.并且确保这些验证规则在用户创建或编辑电影时被执行. 保持事情 DRY ASP.NET MVC 的核心设计信条之一是DRY: "不要重复自己(Don’ ...

  3. IIS发布WCF服务项目之本地

    最近由于项目需求,要做一个上传文件附件的功能,由于是多用户访问,所以这就用到了WCF服务,程序编写完成就需要发布了, 下面记录下发布到IIS的过程: 1,安装IIS 第一步:检查Windows7中II ...

  4. WPF Image控件的Source属性是一个ImageSource对象

    1.固定的图片路径是可以的,如下: <Image Source="../test.png" /> 2.绑定的Path是一个String类型的图片路径,而实际情况它需要的 ...

  5. Java多线程开发技巧

    很多开发者谈到Java多线程开发,仅仅停留在new Thread(...).start()或直接使用Executor框架这个层面,对于线程的管理和控制却不够深入,通过读<Java并发编程实践&g ...

  6. 如何使用Nginx对抗DDoS攻击?

    时不时的就有客户会被DDoS一下.很多时候攻击很简单也容易封堵,但是攻击的目标是应用的时候就更难防御.在这里云端卫士介绍一下使用Nginx作为代理过滤器来封堵一些这种攻击. Apache DDoS攻击 ...

  7. 配置与使用 Git与Github

    Git与Github的注册.配置及使用方法在网上有详细的教程,不做总结. 这里主要总结我在Git.Github的配置及使用过程中遇到的问题.如下: (1) Git与Github的关系.两者都是版本管理 ...

  8. C# Form.Close 的释放问题

    今天使用From窗口Close后,发现From的资源还存在,并没有释放资源,只有在程序关闭的时候才去释放. Form1:button按钮 private void button1_Click(obje ...

  9. VS与ultraedit 正则表达式替换

    ASP中把request("{param}")调用替换为requestX("{param}") VS 表达式替换(?<a>request\(&quo ...

  10. 产生NaN

    1.数学运算失败  数字+undefind=NaN 2.数据类型转化失败产生NaN isNaN()检查 是不是一个有效数字,是NaN 返回 ture  ,正常数字返回false.