# 缓存助手
具体代码可查看
LessSharp.Common
层的CacheHelper
目录。
框架集成了MemoryCache
跟Redis
的缓存机制,当在配置文件Redis
配置选项的Enable
为false
时,就使用MemoryCache
,为true
就启用Redis
。
方法 | 说明 |
---|---|
Exists(string key) | 根据key值判断缓存是否存在 |
Set<T>(string key,T value) | 设置缓存值,如果是对象,会先转换成json字符串 |
Get<T>(string key) | 获取缓存值,返回T类型 |
Delete(string key) | 根据key值删除缓存值 |
Expire(string key,DateTime dateTime) | 设置缓存的到期时间 |
Expire(string key,TimeSpan timeSpan) | 设置缓存的有效时间 |
HashAdd(string key,string hashKey,object hashValue) | 添加Hash的子项 |
HashGet<T>(string key,string hashKey) | 根据key值跟hashKey值获取Hash的子项 |
HashExists(string key,string hashKey) | 根据key值跟hashKey值判断Hash的子项是否存在 |
HashRemove(string key,string hashKey) | 根据key值跟hashKey值删除某个子项 |
SetAdd(string key,params string[] values) | 根据key值添加Set的子项 |
SetRemove(string key, params string[] values) | 根据key值删除Set的子项 |
SetContains(string key,string value) | 根据key值跟value值判断Set缓存内是否存在 |
SetGet(string key) | 根据key值获取整个Set缓存值 |
# 使用方法
用依赖注入的形式,在构造函数中注入ICacheHelper
接口就可以使用