private static async Task AddTestKeysAsync(IRedisClientAsync redis, int count) { for (int i = 0; i < count; i++) { await redis.SetValueAsync("key:" + i, "value:" + i); } }
internal static ValueTask <RedisLock> CreateAsync(IRedisClientAsync redisClient, string key, TimeSpan?timeOut = default, CancellationToken token = default) { var obj = new RedisLock(redisClient, key); return(obj.AcquireAsync(timeOut, token).Await(obj)); }
private static void AssertClientHasHost(IRedisClientAsync client, string hostWithOptionalPort) { var parts = hostWithOptionalPort.Split(':'); var port = parts.Length > 1 ? int.Parse(parts[1]) : RedisConfig.DefaultPort; Assert.That(client.Host, Is.EqualTo(parts[0])); Assert.That(client.Port, Is.EqualTo(port)); }
public override ValueTask ExecuteAsync(IRedisClientAsync client) { try { switch (_asyncReturnCommand) { case null: ExecuteThrowIfSync(); return(default); case Func <IRedisClientAsync, ValueTask> VoidReturnCommandAsync: return(VoidReturnCommandAsync(client)); case Func <IRedisClientAsync, ValueTask <int> > IntReturnCommandAsync: return(IntReturnCommandAsync(client).Await()); case Func <IRedisClientAsync, ValueTask <long> > LongReturnCommandAsync: return(LongReturnCommandAsync(client).Await()); case Func <IRedisClientAsync, ValueTask <double> > DoubleReturnCommandAsync: return(DoubleReturnCommandAsync(client).Await()); case Func <IRedisClientAsync, ValueTask <byte[]> > BytesReturnCommandAsync: return(BytesReturnCommandAsync(client).Await()); case Func <IRedisClientAsync, ValueTask <string> > StringReturnCommandAsync: return(StringReturnCommandAsync(client).Await()); case Func <IRedisClientAsync, ValueTask <byte[][]> > MultiBytesReturnCommandAsync: return(MultiBytesReturnCommandAsync(client).Await()); case Func <IRedisClientAsync, ValueTask <List <string> > > MultiStringReturnCommandAsync: return(MultiStringReturnCommandAsync(client).Await()); case Func <IRedisClientAsync, ValueTask <Dictionary <string, string> > > DictionaryStringReturnCommandAsync: return(DictionaryStringReturnCommandAsync(client).Await()); case Func <IRedisClientAsync, ValueTask <RedisData> > RedisDataReturnCommandAsync: return(RedisDataReturnCommandAsync(client).Await()); case Func <IRedisClientAsync, ValueTask <RedisText> > RedisTextReturnCommandAsync: return(RedisTextReturnCommandAsync(client).Await()); case Func <IRedisClientAsync, ValueTask <bool> > BoolReturnCommandAsync: return(BoolReturnCommandAsync(client).Await()); case object obj: ExecuteThrowIfSync(); return(default); } } catch (Exception ex) { Log.Error(ex); return(default); // RedisCommand.Execute swallows here; we'll do the same
public async Task SetUp() { if (client is object) { await client.DisposeAsync(); client = null; } client = new RedisClient(TestConfig.SingleHost).ForAsyncOnly(); await client.FlushAllAsync(); redis = client.As <T>(); }
public async Task SetUp() { if (client is object) { await client.DisposeAsync(); client = null; } client = new RedisClient(TestConfig.SingleHost); await client.FlushAllAsync(); redis = client.As <T>(); Hash = redis.GetHash <string>(HashId); }
public async Task SetUp() { if (client is object) { await client.DisposeAsync(); client = null; } client = new RedisClient(TestConfig.SingleHost); await client.FlushAllAsync(); redis = client.As <CustomType>(); List = redis.Lists[ListId]; // List2 = redis.Lists[ListId2]; }
public async Task Teardown() { _seredis?.Dispose(); _ssredis?.Dispose(); if (_respite != null) { await _respite.DisposeAsync(); } _seredis_server = null; _seredis_db = null; _seredis = null; _ssredis = null; _respite = null; _ssAsync = null; }
public override ValueTask ExecuteAsync(IRedisClientAsync client) { try { switch (AsyncReturnCommand) { case null: ExecuteThrowIfSync(); return(default); case Func <IRedisClientAsync, ValueTask> VoidReturnCommandAsync: return(VoidReturnCommandAsync(client)); case Func <IRedisClientAsync, ValueTask <int> > IntReturnCommandAsync: return(IntReturnCommandAsync(client).Await()); case Func <IRedisClientAsync, ValueTask <long> > LongReturnCommandAsync: return(LongReturnCommandAsync(client).Await()); case Func <IRedisClientAsync, ValueTask <double> > DoubleReturnCommandAsync: return(DoubleReturnCommandAsync(client).Await()); case Func <IRedisClientAsync, ValueTask <byte[]> > BytesReturnCommandAsync: return(BytesReturnCommandAsync(client).Await()); case Func <IRedisClientAsync, ValueTask <string> > StringReturnCommandAsync: return(StringReturnCommandAsync(client).Await()); case Func <IRedisClientAsync, ValueTask <byte[][]> > MultiBytesReturnCommandAsync: return(MultiBytesReturnCommandAsync(client).Await()); case Func <IRedisClientAsync, ValueTask <List <string> > > MultiStringReturnCommandAsync: return(MultiStringReturnCommandAsync(client).Await()); case object obj: ExecuteThrowIfSync(); // Execute only processes a limited number of patterns; we'll respect that here too throw new InvalidOperationException("Command cannot be executed in this context: " + obj.GetType().FullName); } } catch (Exception ex) { Log.Error(ex); throw; } }
internal async Task Setup(bool minimal) { _ssredis = RedisClient.New(); _ssAsync = _ssredis; if (!minimal) { _seredis = await ConnectionMultiplexer.ConnectAsync("127.0.0.1:6379"); _seredis_server = _seredis.GetServer(_seredis.GetEndPoints().Single()); _seredis_db = _seredis.GetDatabase(); var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); SocketConnection.SetRecommendedClientOptions(socket); socket.Connect("127.0.0.1", 6379); _respite = RespConnection.Create(socket); } }
public GameHub(ITimerService timerService) { if (timerService == null) { throw new ArgumentNullException(nameof(timerService)); } this.timers = timerService; redis = new RedisClient(); rng = new System.Random(); if (groundTruth == null) { using (var file = File.OpenRead("words.json")) { var dictT = JsonSerializer.DeserializeAsync <Dictionary <string, string> >(file); groundTruth = new List <KeyValuePair <string, string> >(dictT.Result); } } }
public RedisClientFacadeAsync(IRedisClientAsync redisClient) { this.redisClient = redisClient; }
async ValueTask <LockState> IDistributedLockAsync.LockAsync(string key, int acquisitionTimeout, int lockTimeout, IRedisClientAsync client, CancellationToken token) { long lockExpire = 0; // cannot lock on a null key if (key == null) { return(new LockState(LOCK_NOT_ACQUIRED, lockExpire)); } const int sleepIfLockSet = 200; acquisitionTimeout *= 1000; //convert to ms int tryCount = (acquisitionTimeout / sleepIfLockSet) + 1; var ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)); var newLockExpire = CalculateLockExpire(ts, lockTimeout); var nativeClient = (IRedisNativeClientAsync)client; long wasSet = await nativeClient.SetNXAsync(key, BitConverter.GetBytes(newLockExpire), token).ConfigureAwait(false); int totalTime = 0; while (wasSet == LOCK_NOT_ACQUIRED && totalTime < acquisitionTimeout) { int count = 0; while (wasSet == 0 && count < tryCount && totalTime < acquisitionTimeout) { await Task.Delay(sleepIfLockSet).ConfigureAwait(false); totalTime += sleepIfLockSet; ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)); newLockExpire = CalculateLockExpire(ts, lockTimeout); wasSet = await nativeClient.SetNXAsync(key, BitConverter.GetBytes(newLockExpire), token).ConfigureAwait(false); count++; } // acquired lock! if (wasSet != LOCK_NOT_ACQUIRED) { break; } // handle possibliity of crashed client still holding the lock var pipe = client.CreatePipeline(); await using (pipe.ConfigureAwait(false)) { long lockValue = 0; pipe.QueueCommand(r => ((IRedisNativeClientAsync)r).WatchAsync(new[] { key }, token)); pipe.QueueCommand(r => ((IRedisNativeClientAsync)r).GetAsync(key, token), x => lockValue = (x != null) ? BitConverter.ToInt64(x, 0) : 0); await pipe.FlushAsync(token).ConfigureAwait(false); // if lock value is 0 (key is empty), or expired, then we can try to acquire it ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)); if (lockValue < ts.TotalSeconds) { ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)); newLockExpire = CalculateLockExpire(ts, lockTimeout); var trans = await client.CreateTransactionAsync(token).ConfigureAwait(false); await using (trans.ConfigureAwait(false)) { var expire = newLockExpire; trans.QueueCommand(r => ((IRedisNativeClientAsync)r).SetAsync(key, BitConverter.GetBytes(expire), token: token)); if (await trans.CommitAsync(token).ConfigureAwait(false)) { wasSet = LOCK_RECOVERED; //recovered lock! } } } else { await nativeClient.UnWatchAsync(token).ConfigureAwait(false); } } if (wasSet != LOCK_NOT_ACQUIRED) { break; } await Task.Delay(sleepIfLockSet).ConfigureAwait(false); totalTime += sleepIfLockSet; } if (wasSet != LOCK_NOT_ACQUIRED) { lockExpire = newLockExpire; } return(new LockState(wasSet, lockExpire)); }
async ValueTask <bool> IDistributedLockAsync.UnlockAsync(string key, long lockExpire, IRedisClientAsync client, CancellationToken token) { if (lockExpire <= 0) { return(false); } long lockVal = 0; var nativeClient = (IRedisNativeClientAsync)client; var pipe = client.CreatePipeline(); await using (pipe.ConfigureAwait(false)) { pipe.QueueCommand(r => ((IRedisNativeClientAsync)r).WatchAsync(new[] { key }, token)); pipe.QueueCommand(r => ((IRedisNativeClientAsync)r).GetAsync(key, token), x => lockVal = (x != null) ? BitConverter.ToInt64(x, 0) : 0); await pipe.FlushAsync(token).ConfigureAwait(false); } if (lockVal != lockExpire) { if (lockVal != 0) { Debug.WriteLine($"Unlock(): Failed to unlock key {key}; lock has been acquired by another client "); } else { Debug.WriteLine($"Unlock(): Failed to unlock key {key}; lock has been identifed as a zombie and harvested "); } await nativeClient.UnWatchAsync(token).ConfigureAwait(false); return(false); } var trans = await client.CreateTransactionAsync(token).ConfigureAwait(false); await using (trans.ConfigureAwait(false)) { trans.QueueCommand(r => ((IRedisNativeClientAsync)r).DelAsync(key, token)); var rc = await trans.CommitAsync(token).ConfigureAwait(false); if (!rc) { Debug.WriteLine($"Unlock(): Failed to delete key {key}; lock has been acquired by another client "); } return(rc); } }
public virtual ValueTask ExecuteAsync(IRedisClientAsync client) => default;
public RedisGeoTestsAsync() { redis = new RedisClient(TestConfig.GeoHost); }
public static ValueTask <long> AppendToValueAsync(this IRedisClientAsync redis, string key, string value, CancellationToken token = default) => redis.AppendToAsync(key, value, token);
private async Task IncrementKeyInsideLock(String key, String lockKey, int clientNo, IRedisClientAsync client) { await using (await client.AcquireLockAsync(lockKey)) { Debug.WriteLine(String.Format("client {0} acquired lock", clientNo)); var val = await client.GetAsync <int>(key); await Task.Delay(200); await client.SetAsync(key, val + 1); Debug.WriteLine(String.Format("client {0} released lock", clientNo)); } }