start collection cache
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
namespace zero.Collections;
|
||||
|
||||
public record CachableEntityCollectionOptions(bool CacheIndividual, bool CacheAll);
|
||||
|
||||
|
||||
public abstract class CachableEntityCollection<T> : EntityCollection<T> where T : ZeroIdEntity, new()
|
||||
{
|
||||
protected CachableEntityCollectionOptions CacheOptions { get; set; }
|
||||
|
||||
protected ICollectionCache Cache { get; set; }
|
||||
|
||||
|
||||
public CachableEntityCollection(ICollectionContext collectionContext, ICollectionCache collectionCache) : base(collectionContext)
|
||||
{
|
||||
CacheOptions = new(CacheIndividual: true, CacheAll: false); // TODO when props update we need to update Cache.For()
|
||||
Cache = collectionCache.For(CacheOptions);
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public override async Task<T> Load(string id, string changeVector = null)
|
||||
{
|
||||
if (changeVector.IsNullOrEmpty() && Cache.TryGetValue(id, out T model))
|
||||
{
|
||||
return model;
|
||||
}
|
||||
|
||||
return await base.Load(id, changeVector);
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public override Task<Dictionary<string, T>> Load(IEnumerable<string> ids)
|
||||
{
|
||||
return base.Load(ids);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace zero.Collections;
|
||||
|
||||
public class CollectionCache : ICollectionCache
|
||||
{
|
||||
protected ConcurrentDictionary<string, object> _cache = new();
|
||||
|
||||
|
||||
public CollectionCache()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public bool TryGetValue<T>(string id, out T model)
|
||||
{
|
||||
if (_cache.TryGetValue(id, out object modelObj))
|
||||
{
|
||||
model = (T)modelObj;
|
||||
return true;
|
||||
}
|
||||
|
||||
model = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public ICollectionCache For(CachableEntityCollectionOptions options)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface ICollectionCache
|
||||
{
|
||||
ICollectionCache For(CachableEntityCollectionOptions options);
|
||||
|
||||
bool TryGetValue<T>(string id, out T model);
|
||||
}
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
namespace zero.Collections;
|
||||
|
||||
public class CountriesCollection : EntityCollection<Country>, ICountriesCollection
|
||||
public class CountriesCollection : CachableEntityCollection<Country>, ICountriesCollection
|
||||
{
|
||||
public CountriesCollection(ICollectionContext context) : base(context) { }
|
||||
public CountriesCollection(ICollectionContext context, ICollectionCache cache) : base(context, cache) { }
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void ValidationRules(ZeroValidator<Country> validator)
|
||||
|
||||
@@ -4,7 +4,7 @@ using Raven.Client.Documents.Linq;
|
||||
|
||||
namespace zero.Collections;
|
||||
|
||||
public abstract partial class EntityCollection<T> : IEntityCollection<T> where T : ZeroIdEntity, new()
|
||||
public abstract class EntityCollection<T> : IEntityCollection<T> where T : ZeroIdEntity, new()
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public Guid Guid { get; private set; } = Guid.NewGuid();
|
||||
@@ -29,7 +29,7 @@ public abstract partial class EntityCollection<T> : IEntityCollection<T> where T
|
||||
Operations = collectionContext.Operations;
|
||||
Context = collectionContext.Context;
|
||||
Interceptors = collectionContext.Interceptors;
|
||||
Options = new(true);
|
||||
Options = new(IncludeInactive: true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user