forked from smartstore/SmartStoreNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncState.cs
More file actions
174 lines (143 loc) · 3.68 KB
/
Copy pathAsyncState.cs
File metadata and controls
174 lines (143 loc) · 3.68 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.Collections.Generic;
using System.Runtime.Caching;
using System.Threading;
namespace SmartStore.Core.Async
{
public class AsyncState
{
private static readonly AsyncState s_instance = new AsyncState();
private readonly MemoryCache _cache = new MemoryCache("SmartStore.AsyncState");
private AsyncState()
{
}
public static AsyncState Current
{
get { return s_instance; }
}
public bool Exists<T>(string name = null)
{
var key = BuildKey<T>(name);
return _cache.Contains(key) && !object.Equals(((StateInfo)_cache[key]).Progress, default(T));
}
public T Get<T>(string name = null)
{
CancellationTokenSource cancelTokenSource;
return Get<T>(out cancelTokenSource, name);
}
public IEnumerable<T> GetAll<T>()
{
var keyPrefix = BuildKey<T>(null);
foreach (var kvp in _cache)
{
if (kvp.Key.StartsWith(keyPrefix))
{
var value = kvp.Value as StateInfo;
if (value != null && value.Progress != null)
{
yield return (T)value.Progress;
}
}
}
}
public CancellationTokenSource GetCancelTokenSource<T>(string name = null)
{
CancellationTokenSource cancelTokenSource;
Get<T>(out cancelTokenSource, name);
return cancelTokenSource;
}
private T Get<T>(out CancellationTokenSource cancelTokenSource, string name = null)
{
cancelTokenSource = null;
var key = BuildKey<T>(name);
var value = _cache.Get(key) as StateInfo;
if (value != null)
{
cancelTokenSource = value.CancellationTokenSource;
return (T)value.Progress;
}
return default(T);
}
public void Set<T>(T state, string name = null, bool neverExpires = false)
{
Guard.ArgumentNotNull(() => state);
Set(state, null, name, neverExpires);
}
public void Update<T>(Action<T> update, string name = null)
{
Guard.ArgumentNotNull(() => update);
var key = BuildKey(typeof(T), name);
var value = _cache.Get(key) as StateInfo;
if (value != null)
{
var state = (T)value.Progress;
if (state != null)
{
update(state);
}
}
}
public void SetCancelTokenSource<T>(CancellationTokenSource cancelTokenSource, string name = null)
{
Guard.ArgumentNotNull(() => cancelTokenSource);
Set<T>(default(T), cancelTokenSource, name);
}
private void Set<T>(T state, CancellationTokenSource cancelTokenSource, string name = null, bool neverExpires = false)
{
var key = BuildKey(typeof(T), name);
var value = _cache.Get(key) as StateInfo;
if (value != null)
{
// exists already, so update
if (state != null)
{
value.Progress = state;
}
if (cancelTokenSource != null && value.CancellationTokenSource == null)
{
value.CancellationTokenSource = cancelTokenSource;
}
}
var policy = new CacheItemPolicy { SlidingExpiration = neverExpires ? TimeSpan.Zero : TimeSpan.FromMinutes(15) };
_cache.Set(
key,
value ?? new StateInfo { Progress = state, CancellationTokenSource = cancelTokenSource },
policy);
}
public bool Remove<T>(string name = null)
{
var key = BuildKey<T>(name);
var value = _cache.Remove(key) as StateInfo;
if (value != null)
{
if (value.CancellationTokenSource != null)
{
value.CancellationTokenSource.Dispose();
}
return true;
}
return false;
}
private string BuildKey<T>(string name)
{
return BuildKey(typeof(T), name);
}
private string BuildKey(Type type, string name)
{
return "{0}:{1}:{2}".FormatInvariant("AsyncStateKey", type.FullName, name.EmptyNull());
}
class StateInfo
{
public object Progress
{
get;
set;
}
public CancellationTokenSource CancellationTokenSource
{
get;
set;
}
}
}
}