You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
3.0 KiB
C#
121 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Admin.Core.Common.Helper
|
|
{
|
|
public class CacheHelper
|
|
{
|
|
private readonly static ConcurrentDictionary<string, string> _registry = new ConcurrentDictionary<string, string>();
|
|
|
|
#region 注册缓存
|
|
/// <summary>
|
|
/// 注册缓存
|
|
/// </summary>
|
|
/// <param name="definition"></param>
|
|
/// <param name="json"></param>
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
public static void RegisterCache(string definition,string json)
|
|
{
|
|
if (_registry.ContainsKey($"{definition}"))
|
|
{
|
|
throw new InvalidOperationException($"Cache {definition} is already registered");
|
|
}
|
|
|
|
lock (_registry)
|
|
{
|
|
_registry[$"{definition}"] = json;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
|
|
|
|
#region 是否注册
|
|
/// <summary>
|
|
/// 是否注册
|
|
/// </summary>
|
|
/// <param name="IP"></param>
|
|
/// <param name="Date"></param>
|
|
/// <returns></returns>
|
|
public static bool IsRegistered(string definition)
|
|
{
|
|
return _registry.ContainsKey($"{definition}");
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 查询
|
|
/// <summary>
|
|
/// 是否注册
|
|
/// </summary>
|
|
/// <param name="IP"></param>
|
|
/// <param name="Date"></param>
|
|
/// <returns></returns>
|
|
public static string GetDefinition(string definition)
|
|
{
|
|
if (!_registry.ContainsKey($"{definition}"))
|
|
return default;
|
|
return _registry[$"{definition}"];
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 注销
|
|
/// <summary>
|
|
/// 注销
|
|
/// </summary>
|
|
/// <param name="IP"></param>
|
|
/// <param name="date"></param>
|
|
public static void DeregisterCache(string definition)
|
|
{
|
|
try
|
|
{
|
|
if (!_registry.ContainsKey($"{definition}"))
|
|
return;
|
|
|
|
lock (_registry)
|
|
{
|
|
_registry.TryRemove($"{definition}", out var _);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
throw;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 获取所有定义的缓存
|
|
/// <summary>
|
|
/// 获取所有定义的缓存
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static IEnumerable<string> GetAllDefinitions()
|
|
{
|
|
return _registry.Values;
|
|
}
|
|
#endregion
|
|
|
|
#region 清空所有缓存
|
|
/// <summary>
|
|
/// 清空所有缓存
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static bool ClearAllDefinitions()
|
|
{
|
|
try
|
|
{
|
|
_registry.Clear();
|
|
return true;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|