Skip to content

Commit 86b9834

Browse files
committed
new "NoScriptCache" command-flag; fix StackExchange#617
1 parent aa198df commit 86b9834

File tree

4 files changed

+22
-93
lines changed

4 files changed

+22
-93
lines changed

BasicTest/Program.cs

Lines changed: 12 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,35 @@
1-
using System;
2-
using System.Diagnostics;
1+
using StackExchange.Redis;
32
using System.Reflection;
43
using System.Runtime.CompilerServices;
5-
using System.Threading;
6-
using System.Threading.Tasks;
7-
using StackExchange.Redis;
8-
using System.IO;
4+
using System;
95

106
[assembly: AssemblyVersion("1.0.0")]
117

128
namespace BasicTest
139
{
14-
static class YourPreferredSerializer
15-
{
16-
public static T Deserialize<T>(Stream s) { return default(T); }
17-
}
1810
static class Program
1911
{
20-
public static RedisValue JsonGet(this IDatabase db, RedisKey key,
21-
string path = ".", CommandFlags flags = CommandFlags.None)
22-
{
23-
return (RedisValue)db.Execute("JSON.GET",
24-
new object[] { key, path }, flags);
25-
}
26-
27-
public static T JsonGet<T>(this IDatabase db, RedisKey key,
28-
string path = ".", CommandFlags flags = CommandFlags.None)
29-
{
30-
byte[] bytes = (byte[])db.Execute("JSON.GET",
31-
new object[] { key, path }, flags);
32-
using (var ms = new MemoryStream(bytes))
33-
{
34-
return YourPreferredSerializer.Deserialize<T>(ms);
35-
}
36-
}
37-
static void Main(string[] args)
12+
static void Main()
3813
{
3914
using (var conn = ConnectionMultiplexer.Connect("127.0.0.1:6379"))
4015
{
4116
var db = conn.GetDatabase();
4217

43-
// needs explicit RedisKey type for key-based
44-
// sharding to work; will still work with strings,
45-
// but no key-based sharding support
46-
RedisKey key = "some_key";
18+
RedisKey key = Me();
19+
db.KeyDelete(key);
20+
db.StringSet(key, "abc");
4721

48-
// note: if command renames are configured in
49-
// the API, they will still work automatically
50-
db.Execute("del", key);
51-
db.Execute("set", key, "12");
52-
db.Execute("incrby", key, 4);
53-
int i = (int)db.Execute("get", key);
22+
string s = (string)db.ScriptEvaluate(@"
23+
local val = redis.call('get', KEYS[1])
24+
redis.call('del', KEYS[1])
25+
return val", new RedisKey[] { key }, flags: CommandFlags.NoScriptCache);
5426

55-
Console.WriteLine(i); // 16;
27+
Console.WriteLine(s);
28+
Console.WriteLine(db.KeyExists(key));
5629

5730
}
58-
59-
60-
61-
//int AsyncOpsQty = 500000;
62-
//if(args.Length == 1)
63-
//{
64-
// int tmp;
65-
// if(int.TryParse(args[0], out tmp))
66-
// AsyncOpsQty = tmp;
67-
//}
68-
//MassiveBulkOpsAsync(AsyncOpsQty, true, true);
69-
//MassiveBulkOpsAsync(AsyncOpsQty, true, false);
70-
//MassiveBulkOpsAsync(AsyncOpsQty, false, true);
71-
//MassiveBulkOpsAsync(AsyncOpsQty, false, false);
7231
}
73-
static void MassiveBulkOpsAsync(int AsyncOpsQty, bool preserveOrder, bool withContinuation)
74-
{
75-
using (var muxer = ConnectionMultiplexer.Connect("localhost,resolvedns=1"))
76-
{
77-
muxer.PreserveAsyncOrder = preserveOrder;
78-
RedisKey key = "MBOA";
79-
var conn = muxer.GetDatabase();
80-
muxer.Wait(conn.PingAsync());
81-
82-
#if CORE_CLR
83-
int number = 0;
84-
#endif
85-
Action<Task> nonTrivial = delegate
86-
{
87-
#if !CORE_CLR
88-
Thread.SpinWait(5);
89-
#else
90-
for (int i = 0; i < 50; i++)
91-
{
92-
number++;
93-
}
94-
#endif
95-
};
96-
var watch = Stopwatch.StartNew();
97-
for (int i = 0; i <= AsyncOpsQty; i++)
98-
{
99-
var t = conn.StringSetAsync(key, i);
100-
if (withContinuation) t.ContinueWith(nonTrivial);
101-
}
102-
int val = (int)muxer.Wait(conn.StringGetAsync(key));
103-
watch.Stop();
10432

105-
Console.WriteLine("After {0}: {1}", AsyncOpsQty, val);
106-
Console.WriteLine("({3}, {4})\r\n{2}: Time for {0} ops: {1}ms; ops/s: {5}", AsyncOpsQty, watch.ElapsedMilliseconds, Me(),
107-
withContinuation ? "with continuation" : "no continuation", preserveOrder ? "preserve order" : "any order",
108-
AsyncOpsQty / watch.Elapsed.TotalSeconds);
109-
}
110-
}
11133
internal static string Me([CallerMemberName] string caller = null)
11234
{
11335
return caller;

StackExchange.Redis/StackExchange/Redis/CommandFlags.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public enum CommandFlags
5656

5757
// 128: used for "internal call"; never user-specified, so not visible on the public API
5858

59-
// 256: used for "retry"; never user-specified, so not visible on the public API
59+
// 256: used for "script unavailable"; never user-specified, so not visible on the public API
60+
61+
/// <summary>
62+
/// Indicates that script-related operations should use EVAL, not SCRIPT LOAD + EVALSHA
63+
/// </summary>
64+
NoScriptCache = 512,
6065
}
6166
}

StackExchange.Redis/StackExchange/Redis/Message.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ abstract class Message : ICompletable
200200
private const CommandFlags UserSelectableFlags
201201
= CommandFlags.None | CommandFlags.DemandMaster | CommandFlags.DemandSlave
202202
| CommandFlags.PreferMaster | CommandFlags.PreferSlave
203-
| CommandFlags.HighPriority | CommandFlags.FireAndForget | CommandFlags.NoRedirect;
203+
| CommandFlags.HighPriority | CommandFlags.FireAndForget | CommandFlags.NoRedirect | CommandFlags.NoScriptCache;
204204

205205
private CommandFlags flags;
206206
internal CommandFlags FlagsRaw { get { return flags; } set { flags = value; } }

StackExchange.Redis/StackExchange/Redis/RedisDatabase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2530,8 +2530,10 @@ public override int GetHashSlot(ServerSelectionStrategy serverSelectionStrategy)
25302530

25312531
public IEnumerable<Message> GetMessages(PhysicalConnection connection)
25322532
{
2533-
if (script != null && connection.Multiplexer.CommandMap.IsAvailable(RedisCommand.SCRIPT)) // a script was provided (rather than a hash); check it is known and supported
2533+
if (script != null && connection.Multiplexer.CommandMap.IsAvailable(RedisCommand.SCRIPT)
2534+
&& (Flags & CommandFlags.NoScriptCache) == 0)
25342535
{
2536+
// a script was provided (rather than a hash); check it is known and supported
25352537
asciiHash = connection.Bridge.ServerEndPoint.GetScriptHash(script, command);
25362538

25372539
if (asciiHash == null)

0 commit comments

Comments
 (0)