go
This commit is contained in:
60
C4IT.API/ApiCache.cs
Normal file
60
C4IT.API/ApiCache.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Runtime.Caching;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace C4IT.API
|
||||
{
|
||||
class ApiCache
|
||||
{
|
||||
public static object GetValue(string key)
|
||||
{
|
||||
MemoryCache memoryCache = MemoryCache.Default;
|
||||
|
||||
return memoryCache.Get(key);
|
||||
}
|
||||
|
||||
public static object AddOrGet(string key, object value, DateTimeOffset absExpiration)
|
||||
{
|
||||
MemoryCache memoryCache = MemoryCache.Default;
|
||||
lock (memoryCache)
|
||||
{
|
||||
return memoryCache.AddOrGetExisting(key, value, absExpiration);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Add(string key, object value, DateTimeOffset absExpiration)
|
||||
{
|
||||
|
||||
MemoryCache memoryCache = MemoryCache.Default;
|
||||
lock (memoryCache)
|
||||
{
|
||||
return memoryCache.Add(key, value, absExpiration);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void Delete(string key)
|
||||
{
|
||||
MemoryCache memoryCache = MemoryCache.Default;
|
||||
if (memoryCache.Contains(key))
|
||||
{
|
||||
memoryCache.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> GetAllKeysInCache()
|
||||
{
|
||||
MemoryCache memoryCache = MemoryCache.Default;
|
||||
List<String> cacheKeys = new List<String>();
|
||||
var keys= memoryCache.Where(o => true).Select(o => o.Key);
|
||||
|
||||
foreach (var key in keys)
|
||||
{
|
||||
cacheKeys.Add(key);
|
||||
}
|
||||
|
||||
return cacheKeys;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user