|
1 | | -using System; |
2 | | -using System.Diagnostics; |
| 1 | +using StackExchange.Redis; |
3 | 2 | using System.Reflection; |
4 | 3 | using System.Runtime.CompilerServices; |
5 | | -using System.Threading; |
6 | | -using System.Threading.Tasks; |
7 | | -using StackExchange.Redis; |
8 | | -using System.IO; |
| 4 | +using System; |
9 | 5 |
|
10 | 6 | [assembly: AssemblyVersion("1.0.0")] |
11 | 7 |
|
12 | 8 | namespace BasicTest |
13 | 9 | { |
14 | | - static class YourPreferredSerializer |
15 | | - { |
16 | | - public static T Deserialize<T>(Stream s) { return default(T); } |
17 | | - } |
18 | 10 | static class Program |
19 | 11 | { |
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() |
38 | 13 | { |
39 | 14 | using (var conn = ConnectionMultiplexer.Connect("127.0.0.1:6379")) |
40 | 15 | { |
41 | 16 | var db = conn.GetDatabase(); |
42 | 17 |
|
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"); |
47 | 21 |
|
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); |
54 | 26 |
|
55 | | - Console.WriteLine(i); // 16; |
| 27 | + Console.WriteLine(s); |
| 28 | + Console.WriteLine(db.KeyExists(key)); |
56 | 29 |
|
57 | 30 | } |
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); |
72 | 31 | } |
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(); |
104 | 32 |
|
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 | | - } |
111 | 33 | internal static string Me([CallerMemberName] string caller = null) |
112 | 34 | { |
113 | 35 | return caller; |
|
0 commit comments