119 lines
3.0 KiB
C#
119 lines
3.0 KiB
C#
|
|
using Microsoft.Extensions.Caching.Memory;
|
|||
|
|
using NPOI.SS.Formula;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
|
|||
|
|
namespace HRM.BO
|
|||
|
|
{
|
|||
|
|
public interface ICacheManager
|
|||
|
|
{
|
|||
|
|
T? Get<T>(enumMemoryCashType type,
|
|||
|
|
enumMemoryCashNature nature,
|
|||
|
|
int? employeeid=null) where T : class;
|
|||
|
|
|
|||
|
|
void Set<T>(T value,
|
|||
|
|
enumMemoryCashType type,
|
|||
|
|
enumMemoryCashNature nature,
|
|||
|
|
int? employeeid=null
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
void Remove(enumMemoryCashType type,
|
|||
|
|
enumMemoryCashNature nature,
|
|||
|
|
int? employeeid=null);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public enum enumMemoryCashType
|
|||
|
|
{
|
|||
|
|
LandingPageEmployee,
|
|||
|
|
Leave,
|
|||
|
|
attendance,
|
|||
|
|
LandingPageActivity,
|
|||
|
|
News,
|
|||
|
|
Holiday,
|
|||
|
|
newJoiner,
|
|||
|
|
LandingPageTeamMembers,
|
|||
|
|
}
|
|||
|
|
public enum enumMemoryCashNature
|
|||
|
|
{
|
|||
|
|
Global,
|
|||
|
|
EmployeeWise,
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public class MemoryCacheService : ICacheManager
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
private readonly IMemoryCache _cache;
|
|||
|
|
public MemoryCacheService(IMemoryCache cache)
|
|||
|
|
{
|
|||
|
|
_cache = cache;
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
private string GetCacheKey(enumMemoryCashType type,
|
|||
|
|
enumMemoryCashNature nature,
|
|||
|
|
int? employeeid = null)
|
|||
|
|
{
|
|||
|
|
if (nature == enumMemoryCashNature.Global)
|
|||
|
|
{
|
|||
|
|
return $"{type}_{nature}";
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
return $"{type}_{nature}_{employeeid}";
|
|||
|
|
}
|
|||
|
|
public T? Get<T>(enumMemoryCashType type,
|
|||
|
|
enumMemoryCashNature nature,
|
|||
|
|
int? employeeid = null) where T : class
|
|||
|
|
{
|
|||
|
|
return _cache.TryGetValue(GetCacheKey(type, nature, employeeid), out T? value)
|
|||
|
|
? value
|
|||
|
|
: null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Set<T>(T value,
|
|||
|
|
enumMemoryCashType type,
|
|||
|
|
enumMemoryCashNature nature,
|
|||
|
|
int? employeeid
|
|||
|
|
|
|||
|
|
)
|
|||
|
|
{
|
|||
|
|
MemoryCacheEntryOptions entryoptions = new MemoryCacheEntryOptions();
|
|||
|
|
entryoptions.SlidingExpiration = TimeSpan.FromMinutes(10);
|
|||
|
|
entryoptions.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(20);
|
|||
|
|
|
|||
|
|
if (nature == enumMemoryCashNature.Global)
|
|||
|
|
{
|
|||
|
|
entryoptions.SlidingExpiration = TimeSpan.FromMinutes(60);
|
|||
|
|
entryoptions.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(60);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
_cache.Set(
|
|||
|
|
GetCacheKey(type, nature, employeeid),
|
|||
|
|
value,
|
|||
|
|
entryoptions);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Remove(enumMemoryCashType type,
|
|||
|
|
enumMemoryCashNature nature,
|
|||
|
|
int? employeeid)
|
|||
|
|
{
|
|||
|
|
if (nature == enumMemoryCashNature.Global)
|
|||
|
|
{
|
|||
|
|
_cache.Remove(GetCacheKey(type, nature));
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
_cache.Remove(GetCacheKey(type, nature, employeeid));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|