-
Notifications
You must be signed in to change notification settings - Fork 2
/
GlobalVar.cs
138 lines (120 loc) · 3.64 KB
/
GlobalVar.cs
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using Nancy.Bootstrapper;
using NantCom.NancyBlack.Configuration;
using NantCom.NancyBlack.Modules.DatabaseSystem;
using NantCom.NancyBlack.Modules.DatabaseSystem.Types;
using System;
using System.Collections.Generic;
using System.Linq;
namespace NantCom.NancyBlack
{
public class GlobalVarEntry : IStaticType
{
public int Id { get; set; }
public DateTime __createdAt { get; set; }
public DateTime __updatedAt { get; set; }
/// <summary>
/// Key
/// </summary>
public string Key { get; set; }
/// <summary>
/// Value
/// </summary>
public string Value { get; set; }
}
/// <summary>
/// A persistent variables that will keep in database
/// </summary>
public class GlobalVar : IPipelineHook
{
private static Dictionary<string, GlobalVarEntry> _Variables = new Dictionary<string, GlobalVarEntry>();
private static HashSet<string> _DirtyList = new HashSet<string>();
/// <summary>
/// Gets/Set variable
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string this[string key]
{
get
{
GlobalVarEntry gv;
if (_Variables.TryGetValue(key, out gv))
{
return gv.Value;
}
gv = new GlobalVarEntry() { Key = key, Value = null };
_Variables[key] = gv;
return null;
}
set
{
GlobalVarEntry gv;
if (_Variables.TryGetValue(key, out gv))
{
gv.Value = value;
}
else
{
_Variables[key] = new GlobalVarEntry() { Key = key, Value = value };
}
_DirtyList.Add(key);
}
}
private GlobalVar() { }
/// <summary>
/// Refreshes current global variable from database
/// </summary>
/// <param name="db"></param>
public void Load(NancyBlackDatabase db)
{
lock ("GVLoad")
{
// persist current values first
this.Persist(db);
var list = db.Query<GlobalVarEntry>().OrderBy( gv => gv.Id ).ToList();
var result = new Dictionary<string, GlobalVarEntry>();
foreach (var item in list)
{
result[item.Key] = item;
}
_Variables = result;
}
}
/// <summary>
/// Store value to database, only dirty values are stored
/// </summary>
/// <param name="db"></param>
public void Persist(NancyBlackDatabase db)
{
lock (this)
{
db.Transaction(() =>
{
foreach (var key in _DirtyList)
{
db.UpsertRecord(_Variables[key]);
}
});
_DirtyList = new HashSet<string>();
}
}
public void Hook(IPipelines p)
{
p.AfterRequest.AddItemToEndOfPipeline((ctx) =>
{
this.Persist(ctx.GetSiteDatabase());
});
}
private static GlobalVar _Default = new GlobalVar();
/// <summary>
/// Gets Default instance of Global Variable
/// </summary>
public static GlobalVar Default
{
get
{
return _Default;
}
}
}
}