Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more "Many" methods to the ICacheSupportsMultipleItems #7523

Merged
merged 4 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions docs/en/Caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,15 @@ In addition, all of the `IDistributedCache<TCacheItem>` (and `IDistributedCache<

## Batch Operations

ABP's distributed cache interfaces provide methods to perform batch get/set methods those improves the performance when you want to get or set multiple cache items in a single method call.
ABP's distributed cache interfaces provide methods to perform batch methods those improves the performance when you want to batch operation multiple cache items in a single method call.

* `SetManyAsync` and `SetMany` methods can be used to set multiple values to the cache.
* `GetManyAsync` and `GetMany` methods can be used to retrieve multiple values from the cache.
* `GetOrAddManyAsync` and `GetOrAddMany` methods can be used to retrieve multiple values and set missing values from the cache
* `RefreshManyAsync` and `RefreshMany` methods can be used to resets the sliding expiration timeout of multiple values from the cache
* `RemoveManyAsync` and `RemoveMany` methods can be used to remove multiple values from the cache

> These are not standard methods of the ASP.NET Core caching. So, some providers may not support them. They are supported by the [ABP Redis Cache integration package](Redis-Cache.md). If the provider doesn't support, it fallbacks to `SetAsync` and `GetAsync` methods (called once for each item).
> These are not standard methods of the ASP.NET Core caching. So, some providers may not support them. They are supported by the [ABP Redis Cache integration package](Redis-Cache.md). If the provider doesn't support, it fallbacks to `SetAsync` and `GetAsync` ... methods (called once for each item).

## Advanced Topics

Expand Down
12 changes: 12 additions & 0 deletions docs/zh-Hans/Caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ public class BookService : ITransientDependency
}
````

## 批量操作

ABP的分布式缓存接口定义了以下批量操作方法,当你需要在一个方法中调用多次缓存操作时,这些方法可以提高性能

* `SetManyAsync` 和 `SetMany` 方法可以用来设置多个值.
* `GetManyAsync` 和 `GetMany` 方法可以用来从缓存中获取多个值.
* `GetOrAddManyAsync` 和 `GetOrAddMany` 方法可以用来从缓存中获取并添加缺少的值.
* `RefreshManyAsync` 和 `RefreshMany` 方法可以来用重置多个值的滚动过期时间.
* `RemoveManyAsync` 和 `RemoveMany` 方法呆以用来删除多个值.

> 这些不是标准的ASP.NET Core缓存方法, 所以某些提供程序可能不支持. [ABP Redis集成包](Redis-Cache.md)实现了它们. 如果提供程序不支持,会回退到 `SetAsync` 和 `GetAsync` ... 方法(循环调用).

### DistributedCacheOptions

TODO
1 change: 1 addition & 0 deletions docs/zh-Hans/Redis-Cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO...
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,26 @@ static AbpRedisCache()

MapMetadataMethod = type.GetMethod("MapMetadata", BindingFlags.Instance | BindingFlags.NonPublic);

GetAbsoluteExpirationMethod = type.GetMethod("GetAbsoluteExpiration", BindingFlags.Static | BindingFlags.NonPublic);
GetAbsoluteExpirationMethod =
type.GetMethod("GetAbsoluteExpiration", BindingFlags.Static | BindingFlags.NonPublic);

GetExpirationInSecondsMethod = type.GetMethod("GetExpirationInSeconds", BindingFlags.Static | BindingFlags.NonPublic);
GetExpirationInSecondsMethod =
type.GetMethod("GetExpirationInSeconds", BindingFlags.Static | BindingFlags.NonPublic);

SetScript = type.GetField("SetScript", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null).ToString();
SetScript = type.GetField("SetScript", BindingFlags.Static | BindingFlags.NonPublic)?.GetValue(null)
.ToString();

AbsoluteExpirationKey = type.GetField("AbsoluteExpirationKey", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null).ToString();
AbsoluteExpirationKey = type.GetField("AbsoluteExpirationKey", BindingFlags.Static | BindingFlags.NonPublic)
?.GetValue(null).ToString();

SlidingExpirationKey = type.GetField("SlidingExpirationKey", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null).ToString();
SlidingExpirationKey = type.GetField("SlidingExpirationKey", BindingFlags.Static | BindingFlags.NonPublic)
?.GetValue(null).ToString();

DataKey = type.GetField("DataKey", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null).ToString();
DataKey = type.GetField("DataKey", BindingFlags.Static | BindingFlags.NonPublic)?.GetValue(null).ToString();

NotPresent = type.GetField("NotPresent", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null).To<int>();
// ReSharper disable once PossibleNullReferenceException
NotPresent = type.GetField("NotPresent", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null)
.To<int>();
}

public AbpRedisCache(IOptions<RedisCacheOptions> optionsAccessor)
Expand Down Expand Up @@ -124,6 +131,42 @@ public async Task SetManyAsync(
await Task.WhenAll(PipelineSetMany(items, options));
}

public void RefreshMany(
IEnumerable<string> keys)
{
keys = Check.NotNull(keys, nameof(keys));

GetAndRefreshMany(keys, false);
}

public async Task RefreshManyAsync(
IEnumerable<string> keys,
CancellationToken token = default)
{
keys = Check.NotNull(keys, nameof(keys));

await GetAndRefreshManyAsync(keys, false, token);
}

public void RemoveMany(IEnumerable<string> keys)
{
keys = Check.NotNull(keys, nameof(keys));

Connect();

RedisDatabase.KeyDelete(keys.Select(key => (RedisKey)(Instance + key)).ToArray());
}

public async Task RemoveManyAsync(IEnumerable<string> keys, CancellationToken token = default)
{
keys = Check.NotNull(keys, nameof(keys));

token.ThrowIfCancellationRequested();
await ConnectAsync(token);

await RedisDatabase.KeyDeleteAsync(keys.Select(key => (RedisKey)(Instance + key)).ToArray());
}

protected virtual byte[][] GetAndRefreshMany(
IEnumerable<string> keys,
bool getData)
Expand Down
Loading