diff --git a/src/Backend/Repositories/FluentCMS.Repositories.Caching/EntityRepository.cs b/src/Backend/Repositories/FluentCMS.Repositories.Caching/EntityRepository.cs index 167f99c14..a7c663804 100644 --- a/src/Backend/Repositories/FluentCMS.Repositories.Caching/EntityRepository.cs +++ b/src/Backend/Repositories/FluentCMS.Repositories.Caching/EntityRepository.cs @@ -4,35 +4,35 @@ public abstract class EntityRepository(IEntityRepository entit { private static string GetAllCacheKey => $"{typeof(TEntity).Name}_GetAll"; - public async Task Create(TEntity entity, CancellationToken cancellationToken = default) + public virtual async Task Create(TEntity entity, CancellationToken cancellationToken = default) { var newEntity = await entityRepository.Create(entity, cancellationToken); InvalidateCache(); return newEntity; } - public async Task> CreateMany(IEnumerable entities, CancellationToken cancellationToken = default) + public virtual async Task> CreateMany(IEnumerable entities, CancellationToken cancellationToken = default) { var newEntities = await entityRepository.CreateMany(entities, cancellationToken); InvalidateCache(); return newEntities ?? []; } - public async Task Delete(Guid id, CancellationToken cancellationToken = default) + public virtual async Task Delete(Guid id, CancellationToken cancellationToken = default) { var deleted = await entityRepository.Delete(id, cancellationToken); InvalidateCache(); return deleted; } - public async Task> DeleteMany(IEnumerable ids, CancellationToken cancellationToken = default) + public virtual async Task> DeleteMany(IEnumerable ids, CancellationToken cancellationToken = default) { var deleted = await entityRepository.DeleteMany(ids, cancellationToken); InvalidateCache(); return deleted; } - public async Task> GetAll(CancellationToken cancellationToken = default) + public virtual async Task> GetAll(CancellationToken cancellationToken = default) { var entitiesDict = await GetCachedDictionary(cancellationToken); return entitiesDict.Values; @@ -51,7 +51,7 @@ public async Task> GetAll(CancellationToken cancellationTok return default; } - public async Task> GetByIds(IEnumerable ids, CancellationToken cancellationToken = default) + public virtual async Task> GetByIds(IEnumerable ids, CancellationToken cancellationToken = default) { // access to cached dictionary var entitiesDict = await GetCachedDictionary(cancellationToken); diff --git a/src/Backend/Repositories/FluentCMS.Repositories.Caching/SiteAssociatedRepository.cs b/src/Backend/Repositories/FluentCMS.Repositories.Caching/SiteAssociatedRepository.cs index e0dc480ea..9ebe93d8c 100644 --- a/src/Backend/Repositories/FluentCMS.Repositories.Caching/SiteAssociatedRepository.cs +++ b/src/Backend/Repositories/FluentCMS.Repositories.Caching/SiteAssociatedRepository.cs @@ -2,9 +2,9 @@ public abstract class SiteAssociatedRepository(ISiteAssociatedRepository siteAssociatedRepository, ICacheProvider cacheProvider) : AuditableEntityRepository(siteAssociatedRepository, cacheProvider), ISiteAssociatedRepository where TEntity : ISiteAssociatedEntity { - public async Task> GetAllForSite(Guid siteId, CancellationToken cancellationToken = default) + public virtual async Task> GetAllForSite(Guid siteId, CancellationToken cancellationToken = default) { - var entities = await siteAssociatedRepository.GetAll(cancellationToken); + var entities = await GetAll(cancellationToken); return entities.Where(e => e.SiteId == siteId); } } diff --git a/src/Backend/Repositories/FluentCMS.Repositories.LiteDb/SiteAssociatedRepository.cs b/src/Backend/Repositories/FluentCMS.Repositories.LiteDb/SiteAssociatedRepository.cs index 94cab133f..fe5c95206 100644 --- a/src/Backend/Repositories/FluentCMS.Repositories.LiteDb/SiteAssociatedRepository.cs +++ b/src/Backend/Repositories/FluentCMS.Repositories.LiteDb/SiteAssociatedRepository.cs @@ -17,7 +17,7 @@ public abstract class SiteAssociatedRepository(ILiteDBContext liteDbCon return await base.Update(entity, cancellationToken); } - public async Task> GetAllForSite(Guid siteId, CancellationToken cancellationToken = default) + public virtual async Task> GetAllForSite(Guid siteId, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); return await Collection.Query().Where(x => x.SiteId == siteId).ToListAsync(); diff --git a/src/Providers/CacheProviders/FluentCMS.Providers.CacheProviders.Abstractions/ICacheProvider.cs b/src/Providers/CacheProviders/FluentCMS.Providers.CacheProviders.Abstractions/ICacheProvider.cs index efda7cdcf..c9180cd9a 100644 --- a/src/Providers/CacheProviders/FluentCMS.Providers.CacheProviders.Abstractions/ICacheProvider.cs +++ b/src/Providers/CacheProviders/FluentCMS.Providers.CacheProviders.Abstractions/ICacheProvider.cs @@ -1,8 +1,34 @@ namespace FluentCMS.Providers.CacheProviders; +/// +/// Interface for a cache provider that supports adding, retrieving, and removing cached items. +/// public interface ICacheProvider { + /// + /// Removes a specific item from the cache by its key. + /// + /// The unique string identifier for the cache entry. void Remove(string key); + + /// + /// Adds or updates an item in the cache. + /// + /// The type of the value to cache. + /// The unique string identifier for the cache entry. + /// The value to cache, of type . void Set(string key, T value); - bool TryGetValue(object key, out T? value); + + /// + /// Attempts to retrieve a cached item by its key. + /// + /// The type of the cached item. + /// The unique string identifier for the cache entry. + /// + /// When this method returns, contains the cached item of type if found; otherwise, the default value for the type of the parameter. + /// + /// + /// true if the cache contains an item with the specified key; otherwise, false. + /// + bool TryGetValue(string key, out T? value); } diff --git a/src/Providers/CacheProviders/FluentCMS.Providers.CacheProviders/FluentCMS.Providers.CacheProviders.csproj b/src/Providers/CacheProviders/FluentCMS.Providers.CacheProviders/FluentCMS.Providers.CacheProviders.csproj index a07a93d1d..ed07d0510 100644 --- a/src/Providers/CacheProviders/FluentCMS.Providers.CacheProviders/FluentCMS.Providers.CacheProviders.csproj +++ b/src/Providers/CacheProviders/FluentCMS.Providers.CacheProviders/FluentCMS.Providers.CacheProviders.csproj @@ -7,7 +7,6 @@ - diff --git a/src/Providers/CacheProviders/FluentCMS.Providers.CacheProviders/InMemoryCacheProvider.cs b/src/Providers/CacheProviders/FluentCMS.Providers.CacheProviders/InMemoryCacheProvider.cs index 6b970eb6b..f05b38309 100644 --- a/src/Providers/CacheProviders/FluentCMS.Providers.CacheProviders/InMemoryCacheProvider.cs +++ b/src/Providers/CacheProviders/FluentCMS.Providers.CacheProviders/InMemoryCacheProvider.cs @@ -1,10 +1,10 @@ -using AutoMapper; -using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Caching.Memory; namespace FluentCMS.Providers.CacheProviders; -public class InMemoryCacheProvider(IMemoryCache memoryCache, IMapper mapper) : ICacheProvider +public class InMemoryCacheProvider(IMemoryCache memoryCache) : ICacheProvider { + // Remove an item from the cache by key public void Remove(string key) { memoryCache.Remove(key); @@ -12,27 +12,26 @@ public void Remove(string key) public void Set(string key, T value) { - var cachedValue = mapper.Map(value); - memoryCache.Set(key, cachedValue); + memoryCache.Set(key, value); } - public bool TryGetValue(object key, out T? value) + public bool TryGetValue(string key, out T? value) { - if (memoryCache.TryGetValue(key, out object? result)) + // Try to get the cached item by key + if (memoryCache.TryGetValue(key, out T? result)) { - if (result == null) + // If the cached value is explicitly null + if (result is null) { - value = default; - return true; + value = default; // Return default value for type T + return true; // Indicates that a null value was found in cache } - if (result is T item) - { - value = mapper.Map(item); - return true; - } + value = result; // Return the cached value + return true; } + // If the item was not found in the cache value = default; return false; }