diff --git a/src/SwiftLink.Infrastructure/CacheProvider/RedisCacheProvider.cs b/src/SwiftLink.Infrastructure/CacheProvider/RedisCacheProvider.cs index 6ad0b6e..1fb806b 100644 --- a/src/SwiftLink.Infrastructure/CacheProvider/RedisCacheProvider.cs +++ b/src/SwiftLink.Infrastructure/CacheProvider/RedisCacheProvider.cs @@ -4,28 +4,60 @@ namespace SwiftLink.Infrastructure.CacheProvider; -public class RedisCacheService(IDistributedCache cache, IOptions options) +/// +/// RedisCacheService implements the ICacheProvider interface and provides caching functionality using a distributed cache. +/// +/// +/// Initializes a new instance of the RedisCacheService class. +/// +/// The distributed cache implementation. +/// Application settings injected as options. +public class RedisCacheService(IDistributedCache cache, IOptions options) : ICacheProvider { private readonly IDistributedCache _cache = cache; private readonly AppSettings _options = options.Value; + /// + /// Removes an item from the cache by its key. + /// + /// The key of the item to be removed from the cache. + /// A task representing the asynchronous operation. public Task Remove(string key) - => _cache.RemoveAsync(key); + => _cache.RemoveAsync(key); + /// + /// Sets a string value in the cache with a default expiration time. + /// + /// The key under which the value will be stored. + /// The string value to be stored in the cache. + /// A task representing the asynchronous operation, returning true if the operation was successful. public async Task Set(string key, string value) => await Set(key, value, DateTime.Now.AddDays(_options.DefaultExpirationTimeInDays)); + /// + /// Sets a string value in the cache with a specified expiration date. + /// + /// The key under which the value will be stored. + /// The string value to be stored in the cache. + /// The expiration date for the cached item. + /// A task representing the asynchronous operation, returning true if the operation was successful. public async Task Set(string key, string value, DateTime expirationDate) { DistributedCacheEntryOptions options = new DistributedCacheEntryOptions() .SetAbsoluteExpiration(expirationDate) .SetSlidingExpiration(TimeSpan.FromHours(_options.Redis.SlidingExpirationHour)); + var dataToCache = Encoding.UTF8.GetBytes(value); await _cache.SetAsync(key, dataToCache, options); return true; } + /// + /// Gets a string value from the cache based on its key. + /// + /// The key of the item to retrieve from the cache. + /// A task representing the asynchronous operation, returning the string value from the cache. public async Task Get(string key) => await _cache.GetStringAsync(key); }