ホテル管理システム
ogi
yesterday 1a1c8e71fcd14858f595029f089b2d4a00202b32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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];
        }
    }
}