garnet
283 строки · 9.4 Кб
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT license.
3
4using System;5using System.Net.Security;6using System.Security.Cryptography.X509Certificates;7using System.Text;8using System.Threading;9using System.Threading.Tasks;10using Garnet.client;11
12namespace GarnetClientSample13{
14/// <summary>15/// Use Garnet with GarnetClient as client library16/// </summary>17public class GarnetClientSamples18{19readonly string address;20readonly int port;21readonly bool useTLS;22
23public GarnetClientSamples(string address, int port, bool useTLS)24{25this.address = address;26this.port = port;27this.useTLS = useTLS;28}29
30public async Task RunAll()31{32await PingAsync();33await SetGetAsync();34SetGetSync();35await IncrAsync();36await IncrByAsync(99);37await DecrByAsync(99);38await DecrAsync("test", 5);39await IncrNoKeyAsync();40await ExistsAsync();41await DeleteAsync();42await SetGetMemoryAsync();43}44
45async Task PingAsync()46{47using var db = new GarnetClient(address, port, GetSslOpts());48await db.ConnectAsync();49var pong = await db.PingAsync();50if (pong != "PONG")51throw new Exception("PingAsync: Error");52Console.WriteLine("Ping: Success");53}54
55async Task SetGetAsync()56{57using var db = new GarnetClient(address, port, GetSslOpts());58await db.ConnectAsync();59
60string origValue = "abcdefg";61await db.StringSetAsync("mykey", origValue);62
63string retValue = await db.StringGetAsync("mykey");64
65if (origValue != retValue)66throw new Exception("SetGetAsync: Error");67Console.WriteLine("SetGetAsync: Success");68}69
70void SetGetSync()71{72using var db = new GarnetClient(address, port, GetSslOpts());73db.Connect();74
75string origValue = "abcdefg";76db.StringSet("mykey", origValue, (c, s) => { if (s != "OK") throw new Exception("SetGetSync: Error"); });77
78ManualResetEventSlim e = new();79db.StringGet("mykey", (c, s) => { if (s != origValue) throw new Exception("SetGetSync: Error"); e.Set(); });80e.Wait();81Console.WriteLine("SetGetSync: Success");82}83
84async Task IncrAsync()85{86using var db = new GarnetClient(address, port, GetSslOpts());87await db.ConnectAsync();88
89// Key storing integer90int nVal = 1000;91var strKey = "key1";92await db.StringSetAsync(strKey, $"{nVal}");93var s = await db.StringGetAsync(strKey);94
95if (s != $"{nVal}")96throw new Exception("IncrementAsync: Error");97
98long n = await db.StringIncrement(strKey);99if (n != nVal + 1)100throw new Exception("IncrementAsync: Error");101
102int nRetVal = int.Parse(await db.StringGetAsync(strKey));103if (n != nRetVal)104throw new Exception("IncrementAsync: Error");105Console.WriteLine("IncrementAsync: Success");106}107
108async Task IncrByAsync(long nIncr)109{110using var db = new GarnetClient(address, port, GetSslOpts());111await db.ConnectAsync();112
113// Key storing integer114int nVal = 1000;115
116var strKey = "key1";117await db.StringSetAsync(strKey, $"{nVal}");118var s = await db.StringGetAsync(strKey);119
120if (s != $"{nVal}")121throw new Exception("IncrementByAsync: Error");122
123long n = int.Parse(await db.ExecuteForStringResultAsync("INCRBY", new string[] { strKey, nIncr.ToString() }));124if (n != nVal + nIncr)125throw new Exception("IncrementByAsync: Error");126
127int nRetVal = int.Parse(await db.StringGetAsync(strKey));128if (n != nRetVal)129throw new Exception("IncrementByAsync: Error");130Console.WriteLine("IncrementByAsync: Success");131}132
133async Task DecrByAsync(long nDecr)134{135using var db = new GarnetClient(address, port, GetSslOpts());136await db.ConnectAsync();137
138// Key storing integer139int nVal = 900;140
141var strKey = "key1";142await db.StringSetAsync(strKey, $"{nVal}");143var s = await db.StringGetAsync(strKey);144
145if (s != $"{nVal}")146throw new Exception("DecrByAsync: Error");147
148long n = int.Parse(await db.ExecuteForStringResultAsync("DECRBY", new string[] { strKey, nDecr.ToString() }));149if (n != nVal - nDecr)150throw new Exception("DecrByAsync: Error");151
152int nRetVal = int.Parse(await db.StringGetAsync(strKey));153if (n != nRetVal)154throw new Exception("DecrByAsync: Error");155Console.WriteLine("DecrByAsync: Success");156}157
158async Task DecrAsync(string strKey, int nVal)159{160using var db = new GarnetClient(address, port, GetSslOpts());161await db.ConnectAsync();162
163await db.StringSetAsync(strKey, $"{nVal}");164var s = await db.StringGetAsync(strKey);165
166if (s != $"{nVal}")167throw new Exception("DecrAsync: Error");168
169long n = await db.StringDecrement(strKey);170if (n != nVal - 1)171throw new Exception("DecrAsync: Error");172
173int nRetVal = int.Parse(await db.StringGetAsync(strKey));174if (n != nRetVal)175throw new Exception("DecrAsync: Error");176Console.WriteLine("DecrAsync: Success");177}178
179async Task IncrNoKeyAsync()180{181using var db = new GarnetClient(address, port, GetSslOpts());182await db.ConnectAsync();183
184// Key storing integer185var strKey = "key1";186int init = int.Parse(await db.StringGetAsync(strKey));187await db.StringIncrement(strKey);188
189var retVal = int.Parse(await db.StringGetAsync(strKey));190
191await db.StringIncrement(strKey);192retVal = int.Parse(await db.StringGetAsync(strKey));193
194if (init + 2 != retVal)195throw new Exception("IncrNoKeyAsync: Error");196Console.WriteLine("IncrNoKeyAsync: Success");197}198
199async Task ExistsAsync()200{201using var db = new GarnetClient(address, port, GetSslOpts());202await db.ConnectAsync();203
204// Key storing integer205int nVal = 100;206var strKey = "key1";207await db.StringSetAsync(strKey, $"{nVal}");208
209bool fExists = int.Parse(await db.ExecuteForStringResultAsync("EXISTS", new string[] { strKey })) == 1 ? true : false;210if (!fExists)211throw new Exception("ExistsAsync: Error");212Console.WriteLine("ExistsAsync: Success");213}214
215async Task DeleteAsync()216{217using var db = new GarnetClient(address, port, GetSslOpts());218await db.ConnectAsync();219
220// Key storing integer221var nVal = 100;222var strKey = "key1";223await db.StringSetAsync(strKey, $"{nVal}");224await db.KeyDeleteAsync(strKey);225
226bool fExists = int.Parse(await db.ExecuteForStringResultAsync("EXISTS", new string[] { strKey })) == 1 ? true : false;227if (fExists)228throw new Exception("DeleteAsync: Error");229Console.WriteLine("DeleteAsync: Success");230}231
232
233async Task SetGetMemoryAsync()234{235using var db = new GarnetClient(address, port, GetSslOpts());236await db.ConnectAsync();237
238var key = new Memory<byte>(new byte[17]);239Encoding.UTF8.GetBytes("SetGetMemoryAsync".AsSpan(), key.Span);240
241var origValueStr = "abcdefg";242var origValue = new Memory<byte>(new byte[7]);243Encoding.UTF8.GetBytes(origValueStr.AsSpan(), origValue.Span);244
245await db.StringSetAsync(key, origValue);246
247using var retValue = await db.StringGetAsMemoryAsync(key);248
249if (!origValue.Span.SequenceEqual(retValue.Span))250throw new Exception("SetGetAsync: Error");251
252Console.WriteLine("SetGetMemoryAsync: Success");253}254
255SslClientAuthenticationOptions GetSslOpts() => useTLS ? new()256{257ClientCertificates = [new X509Certificate2("testcert.pfx", "placeholder")],258TargetHost = "GarnetTest",259RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true,260} : null;261
262public void ConnectionStressTest()263{264for (int t = 0; t < 8; t++)265{266new Thread(() => ConnectionStressRunner(t)).Start();267}268Thread.Sleep(Timeout.Infinite);269
270void ConnectionStressRunner(int thread_id)271{272int i = 0;273while (true)274{275using var client = new GarnetClient(address, port, GetSslOpts());276client.Connect();277Console.WriteLine($"{thread_id}:{i++}: {client.PingAsync().GetAwaiter().GetResult()}");278client.Dispose();279}280}281}282}283}