【無私分享:ASP.NET CORE 項目實戰】目錄索引html
通過 N 久反覆的嘗試,翻閱了網上無數的資料,GitHub上下載了十幾個源碼參考, Memory 和 Redis 終於寫出一個 簡陋 的 封裝,爲了統一和易用,咱們兩種緩存都統一實現了一個接口 ICacheService,微軟也有不少是經過IDistributedCache,你們能夠參考 https://docs.asp.net/en/latest/performance/caching/distributed.html ,GitHub上也有不少很好的封裝,這裏咱們就不一一介紹了,你們自行參考,如今搞 Asp.net Core的仍是不是不少,中文的資料也少的可憐,並且基本都是千篇一概照搬,對於只認識 ABCDEFG的我來講,過程是十分艱辛的,一篇文章每每要看四五遍,逐行逐句翻譯,說多了都是淚,不說了,咱們開始。期間,獲得了不少朋友的幫助,在此表示感謝!數據庫
緩存也好,數據庫也好,咱們就是進行CRUD操做,接口沒什麼好解釋的,註釋我寫的很明白,這裏就列給你們:json
# 驗證緩存項是否存在windows
1 /// <summary> 2 /// 驗證緩存項是否存在 3 /// </summary> 4 /// <param name="key">緩存Key</param> 5 /// <returns></returns> 6 bool Exists(string key); 7 8 /// <summary> 9 /// 驗證緩存項是否存在(異步方式) 10 /// </summary> 11 /// <param name="key">緩存Key</param> 12 /// <returns></returns> 13 Task<bool> ExistsAsync(string key);
# 添加緩存緩存
1 /// <summary> 2 /// 添加緩存 3 /// </summary> 4 /// <param name="key">緩存Key</param> 5 /// <param name="value">緩存Value</param> 6 /// <returns></returns> 7 bool Add(string key, object value); 8 9 /// <summary> 10 /// 添加緩存(異步方式) 11 /// </summary> 12 /// <param name="key">緩存Key</param> 13 /// <param name="value">緩存Value</param> 14 /// <returns></returns> 15 Task<bool> AddAsync(string key, object value); 16 17 /// <summary> 18 /// 添加緩存 19 /// </summary> 20 /// <param name="key">緩存Key</param> 21 /// <param name="value">緩存Value</param> 22 /// <param name="expiresSliding">滑動過時時長(若是在過時時間內有操做,則以當前時間點延長過時時間)</param> 23 /// <param name="expiressAbsoulte">絕對過時時長</param> 24 /// <returns></returns> 25 bool Add(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte); 26 27 /// <summary> 28 /// 添加緩存(異步方式) 29 /// </summary> 30 /// <param name="key">緩存Key</param> 31 /// <param name="value">緩存Value</param> 32 /// <param name="expiresSliding">滑動過時時長(若是在過時時間內有操做,則以當前時間點延長過時時間)</param> 33 /// <param name="expiressAbsoulte">絕對過時時長</param> 34 /// <returns></returns> 35 Task<bool> AddAsync(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte); 36 37 /// <summary> 38 /// 添加緩存 39 /// </summary> 40 /// <param name="key">緩存Key</param> 41 /// <param name="value">緩存Value</param> 42 /// <param name="expiresIn">緩存時長</param> 43 /// <param name="isSliding">是否滑動過時(若是在過時時間內有操做,則以當前時間點延長過時時間)</param> 44 /// <returns></returns> 45 bool Add(string key, object value, TimeSpan expiresIn, bool isSliding = false); 46 47 /// <summary> 48 /// 添加緩存(異步方式) 49 /// </summary> 50 /// <param name="key">緩存Key</param> 51 /// <param name="value">緩存Value</param> 52 /// <param name="expiresIn">緩存時長</param> 53 /// <param name="isSliding">是否滑動過時(若是在過時時間內有操做,則以當前時間點延長過時時間)</param> 54 /// <returns></returns> 55 Task<bool> AddAsync(string key, object value, TimeSpan expiresIn, bool isSliding = false);
# 刪除緩存asp.net
1 /// <summary> 2 /// 刪除緩存 3 /// </summary> 4 /// <param name="key">緩存Key</param> 5 /// <returns></returns> 6 bool Remove(string key); 7 8 /// <summary> 9 /// 刪除緩存(異步方式) 10 /// </summary> 11 /// <param name="key">緩存Key</param> 12 /// <returns></returns> 13 Task<bool> RemoveAsync(string key); 14 15 /// <summary> 16 /// 批量刪除緩存 17 /// </summary> 18 /// <param name="key">緩存Key集合</param> 19 /// <returns></returns> 20 void RemoveAll(IEnumerable<string> keys); 21 22 /// <summary> 23 /// 批量刪除緩存(異步方式) 24 /// </summary> 25 /// <param name="key">緩存Key集合</param> 26 /// <returns></returns> 27 Task RemoveAllAsync(IEnumerable<string> keys);
# 獲取緩存異步
1 /// <summary> 2 /// 獲取緩存 3 /// </summary> 4 /// <param name="key">緩存Key</param> 5 /// <returns></returns> 6 T Get<T>(string key) where T : class; 7 8 /// <summary> 9 /// 獲取緩存(異步方式) 10 /// </summary> 11 /// <param name="key">緩存Key</param> 12 /// <returns></returns> 13 Task<T> GetAsync<T>(string key) where T : class; 14 15 /// <summary> 16 /// 獲取緩存 17 /// </summary> 18 /// <param name="key">緩存Key</param> 19 /// <returns></returns> 20 object Get(string key); 21 22 /// <summary> 23 /// 獲取緩存(異步方式) 24 /// </summary> 25 /// <param name="key">緩存Key</param> 26 /// <returns></returns> 27 Task<object> GetAsync(string key); 28 29 /// <summary> 30 /// 獲取緩存集合 31 /// </summary> 32 /// <param name="keys">緩存Key集合</param> 33 /// <returns></returns> 34 IDictionary<string, object> GetAll(IEnumerable<string> keys); 35 36 /// <summary> 37 /// 獲取緩存集合(異步方式) 38 /// </summary> 39 /// <param name="keys">緩存Key集合</param> 40 /// <returns></returns> 41 Task<IDictionary<string, object>> GetAllAsync(IEnumerable<string> keys);
# 修改緩存 ide
1 /// <summary> 2 /// 修改緩存 3 /// </summary> 4 /// <param name="key">緩存Key</param> 5 /// <param name="value">新的緩存Value</param> 6 /// <returns></returns> 7 bool Replace(string key, object value); 8 9 /// <summary> 10 /// 修改緩存(異步方式) 11 /// </summary> 12 /// <param name="key">緩存Key</param> 13 /// <param name="value">新的緩存Value</param> 14 /// <returns></returns> 15 Task<bool> ReplaceAsync(string key, object value); 16 17 /// <summary> 18 /// 修改緩存 19 /// </summary> 20 /// <param name="key">緩存Key</param> 21 /// <param name="value">新的緩存Value</param> 22 /// <param name="expiresSliding">滑動過時時長(若是在過時時間內有操做,則以當前時間點延長過時時間)</param> 23 /// <param name="expiressAbsoulte">絕對過時時長</param> 24 /// <returns></returns> 25 bool Replace(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte); 26 27 /// <summary> 28 /// 修改緩存(異步方式) 29 /// </summary> 30 /// <param name="key">緩存Key</param> 31 /// <param name="value">新的緩存Value</param> 32 /// <param name="expiresSliding">滑動過時時長(若是在過時時間內有操做,則以當前時間點延長過時時間)</param> 33 /// <param name="expiressAbsoulte">絕對過時時長</param> 34 /// <returns></returns> 35 Task<bool> ReplaceAsync(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte); 36 37 /// <summary> 38 /// 修改緩存 39 /// </summary> 40 /// <param name="key">緩存Key</param> 41 /// <param name="value">新的緩存Value</param> 42 /// <param name="expiresIn">緩存時長</param> 43 /// <param name="isSliding">是否滑動過時(若是在過時時間內有操做,則以當前時間點延長過時時間)</param> 44 /// <returns></returns> 45 bool Replace(string key, object value, TimeSpan expiresIn, bool isSliding = false); 46 47 /// <summary> 48 /// 修改緩存(異步方式) 49 /// </summary> 50 /// <param name="key">緩存Key</param> 51 /// <param name="value">新的緩存Value</param> 52 /// <param name="expiresIn">緩存時長</param> 53 /// <param name="isSliding">是否滑動過時(若是在過時時間內有操做,則以當前時間點延長過時時間)</param> 54 /// <returns></returns> 55 Task<bool> ReplaceAsync(string key, object value, TimeSpan expiresIn, bool isSliding = false);
MemoryCache 這個比較簡單,有微軟比較完善的文檔和一些比較好的文章,這個你們都用很久了。post
咱們新建一個緩存實現類 MemoryCacheService 實現接口 ICacheService學習
public class MemoryCacheService :ICacheService { }
經過構造器注入 IMemoryCache:
protected IMemoryCache _cache;
public MemoryCacheService(IMemoryCache cache)
{
_cache = cache;
}
實現接口的方法,咱們的方法都帶有 異步 和 同步 兩種,咱們節約篇章和時間,異步的就不你們寫了,你們本身添加一下就好。
# 驗證緩存項是否存在
在MemoryCache中,咱們是經過 TryGetValue 來檢測 Key是否存在的
1 /// <summary> 2 /// 驗證緩存項是否存在 3 /// </summary> 4 /// <param name="key">緩存Key</param> 5 /// <returns></returns> 6 public bool Exists(string key) 7 { 8 if (key == null) 9 { 10 throw new ArgumentNullException(nameof(key)); 11 } 12 object cached; 13 return _cache.TryGetValue(key,out cached); 14 }
# 添加緩存
三個添加方法 :
1 /// <summary> 2 /// 添加緩存 3 /// </summary> 4 /// <param name="key">緩存Key</param> 5 /// <param name="value">緩存Value</param> 6 /// <returns></returns> 7 public bool Add(string key, object value) 8 { 9 if (key == null) 10 { 11 throw new ArgumentNullException(nameof(key)); 12 } 13 if (value == null) 14 { 15 throw new ArgumentNullException(nameof(value)); 16 } 17 _cache.Set(key, value); 18 return Exists(key); 19 } 20 /// <summary> 21 /// 添加緩存 22 /// </summary> 23 /// <param name="key">緩存Key</param> 24 /// <param name="value">緩存Value</param> 25 /// <param name="expiresSliding">滑動過時時長(若是在過時時間內有操做,則以當前時間點延長過時時間)</param> 26 /// <param name="expiressAbsoulte">絕對過時時長</param> 27 /// <returns></returns> 28 public bool Add(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte) 29 { 30 if (key == null) 31 { 32 throw new ArgumentNullException(nameof(key)); 33 } 34 if (value == null) 35 { 36 throw new ArgumentNullException(nameof(value)); 37 } 38 _cache.Set(key, value, 39 new MemoryCacheEntryOptions() 40 .SetSlidingExpiration(expiresSliding) 41 .SetAbsoluteExpiration(expiressAbsoulte) 42 ); 43 44 return Exists(key); 45 } 46 /// <summary> 47 /// 添加緩存 48 /// </summary> 49 /// <param name="key">緩存Key</param> 50 /// <param name="value">緩存Value</param> 51 /// <param name="expiresIn">緩存時長</param> 52 /// <param name="isSliding">是否滑動過時(若是在過時時間內有操做,則以當前時間點延長過時時間)</param> 53 /// <returns></returns> 54 public bool Add(string key, object value, TimeSpan expiresIn,bool isSliding = false) 55 { 56 if (key == null) 57 { 58 throw new ArgumentNullException(nameof(key)); 59 } 60 if (value == null) 61 { 62 throw new ArgumentNullException(nameof(value)); 63 } 64 if (isSliding) 65 _cache.Set(key, value, 66 new MemoryCacheEntryOptions() 67 .SetSlidingExpiration(expiresIn) 68 ); 69 else 70 _cache.Set(key, value, 71 new MemoryCacheEntryOptions() 72 .SetAbsoluteExpiration(expiresIn) 73 ); 74 75 return Exists(key); 76 }
# 刪除緩存
單個刪除 和 批量刪除
1 /// <summary> 2 /// 刪除緩存 3 /// </summary> 4 /// <param name="key">緩存Key</param> 5 /// <returns></returns> 6 public bool Remove(string key) 7 { 8 if (key == null) 9 { 10 throw new ArgumentNullException(nameof(key)); 11 } 12 _cache.Remove(key); 13 14 return !Exists(key); 15 } 16 /// <summary> 17 /// 批量刪除緩存 18 /// </summary> 19 /// <param name="key">緩存Key集合</param> 20 /// <returns></returns> 21 public void RemoveAll(IEnumerable<string> keys) 22 { 23 if (keys == null) 24 { 25 throw new ArgumentNullException(nameof(keys)); 26 } 27 28 keys.ToList().ForEach(item => _cache.Remove(item)); 29 }
# 獲取緩存
1 /// <summary> 2 /// 獲取緩存 3 /// </summary> 4 /// <param name="key">緩存Key</param> 5 /// <returns></returns> 6 public T Get<T>(string key) where T : class 7 { 8 if (key == null) 9 { 10 throw new ArgumentNullException(nameof(key)); 11 } 12 return _cache.Get(key) as T; 13 } 14 /// <summary> 15 /// 獲取緩存 16 /// </summary> 17 /// <param name="key">緩存Key</param> 18 /// <returns></returns> 19 public object Get(string key) 20 { 21 if (key == null) 22 { 23 throw new ArgumentNullException(nameof(key)); 24 } 25 return _cache.Get(key); 26 } 27 /// <summary> 28 /// 獲取緩存集合 29 /// </summary> 30 /// <param name="keys">緩存Key集合</param> 31 /// <returns></returns> 32 public IDictionary<string, object> GetAll(IEnumerable<string> keys) 33 { 34 if (keys == null) 35 { 36 throw new ArgumentNullException(nameof(keys)); 37 } 38 39 var dict = new Dictionary<string, object>(); 40 41 keys.ToList().ForEach(item => dict.Add(item, _cache.Get(item))); 42 43 return dict; 44 }
# 修改緩存
1 /// <summary> 2 /// 修改緩存 3 /// </summary> 4 /// <param name="key">緩存Key</param> 5 /// <param name="value">新的緩存Value</param> 6 /// <returns></returns> 7 public bool Replace(string key, object value) 8 { 9 if (key == null) 10 { 11 throw new ArgumentNullException(nameof(key)); 12 } 13 if (value == null) 14 { 15 throw new ArgumentNullException(nameof(value)); 16 } 17 if (Exists(key)) 18 if (!Remove(key)) return false; 19 20 return Add(key, value); 21 22 } 23 /// <summary> 24 /// 修改緩存 25 /// </summary> 26 /// <param name="key">緩存Key</param> 27 /// <param name="value">新的緩存Value</param> 28 /// <param name="expiresSliding">滑動過時時長(若是在過時時間內有操做,則以當前時間點延長過時時間)</param> 29 /// <param name="expiressAbsoulte">絕對過時時長</param> 30 /// <returns></returns> 31 public bool Replace(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte) 32 { 33 if (key == null) 34 { 35 throw new ArgumentNullException(nameof(key)); 36 } 37 if (value == null) 38 { 39 throw new ArgumentNullException(nameof(value)); 40 } 41 if (Exists(key)) 42 if (!Remove(key)) return false; 43 44 return Add(key, value, expiresSliding, expiressAbsoulte); 45 } 46 /// <summary> 47 /// 修改緩存 48 /// </summary> 49 /// <param name="key">緩存Key</param> 50 /// <param name="value">新的緩存Value</param> 51 /// <param name="expiresIn">緩存時長</param> 52 /// <param name="isSliding">是否滑動過時(若是在過時時間內有操做,則以當前時間點延長過時時間)</param> 53 /// <returns></returns> 54 public bool Replace(string key, object value, TimeSpan expiresIn, bool isSliding = false) 55 { 56 if (key == null) 57 { 58 throw new ArgumentNullException(nameof(key)); 59 } 60 if (value == null) 61 { 62 throw new ArgumentNullException(nameof(value)); 63 } 64 if (Exists(key)) 65 if (!Remove(key)) return false; 66 67 return Add(key, value, expiresIn, isSliding); 68 }
最後 釋放:
public void Dispose()
{
if (_cache != null)
_cache.Dispose();
GC.SuppressFinalize(this);
}
Redis通常都是運行的Liunx的,咱們在 【(第十章)】發佈項目到 Linux 上運行 Core 項目 中介紹了,如何在Linux上開發運行Core項目,固然,我相信,不少朋友仍是在 Windows 上作開發、測試的,這裏給你們一個windows 上的Redis和管理軟件,若是沒有或者不知道怎麼找的朋友能夠下載下來,在windows上測試 Redis :鏈接可能失效 百度網盤 提取碼:uglb 百度網盤 提取碼:gl0e
安裝第一個後,啓動Redis服務就好了,管理軟件的界面也很簡潔
咱們先來看咱們的 RedisCacheService:
爲了統一管理和切換使用,咱們的 RedisCacheService 一樣實現接口 ICacheService
public class RedisCacheService : ICacheService { }
一樣,咱們經過構造器注入:
protected IDatabase _cache;
private ConnectionMultiplexer _connection;
private readonly string _instance;
public RedisCacheService(RedisCacheOptions options, int database = 0)
{
_connection = ConnectionMultiplexer.Connect(options.Configuration);
_cache = _connection.GetDatabase(database);
_instance = options.InstanceName;
}
說明一下:咱們須要添加一個Redis包:Microsoft.Extensions.Caching.Redis,這是官方的,可是不知道是我程序的緣由仍是什麼緣由,老是還原失敗,命令強行還原包也不行,因此 我用的是移植的包: Microsoft.Extensions.Caching.Redis.Core,若是大家能夠,最好仍是用官方的包。
這裏咱們寫了個方法,來組合Key值和實例名,就是Key值轉爲 實例名+Key
public string GetKeyForRedis(string key)
{
return _instance + key;
}
# 驗證緩存項是否存在
1 /// <summary> 2 /// 驗證緩存項是否存在 3 /// </summary> 4 /// <param name="key">緩存Key</param> 5 /// <returns></returns> 6 public bool Exists(string key) 7 { 8 if (key == null) 9 { 10 throw new ArgumentNullException(nameof(key)); 11 } 12 return _cache.KeyExists(GetKeyForRedis(key)); 13 }
# 添加緩存
注意:我翻閱了不少資料,沒有找到Redis支持滑動和絕對過時,可是都是繼承的統一接口,因此這裏添加方法 滑動過時時沒有用的。
1 /// <summary> 2 /// 添加緩存 3 /// </summary> 4 /// <param name="key">緩存Key</param> 5 /// <param name="value">緩存Value</param> 6 /// <returns></returns> 7 public bool Add(string key, object value) 8 { 9 if (key == null) 10 { 11 throw new ArgumentNullException(nameof(key)); 12 } 13 return _cache.StringSet(GetKeyForRedis(key), Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value))); 14 } 15 /// <summary> 16 /// 添加緩存 17 /// </summary> 18 /// <param name="key">緩存Key</param> 19 /// <param name="value">緩存Value</param> 20 /// <param name="expiresSliding">滑動過時時長(若是在過時時間內有操做,則以當前時間點延長過時時間,Redis中無效)</param> 21 /// <param name="expiressAbsoulte">絕對過時時長</param> 22 /// <returns></returns> 23 public bool Add(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte) 24 { 25 if (key == null) 26 { 27 throw new ArgumentNullException(nameof(key)); 28 } 29 return _cache.StringSet(GetKeyForRedis(key), Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value)),expiressAbsoulte); 30 } 31 /// <summary> 32 /// 添加緩存 33 /// </summary> 34 /// <param name="key">緩存Key</param> 35 /// <param name="value">緩存Value</param> 36 /// <param name="expiresIn">緩存時長</param> 37 /// <param name="isSliding">是否滑動過時(若是在過時時間內有操做,則以當前時間點延長過時時間,Redis中無效)</param> 38 /// <returns></returns> 39 public bool Add(string key, object value, TimeSpan expiresIn, bool isSliding = false) 40 { 41 if (key == null) 42 { 43 throw new ArgumentNullException(nameof(key)); 44 } 45 46 47 return _cache.StringSet(GetKeyForRedis(key), Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value)), expiresIn); 48 }
# 刪除緩存
1 /// <summary> 2 /// 刪除緩存 3 /// </summary> 4 /// <param name="key">緩存Key</param> 5 /// <returns></returns> 6 public bool Remove(string key) 7 { 8 if (key == null) 9 { 10 throw new ArgumentNullException(nameof(key)); 11 } 12 return _cache.KeyDelete(GetKeyForRedis(key)); 13 } 14 /// <summary> 15 /// 批量刪除緩存 16 /// </summary> 17 /// <param name="key">緩存Key集合</param> 18 /// <returns></returns> 19 public void RemoveAll(IEnumerable<string> keys) 20 { 21 if (keys == null) 22 { 23 throw new ArgumentNullException(nameof(keys)); 24 } 25 26 keys.ToList().ForEach(item => Remove(item)); 27 }
# 獲取緩存
1 /// <summary> 2 /// 獲取緩存 3 /// </summary> 4 /// <param name="key">緩存Key</param> 5 /// <returns></returns> 6 public T Get<T>(string key) where T : class 7 { 8 if (key == null) 9 { 10 throw new ArgumentNullException(nameof(key)); 11 } 12 13 var value = _cache.StringGet(GetKeyForRedis(key)); 14 15 if (!value.HasValue) 16 { 17 return default(T); 18 } 19 20 return JsonConvert.DeserializeObject<T>(value); 21 } 22 /// <summary> 23 /// 獲取緩存 24 /// </summary> 25 /// <param name="key">緩存Key</param> 26 /// <returns></returns> 27 public object Get(string key) 28 { 29 if (key == null) 30 { 31 throw new ArgumentNullException(nameof(key)); 32 } 33 34 var value = _cache.StringGet(GetKeyForRedis(key)); 35 36 if(!value.HasValue) 37 { 38 return null; 39 } 40 /// <summary> 41 /// 獲取緩存集合 42 /// </summary> 43 /// <param name="keys">緩存Key集合</param> 44 /// <returns></returns> 45 public IDictionary<string, object> GetAll(IEnumerable<string> keys) 46 { 47 if (keys == null) 48 { 49 throw new ArgumentNullException(nameof(keys)); 50 } 51 var dict = new Dictionary<string, object>(); 52 53 keys.ToList().ForEach(item => dict.Add(item, Get(GetKeyForRedis(item)))); 54 55 return dict; 56 } 57 58 return JsonConvert.DeserializeObject(value); 59 }
# 修改緩存
1 /// <summary> 2 /// 修改緩存 3 /// </summary> 4 /// <param name="key">緩存Key</param> 5 /// <param name="value">新的緩存Value</param> 6 /// <returns></returns> 7 public bool Replace(string key, object value) 8 { 9 if (key == null) 10 { 11 throw new ArgumentNullException(nameof(key)); 12 } 13 14 if(Exists(key)) 15 if (!Remove(key)) 16 return false; 17 18 return Add(key, value); 19 20 } 21 /// <summary> 22 /// 修改緩存 23 /// </summary> 24 /// <param name="key">緩存Key</param> 25 /// <param name="value">新的緩存Value</param> 26 /// <param name="expiresSliding">滑動過時時長(若是在過時時間內有操做,則以當前時間點延長過時時間)</param> 27 /// <param name="expiressAbsoulte">絕對過時時長</param> 28 /// <returns></returns> 29 public bool Replace(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte) 30 { 31 if (key == null) 32 { 33 throw new ArgumentNullException(nameof(key)); 34 } 35 36 if (Exists(key)) 37 if (!Remove(key)) 38 return false; 39 40 return Add(key, value, expiresSliding, expiressAbsoulte); 41 } 42 /// <summary> 43 /// 修改緩存 44 /// </summary> 45 /// <param name="key">緩存Key</param> 46 /// <param name="value">新的緩存Value</param> 47 /// <param name="expiresIn">緩存時長</param> 48 /// <param name="isSliding">是否滑動過時(若是在過時時間內有操做,則以當前時間點延長過時時間)</param> 49 /// <returns></returns> 50 public bool Replace(string key, object value, TimeSpan expiresIn, bool isSliding = false) 51 { 52 if (key == null) 53 { 54 throw new ArgumentNullException(nameof(key)); 55 } 56 57 if (Exists(key)) 58 if (!Remove(key)) return false; 59 60 return Add(key, value, expiresIn, isSliding); 61 }
一樣,釋放:
public void Dispose()
{
if (_connection != null)
_connection.Dispose();
GC.SuppressFinalize(this);
}
咱們在 【(第八章)】讀取配置文件(二) 讀取自定義配置文件 中講到了如何讀取自定義配置文件,一樣,咱們把緩存的配置 也放在 咱們的自定義配置文件siteconfig.json中:
咱們寫一個類,來獲取配置幾個配置:
CacheProvider: _isUseRedis --- 是否使用Redis
_connectionString --- Redis鏈接
_instanceName ---Redis實例名稱
在 Startup.cs 的 ConfigureServices(IServiceCollection services) 中:
首先添加:services.AddMemoryCache();
判斷是否使用Redis,若是不使用 Redis就默認使用 MemoryCache:
if (_cacheProvider._isUseRedis)
{
//Use Redis
services.AddSingleton(typeof(ICacheService), new RedisCacheService(new RedisCacheOptions
{
Configuration = _cacheProvider._connectionString,
InstanceName = _cacheProvider._instanceName
},0));
}
else
{
//Use MemoryCache
services.AddSingleton<IMemoryCache>(factory =>
{
var cache = new MemoryCache(new MemoryCacheOptions());
return cache;
});
services.AddSingleton<ICacheService, MemoryCacheService>();
}
OK,完成了,由於也是研究Core不到一個月,因此這裏面確定會有些地方寫的很差,但願你們指出來,若是有更高的方案或者方法,也麻煩告知一聲,在此表示感謝!
但願跟你們一塊兒學習Asp.net Core
剛開始接觸,水平有限,不少東西都是本身的理解和翻閱網上大神的資料,若是有不對的地方和不理解的地方,但願你們指正!
雖然Asp.net Core 如今很火熱,可是網上的不少資料都是前篇一概的複製,因此有不少問題我也暫時沒有解決,但願你們能共同幫助一下!
原創文章 轉載請尊重勞動成果 http://yuangang.cnblogs.com