using System.Collections.Concurrent;
|
|
namespace HotelPms.Client.Blazor.Util
|
{
|
/// <summary>
|
/// バッファー保存用メモリDB
|
/// </summary>
|
public class CacheStorage : IDisposable
|
{
|
public enum Key : int
|
{
|
/// <summary>
|
/// マスタ設定画面などViewModel伝送用
|
/// </summary>
|
ViewModel = 0,
|
RoomTypeName,
|
RoomName,
|
}
|
|
|
private static CacheStorage m_Instance;
|
public static CacheStorage Instance
|
{
|
get
|
{
|
if (m_Instance == null) { m_Instance = new CacheStorage(); }
|
return m_Instance;
|
}
|
}
|
|
/// <summary>
|
/// バッファー保存用メモリDB
|
/// </summary>
|
public ConcurrentDictionary<string, object> Data { get; set; } = new ConcurrentDictionary<string, object>();
|
|
public void Dispose()
|
{
|
Data.Clear();
|
}
|
|
public object Get(Key key)
|
{
|
return Get(key.ToString());
|
}
|
|
public object Get(string key)
|
{
|
try
|
{
|
Data.TryGetValue(key, out object value);
|
return value;
|
}
|
catch
|
{
|
return null;
|
}
|
}
|
|
public void Set(Key key, object value)
|
{
|
Set(key.ToString(), value);
|
}
|
|
public void Set(string key, object value)
|
{
|
Data[key] = value;
|
}
|
|
/// <summary>
|
/// マスタデータ
|
/// </summary>
|
/// <param name="id"></param>
|
/// <param name="value"></param>
|
public void SetMasterName(Key key, int id, string value)
|
{
|
ConcurrentDictionary<int, string> dict = Get(key) as ConcurrentDictionary<int, string>;
|
if (dict == null)
|
{
|
dict = new ConcurrentDictionary<int, string>();
|
Set(key, dict);
|
}
|
dict[id] = value;
|
}
|
|
/// <summary>
|
/// 存在しなかったら、nullで返す
|
/// </summary>
|
/// <param name="key"></param>
|
/// <param name="id"></param>
|
/// <returns></returns>
|
public string GetMasterName(Key key, int id)
|
{
|
ConcurrentDictionary<int, string> dict = Get(key) as ConcurrentDictionary<int, string>;
|
if (dict == null) { return string.Empty; }
|
if (!dict.ContainsKey(id)) { return string.Empty; }
|
return dict[id];
|
}
|
}
|
}
|