using System; using Microsoft.Extensions.Options; using Microsoft.Extensions.Caching.Memory; namespace Abp.Runtime.Caching.Memory { /// /// Implements to work with . /// public class AbpMemoryCache : CacheBase { private MemoryCache _memoryCache; /// /// Constructor. /// /// Unique name of the cache public AbpMemoryCache(string name) : base(name) { _memoryCache = new MemoryCache(new OptionsWrapper(new MemoryCacheOptions())); } public override object GetOrDefault(string key) { return _memoryCache.Get(key); } public override void Set(string key, object value, TimeSpan? slidingExpireTime = null, TimeSpan? absoluteExpireTime = null) { if (value == null) { throw new AbpException("Can not insert null values to the cache!"); } if (absoluteExpireTime != null) { _memoryCache.Set(key, value, DateTimeOffset.Now.Add(absoluteExpireTime.Value)); } else if (slidingExpireTime != null) { _memoryCache.Set(key, value, slidingExpireTime.Value); } else if (DefaultAbsoluteExpireTime != null) { _memoryCache.Set(key, value, DateTimeOffset.Now.Add(DefaultAbsoluteExpireTime.Value)); } else { _memoryCache.Set(key, value, DefaultSlidingExpireTime); } } public override void Remove(string key) { _memoryCache.Remove(key); } public override void Clear() { _memoryCache.Dispose(); _memoryCache = new MemoryCache(new OptionsWrapper(new MemoryCacheOptions())); } public override void Dispose() { _memoryCache.Dispose(); base.Dispose(); } } }