Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit de8ce00

Browse files
committed
Add new redisConnection filters
1 parent 57a0daf commit de8ce00

File tree

3 files changed

+106
-6
lines changed

3 files changed

+106
-6
lines changed

build/build.bat

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
SET MSBUILD="C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe"
22

3-
%MSBUILD% build-core.proj /target:TeamCityBuild;NuGetPack /property:Configuration=Release;PatchVersion=41
4-
%MSBUILD% build.proj /target:TeamCityBuild;NuGetPack /property:Configuration=Release;PatchVersion=9
3+
REM %MSBUILD% build-core.proj /target:TeamCityBuild;NuGetPack /property:Configuration=Release;PatchVersion=41
4+
REM %MSBUILD% build.proj /target:TeamCityBuild;NuGetPack /property:Configuration=Release;PatchVersion=9
55
REM %MSBUILD% build-sn.proj /target:NuGetPack /property:Configuration=Signed;PatchVersion=9
6+
7+
msbuild /p:Configuration=Release ..\src\ServiceStack.Redis.sln

src/ServiceStack.Redis/TemplateRedisFilters.cs

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,20 @@ public class RedisSearchResult
2323

2424
public class TemplateRedisFilters : TemplateFilter
2525
{
26-
public IRedisClientsManager RedisManager { get; set; }
27-
public IAppSettings AppSettings { get; set; }
26+
private IRedisClientsManager redisManager;
27+
public IRedisClientsManager RedisManager
28+
{
29+
get => redisManager ?? (redisManager = Context.Container.Resolve<IRedisClientsManager>());
30+
set => redisManager = value;
31+
}
2832

2933
T exec<T>(Func<IRedisClient, T> fn, TemplateScopeContext scope, object options)
3034
{
3135
try
3236
{
33-
using (var db = RedisManager.GetClient())
37+
using (var redis = RedisManager.GetClient())
3438
{
35-
return fn(db);
39+
return fn(redis);
3640
}
3741
}
3842
catch (Exception ex)
@@ -134,6 +138,54 @@ public List<RedisSearchResult> redisSearchKeys(TemplateScopeContext scope, strin
134138
return searchResults.Results;
135139
}
136140

141+
public string redisConnectionString(TemplateScopeContext scope) => exec(r => $"{r.Host}:{r.Port}?db={r.Db}", scope, null);
142+
143+
public string redisToConnectionString(TemplateScopeContext scope, object connectionInfo) => redisToConnectionString(scope, connectionInfo, null);
144+
public string redisToConnectionString(TemplateScopeContext scope, object connectionInfo, object options)
145+
{
146+
var connectionString = connectionInfo as string;
147+
if (connectionString != null)
148+
return connectionString;
149+
150+
if (connectionInfo is IDictionary<string, object> d)
151+
{
152+
var host = (d.TryGetValue("host", out object h) ? h as string : null) ?? "localhost";
153+
var port = d.TryGetValue("port", out object p) ? DynamicInt.Instance.ConvertFrom(p) : 6379;
154+
var db = d.TryGetValue("db", out object oDb) ? DynamicInt.Instance.ConvertFrom(oDb) : 0;
155+
156+
connectionString = $"{host}:{port}?db={db}";
157+
158+
if (d.TryGetValue("password", out object password))
159+
connectionString += "&password=" + password.ToString().UrlEncode();
160+
}
161+
162+
return connectionString;
163+
}
164+
165+
public string redisChangeConnection(TemplateScopeContext scope, object newConnection) => redisChangeConnection(scope, newConnection, null);
166+
public string redisChangeConnection(TemplateScopeContext scope, object newConnection, object options)
167+
{
168+
try
169+
{
170+
var connectionString = redisToConnectionString(scope, newConnection, options);
171+
if (connectionString == null)
172+
throw new NotSupportedException(nameof(redisChangeConnection) + " expects a String or an ObjectDictionary but received: " + (newConnection?.GetType().Name ?? "null"));
173+
174+
using (var testConnection = new RedisClient(connectionString))
175+
{
176+
testConnection.Ping();
177+
}
178+
179+
((IRedisFailover)RedisManager).FailoverTo(connectionString);
180+
181+
return connectionString;
182+
}
183+
catch (Exception ex)
184+
{
185+
throw new StopFilterExecutionException(scope, options ?? newConnection as IDictionary<string,object>, ex);
186+
}
187+
}
188+
137189
public string redisSearchKeysAsJson(TemplateScopeContext scope, string query, object options)
138190
{
139191
if (string.IsNullOrEmpty(query))
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using NUnit.Framework;
2+
using ServiceStack.Templates;
3+
4+
namespace ServiceStack.Redis.Tests
5+
{
6+
class RedisTemplateTests
7+
{
8+
[Test]
9+
public void Does_build_connection_string()
10+
{
11+
var context = new TemplateContext
12+
{
13+
TemplateFilters = { new TemplateRedisFilters() }
14+
};
15+
context.Container.AddSingleton<IRedisClientsManager>(() => new RedisManagerPool());
16+
context.Init();
17+
18+
Assert.That(context.EvaluateTemplate("{{ redisToConnectionString: host:7000?db=1 }}"),
19+
Is.EqualTo("host:7000?db=1"));
20+
21+
Assert.That(context.EvaluateTemplate("{{ { host: 'host' } | redisToConnectionString }}"),
22+
Is.EqualTo("host:6379?db=0"));
23+
24+
Assert.That(context.EvaluateTemplate("{{ { port: 7000 } | redisToConnectionString }}"),
25+
Is.EqualTo("localhost:7000?db=0"));
26+
27+
Assert.That(context.EvaluateTemplate("{{ { db: 1 } | redisToConnectionString }}"),
28+
Is.EqualTo("localhost:6379?db=1"));
29+
30+
Assert.That(context.EvaluateTemplate("{{ { host: 'host', port: 7000, db: 1 } | redisToConnectionString }}"),
31+
Is.EqualTo("host:7000?db=1"));
32+
33+
Assert.That(context.EvaluateTemplate("{{ { host: 'host', port: 7000, db: 1, password:'secret' } | redisToConnectionString | raw }}"),
34+
Is.EqualTo("host:7000?db=1&password=secret"));
35+
36+
Assert.That(context.EvaluateTemplate("{{ redisConnectionString }}"),
37+
Is.EqualTo("localhost:6379?db=0"));
38+
39+
Assert.That(context.EvaluateTemplate("{{ { db: 1 } | redisChangeConnection }}"),
40+
Is.EqualTo("localhost:6379?db=1"));
41+
42+
Assert.That(context.EvaluateTemplate("{{ redisConnectionString }}"),
43+
Is.EqualTo("localhost:6379?db=1"));
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)