using System.Collections.Concurrent; namespace HotelPms.Client.Blazor.Util { /// /// バッファー保存用メモリDB /// public class CacheStorage : IDisposable { public enum Key : int { /// /// マスタ設定画面などViewModel伝送用 /// 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; } } /// /// バッファー保存用メモリDB /// public ConcurrentDictionary Data { get; set; } = new ConcurrentDictionary(); 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; } /// /// マスタデータ /// /// /// public void SetMasterName(Key key, int id, string value) { ConcurrentDictionary dict = Get(key) as ConcurrentDictionary; if (dict == null) { dict = new ConcurrentDictionary(); Set(key, dict); } dict[id] = value; } /// /// 存在しなかったら、nullで返す /// /// /// /// public string GetMasterName(Key key, int id) { ConcurrentDictionary dict = Get(key) as ConcurrentDictionary; if (dict == null) { return string.Empty; } if (!dict.ContainsKey(id)) { return string.Empty; } return dict[id]; } } }