Вопрос

I'm considering implementing some distributed cache clients (Redis & Memcached) through the ObjectCache class. One thing I noticed is that all the methods on this abstract class are synchronous, meaning none of them return Tasks. Since the calls to get or put thing in the distributed cache will be off the local machine and include a network trip, I'd like to use Async versions of these operations. So is there a standard cache class in the .Net framework, like CacheObject that supports Async patterns?

Это было полезно?

Решение

Although there is a AsyncCache in the parallel extensions package mentioned by @TrevorBrooks, it didn't work the way I expected. I needed to put/get/remove items from a distributed cache with a Task that could be awaited. So I ended up creating the following interface:

public interface IAsyncCache<T>
{
    Task Initialize(Dictionary<string, string> parameters);
    Task Put(string key, T value, TimeSpan lifeSpan);
    Task<T> Get(string key);
    Task Remove(string key);
}

I have since been able to implement that interface against several cache providers that offer Task/Async version for their get/put/remove methods and its working well.

I posted this code on github.

Другие советы

Use the AsyncCache class included with ParallelExtensionsExtras NuGet package:

http://www.nuget.org/packages/MSFT.ParallelExtensionsExtras/1.2.0

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top