public async Task <TAgent> FetchById(string agentId) { var key = agentId; if (await csredis.ExistsAsync(key)) { return(JsonConvert.DeserializeObject <TAgent>(await csredis.GetAsync(key))); } else { return(null); } }
static void CSRedisTest(CSRedisClient con, int count) { Task[] tasks = new Task[count]; string[] keys = new string[count]; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); for (int i = 0; i < count; i++) { keys[i] = Guid.NewGuid().ToString(); tasks[i] = con.SetAsync(keys[i], Guid.NewGuid().ToString()); } Task.WaitAll(tasks); sw.Stop(); var set = sw.ElapsedMilliseconds; sw.Restart(); for (int i = 0; i < count; i++) { tasks[i] = con.GetAsync(keys[i]); } Task.WaitAll(tasks); sw.Stop(); Console.WriteLine($"CSRedisTest: [{count}] SetAsync: [{set}ms] GetAsync: [{sw.ElapsedMilliseconds}ms]"); // Test Result // foreach (var task in tasks) // { // Console.WriteLine(((Task<string>) task).Result); // } }
/// <summary> /// Try get value from cache or create into cache async. /// </summary> /// <param name="key">key name.</param> /// <param name="func">Delegate used to return value.</param> /// <returns></returns> public async Task <T> TryGetOrCreateAsync <T>(string key, Func <Task <T> > func, int expireMS) { var val = await _client.GetAsync <T>(key); if (val != null) { return(val); } var rt = await func(); await _client.SetAsync(key, rt, RandomExpireMS(expireMS)); return(rt); }
/// <summary> /// 获取或添加缓存 /// </summary> /// <typeparam name="T">获取的结果类型</typeparam> /// <param name="cacheKey">缓存key</param> /// <param name="factory">若缓存没有命中,执行的获取数据的方法</param> /// <returns></returns> public async Task <T> GetOrCreateAsync <T>(string cacheKey, Func <CacheEntryOptions, Task <T> > factory) { var entry = GetDefaultCacheEntryOptions(); // 如果不启用cache直接请求 if (!_cacheOptions.Enable) { return(await factory(entry)); } if (string.IsNullOrEmpty(cacheKey)) { throw new ArgumentNullException(nameof(cacheKey)); } if (factory == null) { throw new ArgumentNullException(nameof(factory)); } var value = await _redisClient.GetAsync <T>(cacheKey); if (value != null) { return(value); } value = await factory(entry); if (value == null) { return(default);
public async Task <T> GetAsync <T>(string key) { if (string.IsNullOrWhiteSpace(key)) { throw new ArgumentNullException(nameof(key)); } return(await client.GetAsync <T>(key)); }
public async ValueTask <ReadOnlyMemory <byte>?> GetAsync(string key, CancellationToken cancellationToken) { var result = await _client.GetAsync <byte[]>(GetKey(key)).ConfigureAwait(false); // ReSharper disable once ConvertIfStatementToReturnStatement // ReSharper disable once UseNullPropagation if (result == null) { return(null); } return(result); }
public override async Task <ServerReply> getServerIp(ServerRequest request, ServerCallContext context) { var ip = await cSRedisClient.GetAsync(request.ContainerId); if (ip == null) { ip = ""; } return(new ServerReply() { Ip = ip }); }
public async Task <string> GetItemAsync(string key) { return(await csredis.GetAsync(key)); }
/// <summary> /// 获取string类型 /// </summary> /// <param name="key">缓存键</param> /// <returns></returns> public async Task <string> GetAsync(string key) { return(await _redisClient.GetAsync <string>(key)); }