Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added cache read mode to redis client options #81

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Foundatio.Redis/Cache/RedisCacheClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public async Task<CacheValue<T>> GetAsync<T>(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<T>(redisValue);
}

Expand Down Expand Up @@ -149,7 +149,7 @@ private CacheValue<T> RedisValueToCacheValue<T>(RedisValue redisValue) {

public async Task<IDictionary<string, CacheValue<T>>> GetAllAsync<T>(IEnumerable<string> 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<string, CacheValue<T>>();
for (int i = 0; i < keyArray.Length; i++)
Expand All @@ -166,12 +166,12 @@ public async Task<CacheValue<ICollection<T>>> GetListAsync<T>(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<T>(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<T>(set);
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/Foundatio.Redis/Cache/RedisCacheClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public class RedisCacheClientOptions : SharedOptions {
/// </summary>
public bool ShouldThrowOnSerializationError { get; set; } = true;

/// <summary>
/// The behaviour required when performing read operations from cache
/// </summary>
public CommandFlags ReadMode { get; set; } = CommandFlags.None;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to include a comment on when you would prefer replica vs primary. I'd think you'd still want to prefer replica by default no? @ejsmith

Also seeing the latest redis client release, wonder if this fixes some of the issues you saw.. https://stackexchange.github.io/StackExchange.Redis/ReleaseNotes

Fix #2593: EXPIRETIME and PEXPIRETIME miscategorized as PrimaryOnly commands causing them to fail when issued against a read-only replica (#2593 by slorello89)

On a side note, can you please sign the CLA.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think you should opt into reading potentially stale data. Really you'd want to make that decision on a case by case basis.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@niemyjski I signed the CLA but it still shows that the check fails. maybe I messed something up?
I think I didn't set author when I pushed the commit so maybe it used different user which I use for my other git server? (I imagined using my github key is enough for it to be linked to this account? should I try and create new commit and PR or maybe I'm missing something simpler?)
Thanks.


}

public class RedisCacheClientOptionsBuilder : SharedOptionsBuilder<RedisCacheClientOptions, RedisCacheClientOptionsBuilder> {
Expand Down
Loading