61 lines
1.6 KiB
C#
61 lines
1.6 KiB
C#
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;
|
|
}
|
|
}
|
|
}
|