Skip to content

Commit 116e8f3

Browse files
committed
Switch to StackExchange.Redis
1 parent e81f196 commit 116e8f3

File tree

6 files changed

+57
-39
lines changed

6 files changed

+57
-39
lines changed

docs-src/configuration.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Configuration is accomplished by modifying the appsettings.json file that comes
77
{
88
"InEngine": {
99
"Queue": {
10+
"PrimaryQueueConsumers": 16,
11+
"SecondaryQueueConsumers": -1,
1012
"QueueName": "InEngine:Queue",
1113
"RedisHost": "localhost",
1214
"RedisPort": 6379,
@@ -15,6 +17,7 @@ Configuration is accomplished by modifying the appsettings.json file that comes
1517
}
1618
}
1719
}
20+
1821
```
1922

2023
The -c, --configuration argument can also be used to specify an alternate configuration file.

src/InEngine.Core/InEngine.Core.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@
5858
<Reference Include="CommandLine">
5959
<HintPath>..\packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll</HintPath>
6060
</Reference>
61-
<Reference Include="RedisBoost">
62-
<HintPath>..\packages\RedisBoost.1.6.4\lib\net40\RedisBoost.dll</HintPath>
63-
</Reference>
6461
<Reference Include="System.Configuration" />
6562
<Reference Include="Common.Logging.Core">
6663
<HintPath>..\packages\Common.Logging.Core.3.4.1\lib\net40\Common.Logging.Core.dll</HintPath>
@@ -102,6 +99,9 @@
10299
<HintPath>..\packages\System.IO.Compression.4.3.0\lib\net46\System.IO.Compression.dll</HintPath>
103100
</Reference>
104101
<Reference Include="mscorlib" />
102+
<Reference Include="StackExchange.Redis">
103+
<HintPath>..\packages\StackExchange.Redis.1.2.4\lib\net46\StackExchange.Redis.dll</HintPath>
104+
</Reference>
105105
</ItemGroup>
106106
<ItemGroup>
107107
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -135,6 +135,7 @@
135135
<Compile Include="Commands\Options.cs" />
136136
<Compile Include="IO\Write.cs" />
137137
<Compile Include="IO\IWrite.cs" />
138+
<Compile Include="JsonExtensions.cs" />
138139
</ItemGroup>
139140
<ItemGroup>
140141
<None Include="packages.config" />
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Newtonsoft.Json;
2+
3+
namespace InEngine.Core
4+
{
5+
public static class JsonExtensions
6+
{
7+
public static string SerializeToJson<T>(this T message) where T : class
8+
{
9+
return JsonConvert.SerializeObject(message);
10+
}
11+
12+
public static T DeserializeFromJson<T>(this string payload)
13+
{
14+
return JsonConvert.DeserializeObject<T>(payload);
15+
}
16+
}
17+
}

src/InEngine.Core/Queue/Broker.cs

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Threading.Tasks;
44
using InEngine.Core.Exceptions;
55
using Newtonsoft.Json;
6-
using RedisBoost;
6+
using StackExchange.Redis;
77

88
namespace InEngine.Core.Queue
99
{
@@ -15,16 +15,16 @@ public class Broker
1515
public string SecondaryWaitingQueueName { get { return QueueBaseName + ":SecondaryWaiting"; } }
1616
public string SecondaryProcessingQueueName { get { return QueueBaseName + ":SecondaryProcessing"; } }
1717

18-
public IRedisClient _redis;
19-
public IRedisClient Redis
18+
public ConnectionMultiplexer _redis;
19+
public ConnectionMultiplexer Redis
2020
{
2121
get
2222
{
2323
if (_redis == null)
2424
{
25-
var connectionTask = RedisClient.ConnectAsync(RedisHost, RedisPort, RedisDb);
26-
connectionTask.Wait();
27-
_redis = connectionTask.Result;
25+
var redisConfig = ConfigurationOptions.Parse($"{RedisHost}:{RedisPort}");
26+
redisConfig.Password = string.IsNullOrWhiteSpace(RedisPassword) ? null : RedisPassword;
27+
_redis = ConnectionMultiplexer.Connect(redisConfig);
2828
}
2929
return _redis;
3030
}
@@ -49,22 +49,26 @@ public static Broker Make()
4949

5050
public void Publish(ICommand command, bool useSecondaryQueue = false)
5151
{
52-
Redis.LPushAsync(useSecondaryQueue ? SecondaryWaitingQueueName : PrimaryWaitingQueueName,
53-
new Message()
54-
{
52+
Redis.GetDatabase(RedisDb).ListLeftPush(
53+
useSecondaryQueue ? SecondaryWaitingQueueName : PrimaryWaitingQueueName,
54+
new Message() {
5555
CommandClassName = command.GetType().FullName,
5656
CommandAssemblyName = command.GetType().Assembly.GetName().Name + ".dll",
5757
SerializedCommand = JsonConvert.SerializeObject(command)
58-
});
58+
}.SerializeToJson()
59+
);
5960
}
6061

6162
public bool Consume(bool useSecondaryQueue = false)
6263
{
6364
var waitingQueueName = useSecondaryQueue ? SecondaryWaitingQueueName : PrimaryWaitingQueueName;
6465
var processingQueueName = useSecondaryQueue ? SecondaryProcessingQueueName : PrimaryProcessingQueueName;
65-
var stageMessageTask = Redis.RPopLPushAsync(waitingQueueName, processingQueueName);
66-
stageMessageTask.Wait();
67-
var message = stageMessageTask.Result.As<Message>();
66+
67+
var stageMessageTask = Redis.GetDatabase(RedisDb).ListRightPopLeftPush(waitingQueueName, processingQueueName);
68+
var serializedMessage = stageMessageTask.ToString();
69+
if (serializedMessage == null)
70+
return false;
71+
var message = serializedMessage.DeserializeFromJson<Message>();
6872
if (message == null)
6973
return false;
7074

@@ -76,7 +80,7 @@ public bool Consume(bool useSecondaryQueue = false)
7680
try
7781
{
7882
commandInstance.Run();
79-
Redis.LRemAsync(processingQueueName, 1, message).Wait();
83+
Redis.GetDatabase(RedisDb).ListRemove(processingQueueName, serializedMessage, 1);
8084
}
8185
catch (Exception exception)
8286
{
@@ -88,52 +92,45 @@ public bool Consume(bool useSecondaryQueue = false)
8892
#region Primary Queue Management Methods
8993
public long GetPrimaryWaitingQueueLength()
9094
{
91-
return WaitAndReturnResult(Redis.LLenAsync(PrimaryWaitingQueueName));
95+
return Redis.GetDatabase(RedisDb).ListLength(PrimaryWaitingQueueName);
9296
}
9397

9498
public long GetPrimaryProcessingQueueLength()
9599
{
96-
return WaitAndReturnResult(Redis.LLenAsync(PrimaryProcessingQueueName));
100+
return Redis.GetDatabase(RedisDb).ListLength(PrimaryProcessingQueueName);
97101
}
98102

99-
public long ClearPrimaryWaitingQueue()
103+
public bool ClearPrimaryWaitingQueue()
100104
{
101-
return WaitAndReturnResult(Redis.DelAsync(SecondaryWaitingQueueName));
105+
return Redis.GetDatabase(RedisDb).KeyDelete(PrimaryWaitingQueueName);
102106
}
103107

104-
public long ClearPrimaryProcessingQueue()
108+
public bool ClearPrimaryProcessingQueue()
105109
{
106-
return WaitAndReturnResult(Redis.DelAsync(SecondaryProcessingQueueName));
110+
return Redis.GetDatabase(RedisDb).KeyDelete(PrimaryProcessingQueueName);
107111
}
108112
#endregion
109113

110114
#region Secondary Queue Management Methods
111115
public long GetSecondaryWaitingQueueLength()
112116
{
113-
return WaitAndReturnResult(Redis.LLenAsync(PrimaryWaitingQueueName));
117+
return Redis.GetDatabase(RedisDb).ListLength(SecondaryWaitingQueueName);
114118
}
115119

116120
public long GetSecondaryProcessingQueueLength()
117121
{
118-
return WaitAndReturnResult(Redis.LLenAsync(PrimaryProcessingQueueName));
122+
return Redis.GetDatabase(RedisDb).ListLength(SecondaryProcessingQueueName);
119123
}
120124

121-
public long ClearSecondaryWaitingQueue()
125+
public bool ClearSecondaryWaitingQueue()
122126
{
123-
return WaitAndReturnResult(Redis.DelAsync(SecondaryWaitingQueueName));
127+
return Redis.GetDatabase(RedisDb).KeyDelete(SecondaryWaitingQueueName);
124128
}
125129

126-
public long ClearSecondaryProcessingQueue()
130+
public bool ClearSecondaryProcessingQueue()
127131
{
128-
return WaitAndReturnResult(Redis.DelAsync(SecondaryProcessingQueueName));
132+
return Redis.GetDatabase(RedisDb).KeyDelete(SecondaryProcessingQueueName);
129133
}
130134
#endregion
131-
132-
133-
public long WaitAndReturnResult(Task<long> task)
134-
{
135-
task.Wait();
136-
return task.Result;
137-
}
138135
}
139136
}

src/InEngine.Core/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net462" />
1919
<package id="NLog" version="4.4.12" targetFramework="net462" />
2020
<package id="Quartz" version="2.6.1" targetFramework="net462" />
21-
<package id="RedisBoost" version="1.6.4" targetFramework="net462" />
21+
<package id="StackExchange.Redis" version="1.2.4" targetFramework="net462" />
2222
<package id="System.IO.Compression" version="4.3.0" targetFramework="net462" />
2323
<package id="System.Runtime.CompilerServices.Unsafe" version="4.4.0" targetFramework="net462" />
2424
</packages>

src/InEngine/appsettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"InEngine": {
33
"Queue": {
4-
"PrimaryQueueConsumers": 16,
5-
"SecondaryQueueConsumers": -1,
4+
"PrimaryQueueConsumers": 4,
5+
"SecondaryQueueConsumers": 4,
66
"QueueName": "InEngine:Queue",
77
"RedisHost": "localhost",
88
"RedisPort": 6379,

0 commit comments

Comments
 (0)