using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EasyTemplate.Tool.Util;
public class Cache
{
///
///
///
///
///
public static void Set(string key, object value, string lockkey = "")
{
var type = Setting.Get("Cache:CacheType");
if ((type?.ToLower().Contains("redis")).Value)
{
if (!string.IsNullOrWhiteSpace(lockkey))
{
Redis.SetWithLockAsync(lockkey, key, value);
}
else
{
Redis.SetAsync(key, value);
}
}
else
{
MemoryCache.Set(key, value);
}
}
///
///
///
///
///
public static async Task Increase(string key, int value = 1, string lockkey = "")
{
var current = await Get(key);
var type = Setting.Get("Cache:CacheType");
if ((type?.ToLower().Contains("redis")).Value)
{
if (!string.IsNullOrWhiteSpace(lockkey))
{
await Redis.SetWithLockAsync(lockkey, key, current+value);
}
else
{
await Redis.SetAsync(key, current + value);
}
}
else
{
MemoryCache.Set(key, current + value);
}
}
///
///
///
///
///
///
public static async Task Get(string key)
{
var type = Setting.Get("Cache:CacheType");
if ((type?.ToLower().Contains("redis")).Value)
{
return await Redis.GetAsync(key);
}
else
{
return MemoryCache.GetT(key);
}
}
///
///
///
///
///
///
public static async Task Exist(string key)
{
var type = Setting.Get("Cache:CacheType");
if ((type?.ToLower().Contains("redis")).Value)
{
return await Redis.ExistsAsync(key);
}
else
{
return MemoryCache.Exists(key);
}
}
}