12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.Extensions.Caching;
- using Microsoft.Extensions.Caching.Memory;
- namespace Utils
- {
- /// <summary>
- /// 缓存操作,默认缓存10小时
- /// </summary>
- public class CacheHelper
- {
- private IMemoryCache _cache;
- public CacheHelper(IMemoryCache memoryCache)
- {
- _cache = memoryCache;
- }
- /// <summary>
- /// 读取缓存项
- /// </summary>
- /// <returns></returns>
- public object GetCache(string cacheKey)
- {
- return _cache.Get(cacheKey);
- }
- /// <summary>
- /// 写入缓存项
- /// </summary>
- public void SetCache(string cacheKey, object cacheValue)
- {
- _cache.Set(cacheKey, cacheValue, new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromHours(10)));
- }
- /// <summary>.
- /// 移除指定缓存项
- /// </summary>
- public void DelCache(string cacheName)
- {
- _cache.Remove(cacheName);
- }
- }
- }
|