/// <inheritdoc /> public async ValueTask <T> AddAsync(T entity) { Log.Debug($"Pushing {typeof(T).Name} to cache as {GetCacheKey(entity)} - {GetMemberKey(entity)}"); if (entity == null) { throw new ArgumentNullException(nameof(entity)); } await cacheClient.HashUpsertAsync( GetCacheKey(entity), GetMemberKey(entity), entity); return(entity); }
/// <inheritdoc /> public async ValueTask <BlackjackContext> AddAsync(BlackjackContext entity) { var value = await GetAsync(entity.ChannelId, entity.UserId); if (value != null) { throw new DuplicateSessionException(); } var key = GetInstanceKey(entity.ChannelId, entity.UserId); Logging.Log.Debug($"ADD - {SessionKey}: {key}"); await cache.HashUpsertAsync(SessionKey, key, entity); return(entity); }
public static async Task SyncAvatarAsync(IDiscordUser user, IExtendedCacheClient cache, MikiDbContext context) { PutObjectRequest request = new PutObjectRequest(); request.BucketName = "miki-cdn"; request.Key = $"avatars/{user.Id}.png"; request.ContentType = "image/png"; request.CannedACL = new S3CannedACL("public-read"); string avatarUrl = user.GetAvatarUrl(); using (var client = new Rest.RestClient(avatarUrl, true)) { request.InputStream = await client.GetStreamAsync(); } var response = await Global.CdnClient.PutObjectAsync(request); if (response.HttpStatusCode != System.Net.HttpStatusCode.OK) { throw new AvatarSyncException(); } await MikiApp.Instance.GetService <BunnyCDNClient>() .PurgeCacheAsync($"https://mikido.b-cdn.net/avatars/{user.Id}.png"); User u = await User.GetAsync(context, user.Id, user.Username); await cache.HashUpsertAsync("avtr:sync", user.Id.ToString(), 1); u.AvatarUrl = u.Id.ToString(); await context.SaveChangesAsync(); }
/// <inheritdoc /> public async ValueTask <Optional <Block> > GetBlockAsync(long guildId, string commandName) { var cachePackage = await cache.HashGetAsync <BlockCache>( CommandCacheKey, commandName + ":" + guildId); if (cachePackage != null) { await using var stream = new MemoryStream(cachePackage.Bytes); var reader = new BinaryReader(stream); return(reader.ReadBlock()); } var repository = unitOfWork.GetRepository <CustomCommand>(); var command = await repository.GetAsync(guildId, commandName); if (command == null) { return(Optional <Block> .None); } var block = BlockGenerator.Compile(command.CommandBody); await cache.HashUpsertAsync( CommandCacheKey, commandName + ":" + guildId, BlockCache.Create(block)); return(block); }
protected async Task QueueTaskAsync(TaskPayload payload) { await cacheClient.SortedSetUpsertAsync( parent.GetQueueNamespace(), payload.GetKey(), payload.TimeEpoch.Seconds); await cacheClient.HashUpsertAsync( parent.GetObjectNamespace(payload.OwnerId), payload.Uuid, payload); }
private async Task RequeueWorkAsync(TaskPayload payload, bool updatePayload) { if (updatePayload) { await cacheClient.HashUpsertAsync( GetObjectNamespace(payload.OwnerId), payload.Uuid, payload); } await cacheClient.SortedSetUpsertAsync( SchedulerQueueKey, payload.GetKey(), payload.TimeEpoch.Seconds); }
/// <summary> /// Update the values in Redis. /// </summary> public async ValueTask UpdateAsync(Context context, CancellationToken token = default) { if (updatedKeys.Count == 0) { return; } var cacheKey = GetCacheKey(guildId); var cacheKeys = (await cache.HashKeysAsync(cacheKey)).ToList(); var deletedKeys = updatedKeys .Where(k => !values.TryGetValue(k, out var value) || value.IsNull) .ToList(); foreach (var key in deletedKeys.Where(cacheKeys.Contains)) { await cache.HashDeleteAsync(cacheKey, key); } foreach (var key in updatedKeys) { if (deletedKeys.Contains(key)) { continue; } var value = values[key]; if (cacheKeys.Count >= keyLimit && !cacheKeys.Contains(key)) { throw new MiScriptException($"You can store up to {keyLimit} keys in the storage"); } await using var stream = new MemoryStream(); var writer = new StreamWriter(stream); var jsonWriter = new JsonTextWriter(writer); await value.WriteJsonAsync(context, jsonWriter); await jsonWriter.FlushAsync(token); var json = Encoding.UTF8.GetString(stream.ToArray()); if (json.Length > valueLimit) { throw new MiScriptException($"The value limit is {valueLimit} bytes, tried to store {json.Length} bytes."); } await cache.HashUpsertAsync(cacheKey, key, json); } }
public async Task AddAsync(string key, T value) { await _cacheClient.HashUpsertAsync(_key, key, value); }
/// <inheritdoc /> public async ValueTask SetCurrentUserAsync(DiscordUserPacket packet) { await cache.HashUpsertAsync(CacheHelpers.UsersCacheKey, "me", packet); }