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

Commit a73eb60

Browse files
committed
Add support for useRedis to change default redis connection on page
1 parent 50d1921 commit a73eb60

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/ServiceStack.Redis/RedisScripts.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class TemplateRedisFilters : RedisScripts {}
2525

2626
public class RedisScripts : ScriptMethods
2727
{
28+
private const string RedisConnection = "__redisConnection";
29+
2830
private IRedisClientsManager redisManager;
2931
public IRedisClientsManager RedisManager
3032
{
@@ -36,6 +38,15 @@ T exec<T>(Func<IRedisClient, T> fn, ScriptScopeContext scope, object options)
3638
{
3739
try
3840
{
41+
if ((options is Dictionary<string, object> obj && obj.TryGetValue("connectionString", out var oRedisConn))
42+
|| scope.PageResult.Args.TryGetValue(RedisConnection, out oRedisConn))
43+
{
44+
using (var redis = new RedisClient((string)oRedisConn))
45+
{
46+
return fn(redis);
47+
}
48+
}
49+
3950
using (var redis = RedisManager.GetClient())
4051
{
4152
return fn(redis);
@@ -47,6 +58,12 @@ T exec<T>(Func<IRedisClient, T> fn, ScriptScopeContext scope, object options)
4758
}
4859
}
4960

61+
public IgnoreResult useRedis(ScriptScopeContext scope, string redisConnection)
62+
{
63+
scope.PageResult.Args[RedisConnection] = redisConnection;
64+
return IgnoreResult.Value;
65+
}
66+
5067
static readonly Dictionary<string, int> cmdArgCounts = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase) {
5168
{ "SET", 3 }
5269
};

0 commit comments

Comments
 (0)