好像是沒有 nuget 包的直接using即可,
using Microsoft.Extensions.Caching.Memory;
1 public static class CacheHelperNetCore 2 { 3 public static IMemoryCache _memoryCache = new MemoryCache(new MemoryCacheOptions()); 4 /// <summary> 5 /// 設定key值快取 6 /// </summary> 7 /// <param name="key"></param> 8 /// <param name="obj">值</param> 9 /// <param name="timeSpan">過期時間</param> 10 public static void SetCache(string key,object obj,TimeSpan timeSpan) 11 { 12 _memoryCache.Set(key,obj,timeSpan); 13 } 14 /// <summary> 15 /// 獲取key值快取 16 /// </summary> 17 /// <param name="key"></param> 18 /// <returns></returns> 19 public static object GetCache(string key) 20 { 21 return _memoryCache.Get(key); 22 } 23 /// <summary> 24 /// 獲取key值快取 25 /// </summary> 26 /// <typeparam name="T"></typeparam> 27 /// <param name="key"></param> 28 /// <returns></returns> 29 public static T GetCache<T>(string key) 30 { 31 return _memoryCache.Get<T>(key); 32 } 33 /// <summary> 34 /// 該key值快取是否存在 35 /// </summary> 36 /// <param name="key"></param> 37 /// <returns></returns> 38 public static bool Exist(string key) 39 { 40 return _memoryCache.TryGetValue(key,out _); 41 } 42 }
基本都是常用的,提升下性能還是可以的,不過有持久化需求或者對資料結構和處理有高級要求的建議選擇Redis,
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/20528.html
標籤:.NET Core
上一篇:Elasticsearch 入門(三)使用Elasticsearch.Net & NEST作為elasticsearch客戶端
