Skip to content

Commit 5345f35

Browse files
committed
Finished RedisAsyncState
1 parent 8f10a24 commit 5345f35

4 files changed

Lines changed: 49 additions & 31 deletions

File tree

src/Libraries/SmartStore.Core/Async/LocalAsyncState.cs

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ public virtual void Set<T>(T state, string name = null, bool neverExpires = fals
6262
// add new entry
6363
var duration = neverExpires ? TimeSpan.Zero : TimeSpan.FromMinutes(15);
6464
var policy = new CacheItemPolicy { SlidingExpiration = duration };
65+
var key = BuildKey<T>(name);
6566

6667
// On expiration or removal: remove corresponding cancel token also.
67-
policy.RemovedCallback = (x) => RemoveCancelTokenSource<T>(name);
68+
policy.RemovedCallback = (x) => OnRemoveCancelTokenSource(key);
6869

69-
_states.Set(BuildKey<T>(name), new AsyncStateInfo { Progress = state, Duration = duration }, policy);
70+
_states.Set(key, new AsyncStateInfo { Progress = state, Duration = duration }, policy);
7071
}
7172
}
7273

@@ -88,19 +89,26 @@ public virtual void Update<T>(Action<T> update, string name = null)
8889

8990
public virtual bool Remove<T>(string name = null)
9091
{
91-
var value = _states.Remove(BuildKey<T>(name));
92+
var key = BuildKey<T>(name);
9293

93-
if (value != null)
94+
if (OnRemoveStateInfo(key))
9495
{
95-
RemoveCancelTokenSource<T>(name);
96+
OnRemoveCancelTokenSource(key);
97+
return true;
9698
}
9799

98100
return false;
99101
}
100102

101-
protected bool RemoveCancelTokenSource<T>(string name = null)
103+
protected virtual bool OnRemoveStateInfo(string key)
102104
{
103-
var key = BuildKey<T>(name);
105+
return _states.Remove(key) != null;
106+
}
107+
108+
protected virtual bool OnRemoveCancelTokenSource(string key, bool successive = false)
109+
{
110+
Guard.NotEmpty(key, nameof(key));
111+
104112
var token = _cancelTokens.Remove(key) as CancellationTokenSource;
105113

106114
if (token != null)
@@ -113,9 +121,15 @@ protected bool RemoveCancelTokenSource<T>(string name = null)
113121
}
114122

115123

116-
public virtual CancellationTokenSource GetCancelTokenSource<T>(string name = null)
124+
public CancellationTokenSource GetCancelTokenSource<T>(string name = null)
117125
{
118-
var key = BuildKey<T>(name);
126+
return OnGetCancelTokenSource(BuildKey<T>(name));
127+
}
128+
129+
protected virtual CancellationTokenSource OnGetCancelTokenSource(string key, bool successive = false)
130+
{
131+
Guard.NotEmpty(key, nameof(key));
132+
119133
var token = _cancelTokens.Get(key);
120134

121135
if (token != null)
@@ -130,18 +144,26 @@ public virtual void SetCancelTokenSource<T>(CancellationTokenSource cancelTokenS
130144
{
131145
Guard.NotNull(cancelTokenSource, nameof(cancelTokenSource));
132146

133-
RemoveCancelTokenSource<T>(name);
147+
var key = BuildKey<T>(name);
134148

135-
var state = GetStateInfo<T>(name);
149+
OnRemoveCancelTokenSource(key);
136150

151+
var state = GetStateInfo<T>(name);
137152
var policy = new CacheItemPolicy { SlidingExpiration = state != null ? state.Duration : TimeSpan.FromMinutes(15) };
138153

139-
_cancelTokens.Set(BuildKey<T>(name), cancelTokenSource, policy);
154+
_cancelTokens.Set(key, cancelTokenSource, policy);
140155
}
141156

142-
public virtual bool Cancel<T>(string name = null)
157+
public bool Cancel<T>(string name = null)
143158
{
144-
var cts = GetCancelTokenSource<T>(name);
159+
return OnCancel(BuildKey<T>(name));
160+
}
161+
162+
protected virtual bool OnCancel(string key, bool successive = false)
163+
{
164+
Guard.NotEmpty(key, nameof(key));
165+
166+
var cts = OnGetCancelTokenSource(key);
145167

146168
if (cts != null)
147169
{
@@ -174,7 +196,7 @@ protected string BuildKey<T>(string name)
174196

175197
protected virtual string BuildKey(Type type, string name)
176198
{
177-
return "{0}:{1}:{2}".FormatInvariant("AsyncStateKey", type.FullName, name.EmptyNull());
199+
return "{0}{1}".FormatInvariant(type.FullName, name.HasValue() ? ":" + name : "");
178200
}
179201
}
180202
}

src/Libraries/SmartStore.Services/Tasks/ITaskScheduler.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
using System;
22
using System.Web;
33
using System.Linq;
4+
using System.Collections.Generic;
45
using SmartStore.Core;
56
using SmartStore.Services.Stores;
6-
using System.Web.Hosting;
7-
using System.Collections.Generic;
8-
using System.Threading;
9-
using SmartStore.Core.Domain.Tasks;
7+
using SmartStore.Core.Async;
108

119
namespace SmartStore.Services.Tasks
1210
{
@@ -32,12 +30,12 @@ public interface ITaskScheduler
3230
bool IsActive { get; }
3331

3432
/// <summary>
35-
/// Gets a <see cref="CancellationTokenSource"/> instance used
36-
/// to signal a task cancellation request.
33+
/// Gets the unique key by which a <see cref="CancellationTokenSource"/> instance
34+
/// can be retrieved from an <see cref="IAsyncState"/> instance
3735
/// </summary>
38-
/// <param name="scheduleTaskId">A <see cref="ScheduleTask"/> identifier</param>
39-
/// <returns>A <see cref="CancellationTokenSource"/> instance if the task is running, <c>null</c> otherwise</returns>
40-
CancellationTokenSource GetCancelTokenSourceFor(int scheduleTaskId);
36+
/// <param name="scheduleTaskId">The schedule task id</param>
37+
/// <returns>A unique string key</returns>
38+
string GetAsyncStateKey(int scheduleTaskId);
4139

4240
/// <summary>
4341
/// Starts/initializes the scheduler

src/Libraries/SmartStore.Services/Tasks/TaskExecutionContext.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Linq;
44
using System.Threading;
55
using Autofac;
6-
using SmartStore.Core.Async;
76
using SmartStore.Core.Data;
87
using SmartStore.Core.Domain.Tasks;
98

@@ -19,9 +18,9 @@ public class TaskExecutionContext
1918

2019
internal TaskExecutionContext(IComponentContext componentContext, ScheduleTask originalTask)
2120
{
22-
this._componentContext = componentContext;
23-
this._originalTask = originalTask;
24-
this.Parameters = new Dictionary<string, string>();
21+
_componentContext = componentContext;
22+
_originalTask = originalTask;
23+
Parameters = new Dictionary<string, string>();
2524
}
2625

2726
public T Resolve<T>(object key = null) where T : class

src/Libraries/SmartStore.Services/Tasks/TaskScheduler.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,9 @@ public bool IsActive
9696
get { return _timer.Enabled; }
9797
}
9898

99-
public CancellationTokenSource GetCancelTokenSourceFor(int scheduleTaskId)
99+
public string GetAsyncStateKey(int scheduleTaskId)
100100
{
101-
var cts = _asyncState.GetCancelTokenSource<ScheduleTask>(scheduleTaskId.ToString());
102-
return cts;
101+
return scheduleTaskId.ToString();
103102
}
104103

105104
private string CreateAuthToken()

0 commit comments

Comments
 (0)