40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using iPackage.Core.Web.Services.Contracts;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
namespace iPackage.Core.Web.Services
|
|
{
|
|
public class CacheService : ICacheService
|
|
{
|
|
private readonly IMemoryCache _memoryCache;
|
|
|
|
public CacheService(IMemoryCache memoryCache)
|
|
{
|
|
_memoryCache = memoryCache;
|
|
}
|
|
public TCache Get<TCache>(string cacheName = null)
|
|
{
|
|
if (string.IsNullOrEmpty(cacheName))
|
|
cacheName = (typeof(TCache).Name);
|
|
var cache = _memoryCache.Get<TCache>(cacheName);
|
|
return cache;
|
|
}
|
|
|
|
public void Set(object cache, string cacheName = null)
|
|
{
|
|
if (string.IsNullOrEmpty(cacheName))
|
|
cacheName = cache.GetType().Name;
|
|
_memoryCache.Set(cacheName, cache);
|
|
}
|
|
|
|
public void Delete<TCache>(string cacheName = null)
|
|
{
|
|
if (string.IsNullOrEmpty(cacheName))
|
|
cacheName = (typeof(TCache)).Name;
|
|
_memoryCache.Remove(cacheName);
|
|
}
|
|
}
|
|
}
|