forked from yavordimitrov/SmartStoreNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncState.cs
More file actions
137 lines (110 loc) · 3.03 KB
/
Copy pathAsyncState.cs
File metadata and controls
137 lines (110 loc) · 3.03 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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text;
using System.Threading;
namespace SmartStore.Core.Async
{
public class AsyncState
{
private readonly static AsyncState s_instance = new AsyncState();
private readonly MemoryCache _cache = MemoryCache.Default;
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 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)
{
this.Set(state, null, name, neverExpires);
}
public void SetCancelTokenSource<T>(CancellationTokenSource cancelTokenSource, string name = null)
{
Guard.ArgumentNotNull(() => cancelTokenSource);
this.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
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;
}
}
}
}