-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
[API Proposal]: IDistributedCache overloads with ReadOnlyMemory<byte> support #70137
Comments
Tagging subscribers to this area: @dotnet/area-extensions-caching Issue DetailsBackground and motivation
API ProposalAdditions to void Set(string key, ReadOnlyMemory<byte> value, DistributedCacheEntryOptions options);
Task SetAsync(string key, ReadOnlyMemory<byte> value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken)); API Usagepublic static async Task Cache(string key, string looongString, IDistributedCache cache, DistributedCacheEntryOptions options)
{
var maxLength = Encoding.UTF8.GetMaxByteCount(looongString.Length);
var pooled = ArrayPool<byte>.Shared.Rent(maxLength);
try
{
var bytesWritten = Encoding.UTF8.GetBytes(looongString, pooled);
await cache.SetAsync(key, pooled.AsMemory(0, bytesWritten), options);
}
finally
{
ArrayPool<byte>.Shared.Return(pooled);
}
} Alternative DesignsNo response RisksNo response
|
Is there a reason this has to be |
Because you can't use In theory the synchronous version could still use the |
I would not mind contributing this change if approved and within the .NET 7 timeframe. I assume it'll just involve adding the methods and updating any surface area tests. Then I don't know how such changes flow downstream, among others, to the ASP.NET repo, where the Redis implementation will have to be updated because the build will break as soon as the new interface is imported.
StackeExchange.Redis concerns me personally the most, and since |
I think that is a good reason to leave the sync version using
We won't be accepting a breaking change. Instead, the new methods would have to be "DIM"s - https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/default-interface-methods-versions. |
Any update on this? Who would need to approve this @eerhardt ? What are the next steps? ASP.NET Core spend a lot of effort over the years avoiding even small allocations on each request. IDistributedCache is often used as a session store which is used on each request on many websites, often in combination with relatively large chunks of data and things like serialization. So it seems very worthwhile to be able to avoid unecessary allocations/copies in this common scenario. |
@davidfowl I can see that most of the implementations of this interface are provided by the ASP.NET Team. Whom should we ask for feedback about extending that interface with the proposed methods? |
Update: see |
Is there a reason for this proposal to still exist with the creation of the new |
The For application code, @julealgon is correct that |
Background and motivation
IDistributedCache.Set
andSetAsync
only take a byte array as the input. This is not ideal when working with oversized pooled arrays for example, because you need to reallocate the data out into a fixed-size array just to pass it into the cache. Having overloads withReadOnlyMemory<byte>
parameters would alleviate this issue (supposing the actual caching implementation itself supportsMemory
, which StackExchange.Redis does).API Proposal
Additions to
Microsoft.Extensions.Caching.Distributed.IDistributedCache
:API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: