-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Entity caching via IDistributedCache (#393)
- Loading branch information
1 parent
f4055b2
commit d2b549b
Showing
39 changed files
with
1,305 additions
and
339 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
60 changes: 60 additions & 0 deletions
60
Firebend.AutoCrud.Core/Extensions/EntityCacheServiceExtensions.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Firebend.AutoCrud.Core.Interfaces.Caching; | ||
using Firebend.AutoCrud.Core.Interfaces.Models; | ||
|
||
namespace Firebend.AutoCrud.Core.Extensions; | ||
|
||
public static class EntityCacheServiceExtensions | ||
{ | ||
public static async Task<TEntity> GetOrSetAsync<TKey, TEntity>( | ||
this IEntityCacheService<TKey, TEntity> cacheService, | ||
TKey key, | ||
Func<Task<TEntity>> factory, | ||
CancellationToken cancellationToken) | ||
where TKey : struct | ||
where TEntity : class, IEntity<TKey> | ||
{ | ||
var entity = await cacheService.GetAsync(key, cancellationToken); | ||
|
||
if (entity is not null) | ||
{ | ||
return entity; | ||
} | ||
|
||
entity = await factory(); | ||
|
||
if (entity is not null) | ||
{ | ||
await cacheService.SetAsync(entity, cancellationToken); | ||
} | ||
|
||
return entity; | ||
} | ||
|
||
public static async Task<List<TEntity>> GetOrSetAsync<TKey, TEntity>( | ||
this IEntityCacheService<TKey, TEntity> cacheService, | ||
Func<Task<List<TEntity>>> factory, | ||
CancellationToken cancellationToken) | ||
where TKey : struct | ||
where TEntity : class, IEntity<TKey> | ||
{ | ||
var collection = await cacheService.GetCollectionAsync(cancellationToken); | ||
|
||
if (collection is not null) | ||
{ | ||
return collection; | ||
} | ||
|
||
collection = await factory(); | ||
|
||
if (collection.HasValues()) | ||
{ | ||
await cacheService.SetCollectionAsync(collection, cancellationToken); | ||
} | ||
|
||
return collection; | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.