Skip to content

Commit

Permalink
Storage cache in the unit of work.
Browse files Browse the repository at this point in the history
Resolve #4040
  • Loading branch information
maliming committed Jun 29, 2020
1 parent e07fb5e commit f8a251a
Show file tree
Hide file tree
Showing 12 changed files with 924 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
using Volo.Abp.MultiTenancy;
using Volo.Abp.Serialization;
using Volo.Abp.Threading;
using Volo.Abp.Uow;

namespace Volo.Abp.Caching
{
[DependsOn(
typeof(AbpThreadingModule),
typeof(AbpSerializationModule),
typeof(AbpUnitOfWorkModule),
typeof(AbpMultiTenancyModule),
typeof(AbpJsonModule))]
public class AbpCachingModule : AbpModule
Expand Down
546 changes: 435 additions & 111 deletions framework/src/Volo.Abp.Caching/Volo/Abp/Caching/DistributedCache.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -29,42 +29,48 @@ public interface IDistributedCache<TCacheItem, TCacheKey>
/// Gets a cache item with the given key. If no cache item is found for the given key then returns null.
/// </summary>
/// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <returns>The cache item, or null.</returns>
TCacheItem Get(
TCacheKey key,
bool considerUow = false,
bool? hideErrors = null
);

/// <summary>
/// Gets multiple cache items with the given keys.
///
/// The returned list contains exactly the same count of items specified in the given keys.
/// An item in the return list can not be null, but an item in the list has null value
/// if the related key not found in the cache.
/// if the related key not found in the cache.
/// </summary>
/// <param name="keys">The keys of cached items to be retrieved from the cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <returns>List of cache items.</returns>
KeyValuePair<TCacheKey, TCacheItem>[] GetMany(
IEnumerable<TCacheKey> keys,
bool considerUow = false,
bool? hideErrors = null
);

/// <summary>
/// Gets multiple cache items with the given keys.
///
/// The returned list contains exactly the same count of items specified in the given keys.
/// An item in the return list can not be null, but an item in the list has null value
/// if the related key not found in the cache.
///
///
/// </summary>
/// <param name="keys">The keys of cached items to be retrieved from the cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// /// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param>
/// <returns>List of cache items.</returns>
Task<KeyValuePair<TCacheKey, TCacheItem>[]> GetManyAsync(
IEnumerable<TCacheKey> keys,
bool considerUow = false,
bool? hideErrors = null,
CancellationToken token = default
);
Expand All @@ -74,10 +80,12 @@ Task<KeyValuePair<TCacheKey, TCacheItem>[]> GetManyAsync(
/// </summary>
/// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param>
/// <returns>The cache item, or null.</returns>
Task<TCacheItem> GetAsync(
[NotNull] TCacheKey key,
bool considerUow = false,
bool? hideErrors = null,
CancellationToken token = default
);
Expand All @@ -89,12 +97,14 @@ Task<TCacheItem> GetAsync(
/// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="factory">The factory delegate is used to provide the cache item when no cache item is found for the given <paramref name="key" />.</param>
/// <param name="optionsFactory">The cache options for the factory delegate.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <returns>The cache item.</returns>
TCacheItem GetOrAdd(
TCacheKey key,
Func<TCacheItem> factory,
Func<DistributedCacheEntryOptions> optionsFactory = null,
bool considerUow = false,
bool? hideErrors = null
);

Expand All @@ -105,13 +115,15 @@ TCacheItem GetOrAdd(
/// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="factory">The factory delegate is used to provide the cache item when no cache item is found for the given <paramref name="key" />.</param>
/// <param name="optionsFactory">The cache options for the factory delegate.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param>
/// <returns>The cache item.</returns>
Task<TCacheItem> GetOrAddAsync(
[NotNull] TCacheKey key,
Func<Task<TCacheItem>> factory,
Func<DistributedCacheEntryOptions> optionsFactory = null,
bool considerUow = false,
bool? hideErrors = null,
CancellationToken token = default
);
Expand All @@ -122,11 +134,13 @@ Task<TCacheItem> GetOrAddAsync(
/// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="value">The cache item value to set in the cache.</param>
/// <param name="options">The cache options for the value.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
void Set(
TCacheKey key,
TCacheItem value,
DistributedCacheEntryOptions options = null,
bool considerUow = false,
bool? hideErrors = null
);

Expand All @@ -136,13 +150,15 @@ void Set(
/// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="value">The cache item value to set in the cache.</param>
/// <param name="options">The cache options for the value.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param>
/// <returns>The <see cref="T:System.Threading.Tasks.Task" /> indicating that the operation is asynchronous.</returns>
Task SetAsync(
[NotNull] TCacheKey key,
[NotNull] TCacheItem value,
[CanBeNull] DistributedCacheEntryOptions options = null,
bool considerUow = false,
bool? hideErrors = null,
CancellationToken token = default
);
Expand All @@ -153,25 +169,29 @@ Task SetAsync(
/// </summary>
/// <param name="items">Items to set on the cache</param>
/// <param name="options">The cache options for the value.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
void SetMany(
IEnumerable<KeyValuePair<TCacheKey, TCacheItem>> items,
DistributedCacheEntryOptions options = null,
bool considerUow = false,
bool? hideErrors = null
);

/// <summary>
/// Sets multiple cache items.
/// Based on the implementation, this can be more efficient than setting multiple items individually.
/// </summary>
/// <param name="items">Items to set on the cache</param>
/// <param name="options">The cache options for the value.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param>
/// <returns>The <see cref="T:System.Threading.Tasks.Task" /> indicating that the operation is asynchronous.</returns>
Task SetManyAsync(
IEnumerable<KeyValuePair<TCacheKey, TCacheItem>> items,
DistributedCacheEntryOptions options = null,
bool considerUow = false,
bool? hideErrors = null,
CancellationToken token = default
);
Expand Down Expand Up @@ -203,21 +223,25 @@ Task RefreshAsync(
/// Removes the cache item for given key from cache.
/// </summary>
/// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
void Remove(
TCacheKey key,
bool considerUow = false,
bool? hideErrors = null
);

/// <summary>
/// Removes the cache item for given key from cache.
/// </summary>
/// <param name="key">The key of cached item to be retrieved from the cache.</param>
/// <param name="considerUow">This will store the cache in the current unit of work until the end of the current unit of work does not really affect the cache.</param>
/// <param name="hideErrors">Indicates to throw or hide the exceptions for the distributed cache.</param>
/// <param name="token">The <see cref="T:System.Threading.CancellationToken" /> for the task.</param>
/// <returns>The <see cref="T:System.Threading.Tasks.Task" /> indicating that the operation is asynchronous.</returns>
Task RemoveAsync(
TCacheKey key,
bool considerUow = false,
bool? hideErrors = null,
CancellationToken token = default
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;

namespace Volo.Abp.Caching
{
[Serializable]
public class UnitOfWorkCacheItem<TValue>
where TValue : class
{
public bool IsRemoved { get; set; }

public TValue Value { get; set; }

public UnitOfWorkCacheItem()
{

}

public UnitOfWorkCacheItem(TValue value)
{
Value = value;
}

public UnitOfWorkCacheItem(TValue value, bool isRemoved)
{
Value = value;
IsRemoved = isRemoved;
}

public UnitOfWorkCacheItem<TValue> SetValue(TValue value)
{
Value = value;
IsRemoved = false;
return this;
}

public UnitOfWorkCacheItem<TValue> RemoveValue()
{
Value = null;
IsRemoved = true;
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Volo.Abp.Caching
{
public static class UnitOfWorkCacheItemExtensions
{
public static TValue GetUnRemovedValueOrNull<TValue>(this UnitOfWorkCacheItem<TValue> item)
where TValue : class
{
return item != null && !item.IsRemoved ? item.Value : null;
}
}
}
4 changes: 2 additions & 2 deletions framework/src/Volo.Abp.Uow/Volo/Abp/Uow/UnitOfWork.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
Expand Down Expand Up @@ -316,4 +316,4 @@ public override string ToString()
return $"[UnitOfWork {Id}]";
}
}
}
}
45 changes: 43 additions & 2 deletions framework/src/Volo.Abp.Uow/Volo/Abp/Uow/UnitOfWorkExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;

namespace Volo.Abp.Uow
{
Expand All @@ -10,5 +13,43 @@ public static bool IsReservedFor([NotNull] this IUnitOfWork unitOfWork, string r

return unitOfWork.IsReserved && unitOfWork.ReservationName == reservationName;
}

public static void AddItem<TValue>([NotNull] this IUnitOfWork unitOfWork, string key, TValue value)
where TValue : class
{
Check.NotNull(unitOfWork, nameof(unitOfWork));

if (!unitOfWork.Items.ContainsKey(key))
{
unitOfWork.Items[key] = value;
}
else
{
unitOfWork.Items.Add(key, value);
}
}

public static TValue GetItemOrDefault<TValue>([NotNull] this IUnitOfWork unitOfWork, string key)
where TValue : class
{
Check.NotNull(unitOfWork, nameof(unitOfWork));

return unitOfWork.Items.FirstOrDefault(x => x.Key == key).As<TValue>();
}

public static TValue GetOrAddItem<TValue>([NotNull] this IUnitOfWork unitOfWork, string key, Func<string, TValue> factory)
where TValue : class
{
Check.NotNull(unitOfWork, nameof(unitOfWork));

return unitOfWork.Items.GetOrAdd(key, factory).As<TValue>();
}

public static void RemoveItem([NotNull] this IUnitOfWork unitOfWork, string key)
{
Check.NotNull(unitOfWork, nameof(unitOfWork));

unitOfWork.Items.RemoveAll(x => x.Key == key);
}
}
}
}
Loading

0 comments on commit f8a251a

Please sign in to comment.