Skip to content

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ejsmith
Copy link
Contributor

@ejsmith ejsmith commented Apr 2, 2025

No description provided.

@niemyjski
Copy link
Member

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 InvalidateLocalCache or InvalidateInMemoryHybridCache

@ejsmith
Copy link
Contributor Author

ejsmith commented Apr 2, 2025

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants