-
-
Notifications
You must be signed in to change notification settings - Fork 246
Add ability to skip local cache in hybrid cache client #371
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
Draft
ejsmith
wants to merge
1
commit into
main
Choose a base branch
from
hybridcache-skip
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+88
−12
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I was thinking something like this: public interface IHybridAwareCacheClient : ICacheClient;
public class HybridAwareCacheClient : IHybridAwareCacheClient, IHaveTimeProvider, IHaveLogger
{
protected readonly ICacheClient _distributedCache;
protected readonly IMessagePublisher _messageBusPublisher;
private readonly string _cacheId = Guid.NewGuid().ToString("N");
private readonly ILogger _logger;
public HybridAwareCacheClient(ICacheClient distributedCacheClient, IMessagePublisher messageBusPublisher, ILoggerFactory loggerFactory = null)
{
_logger = loggerFactory?.CreateLogger<HybridAwareCacheClient>() ?? NullLogger<HybridAwareCacheClient>.Instance;
_distributedCache = distributedCacheClient;
_messageBusPublisher = messageBusPublisher;
}
ILogger IHaveLogger.Logger => _logger;
TimeProvider IHaveTimeProvider.TimeProvider => _distributedCache.GetTimeProvider();
public async Task<bool> RemoveAsync(string key)
{
bool removed = await _distributedCache.RemoveAsync(key).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = [key] }).AnyContext();
return removed;
}
public async Task<bool> RemoveIfEqualAsync<T>(string key, T expected)
{
bool removed = await _distributedCache.RemoveAsync(key).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = [key] }).AnyContext();
return removed;
}
public async Task<int> RemoveAllAsync(IEnumerable<string> keys = null)
{
string[] items = keys?.ToArray();
bool flushAll = items == null || items.Length == 0;
int removed = await _distributedCache.RemoveAllAsync(items).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, FlushAll = flushAll, Keys = items }).AnyContext();
return removed;
}
public async Task<int> RemoveByPrefixAsync(string prefix)
{
int removed = await _distributedCache.RemoveByPrefixAsync(prefix).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = [prefix + "*"] }).AnyContext();
return removed;
}
public Task<CacheValue<T>> GetAsync<T>(string key)
{
return _distributedCache.GetAsync<T>(key);
}
public Task<IDictionary<string, CacheValue<T>>> GetAllAsync<T>(IEnumerable<string> keys)
{
return _distributedCache.GetAllAsync<T>(keys);
}
public Task<bool> AddAsync<T>(string key, T value, TimeSpan? expiresIn = null)
{
return _distributedCache.AddAsync(key, value, expiresIn);
}
public async Task<bool> SetAsync<T>(string key, T value, TimeSpan? expiresIn = null)
{
bool set = await _distributedCache.SetAsync(key, value, expiresIn).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = [key] }).AnyContext();
return set;
}
public async Task<int> SetAllAsync<T>(IDictionary<string, T> values, TimeSpan? expiresIn = null)
{
if (values == null || values.Count == 0)
return 0;
int set = await _distributedCache.SetAllAsync(values, expiresIn).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = values.Keys.ToArray() }).AnyContext();
return set;
}
public async Task<bool> ReplaceAsync<T>(string key, T value, TimeSpan? expiresIn = null)
{
bool replaced = await _distributedCache.ReplaceAsync(key, value, expiresIn).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = [key] }).AnyContext();
return replaced;
}
public async Task<bool> ReplaceIfEqualAsync<T>(string key, T value, T expected, TimeSpan? expiresIn = null)
{
bool replaced = await _distributedCache.ReplaceAsync(key, value, expiresIn).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = [key] }).AnyContext();
return replaced;
}
public async Task<double> IncrementAsync(string key, double amount, TimeSpan? expiresIn = null)
{
double incremented = await _distributedCache.IncrementAsync(key, amount, expiresIn).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = [key] }).AnyContext();
return incremented;
}
public async Task<long> IncrementAsync(string key, long amount, TimeSpan? expiresIn = null)
{
long incremented = await _distributedCache.IncrementAsync(key, amount, expiresIn).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = [key] }).AnyContext();
return incremented;
}
public Task<bool> ExistsAsync(string key)
{
return _distributedCache.ExistsAsync(key);
}
public Task<TimeSpan?> GetExpirationAsync(string key)
{
return _distributedCache.GetExpirationAsync(key);
}
public async Task SetExpirationAsync(string key, TimeSpan expiresIn)
{
await _distributedCache.SetExpirationAsync(key, expiresIn).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = [key] }).AnyContext();
}
public async Task<double> SetIfHigherAsync(string key, double value, TimeSpan? expiresIn = null)
{
double difference = await _distributedCache.SetIfHigherAsync(key, value, expiresIn).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = [key] }).AnyContext();
return difference;
}
public async Task<long> SetIfHigherAsync(string key, long value, TimeSpan? expiresIn = null)
{
long difference = await _distributedCache.SetIfHigherAsync(key, value, expiresIn).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = [key] }).AnyContext();
return difference;
}
public async Task<double> SetIfLowerAsync(string key, double value, TimeSpan? expiresIn = null)
{
double difference = await _distributedCache.SetIfLowerAsync(key, value, expiresIn).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = [key] }).AnyContext();
return difference;
}
public async Task<long> SetIfLowerAsync(string key, long value, TimeSpan? expiresIn = null)
{
long difference = await _distributedCache.SetIfLowerAsync(key, value, expiresIn).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = [key] }).AnyContext();
return difference;
}
public async Task<long> ListAddAsync<T>(string key, IEnumerable<T> values, TimeSpan? expiresIn = null)
{
if (values is string stringValue)
{
long set = await _distributedCache.ListAddAsync(key, stringValue, expiresIn).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = [key] }).AnyContext();
return set;
}
else
{
var items = values?.ToArray();
long set = await _distributedCache.ListAddAsync(key, items, expiresIn).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = [key] }).AnyContext();
return set;
}
}
public async Task<long> ListRemoveAsync<T>(string key, IEnumerable<T> values, TimeSpan? expiresIn = null)
{
if (values is string stringValue)
{
long removed = await _distributedCache.ListRemoveAsync(key, stringValue, expiresIn).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = [key] }).AnyContext();
return removed;
}
else
{
var items = values?.ToArray();
long removed = await _distributedCache.ListRemoveAsync(key, items, expiresIn).AnyContext();
await _messageBusPublisher.PublishAsync(new InvalidateCache { CacheId = _cacheId, Keys = [key] }).AnyContext();
return removed;
}
}
public Task<CacheValue<ICollection<T>>> GetListAsync<T>(string key, int? page = null, int pageSize = 100)
{
return _distributedCache.GetListAsync<T>(key, page, pageSize);
}
public virtual void Dispose()
{
}
public class InvalidateCache
{
public string CacheId { get; set; }
public string[] Keys { get; set; }
public bool FlushAll { get; set; }
public bool Expired { get; set; }
}
} Was tempted to also move InvalidateCache out of this class and rename it to |
Seems like a lot of repeated code and probably going to miss some spots of keeping it in sync. I don't care that much though if it's built outside of the existing stuff. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.