From 6545f35b42f9ed07fe603ce40e40db62e78ca77b Mon Sep 17 00:00:00 2001 From: Seadoo2017 <30262101+Seadoo2017@users.noreply.github.com> Date: Mon, 18 Dec 2023 05:52:10 +0200 Subject: [PATCH] added cache read mode to redis client options (#82) --- src/Foundatio.Redis/Cache/RedisCacheClient.cs | 8 ++++---- src/Foundatio.Redis/Cache/RedisCacheClientOptions.cs | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Foundatio.Redis/Cache/RedisCacheClient.cs b/src/Foundatio.Redis/Cache/RedisCacheClient.cs index fcf6ea6..cf99949 100644 --- a/src/Foundatio.Redis/Cache/RedisCacheClient.cs +++ b/src/Foundatio.Redis/Cache/RedisCacheClient.cs @@ -102,7 +102,7 @@ public async Task> GetAsync(string key) { if (String.IsNullOrEmpty(key)) throw new ArgumentNullException(nameof(key), "Key cannot be null or empty."); - var redisValue = await Database.StringGetAsync(key, CommandFlags.PreferReplica).AnyContext(); + var redisValue = await Database.StringGetAsync(key, _options.ReadMode).AnyContext(); return RedisValueToCacheValue(redisValue); } @@ -149,7 +149,7 @@ private CacheValue RedisValueToCacheValue(RedisValue redisValue) { public async Task>> GetAllAsync(IEnumerable keys) { string[] keyArray = keys.ToArray(); - var values = await Database.StringGetAsync(keyArray.Select(k => (RedisKey)k).ToArray(), CommandFlags.PreferReplica).AnyContext(); + var values = await Database.StringGetAsync(keyArray.Select(k => (RedisKey)k).ToArray(), _options.ReadMode).AnyContext(); var result = new Dictionary>(); for (int i = 0; i < keyArray.Length; i++) @@ -166,12 +166,12 @@ public async Task>> GetListAsync(string key, int? p throw new ArgumentNullException(nameof(page), "Page cannot be less than 1."); if (!page.HasValue) { - var set = await Database.SortedSetRangeByScoreAsync(key, flags: CommandFlags.PreferReplica).AnyContext(); + var set = await Database.SortedSetRangeByScoreAsync(key, flags: _options.ReadMode).AnyContext(); return RedisValuesToCacheValue(set); } else { long start = ((page.Value - 1) * pageSize); long end = start + pageSize - 1; - var set = await Database.SortedSetRangeByRankAsync(key, start, end, flags: CommandFlags.PreferReplica).AnyContext(); + var set = await Database.SortedSetRangeByRankAsync(key, start, end, flags: _options.ReadMode).AnyContext(); return RedisValuesToCacheValue(set); } } diff --git a/src/Foundatio.Redis/Cache/RedisCacheClientOptions.cs b/src/Foundatio.Redis/Cache/RedisCacheClientOptions.cs index 5f78e82..bd82447 100644 --- a/src/Foundatio.Redis/Cache/RedisCacheClientOptions.cs +++ b/src/Foundatio.Redis/Cache/RedisCacheClientOptions.cs @@ -11,6 +11,11 @@ public class RedisCacheClientOptions : SharedOptions { /// public bool ShouldThrowOnSerializationError { get; set; } = true; + /// + /// The behaviour required when performing read operations from cache + /// + public CommandFlags ReadMode { get; set; } = CommandFlags.None; + } public class RedisCacheClientOptionsBuilder : SharedOptionsBuilder {