Open sandboxFocusImprove this doc

Caching

Caching is a great way to improve the latency of an application. Traditionally, when you implement caching, you need to play with the API of the caching framework (such as MemoryCache) or caching server (such as Redis) and to include moderately complex logic in your source code to generate the cache key, check the existence of the item in the cache, and add the item into the cache. Another source of complexity stems from removing items from the cache when the source data is updated. Implementing caching manually is time-consuming and error-prone: it is easy to generate inconsistent cache keys between read and update methods.

PostSharp allows you to dramatically reduce the complexity of caching. It allows you to cache the return value of a method as a function of its arguments with just a custom attribute, namely the CacheAttribute aspect. The InvalidateCacheAttribute aspect and the CachingServices.Invalidation API offer a strongly-typed way to invalidate cached methods. Additionally, PostSharp is independent from the caching framework or server (called caching backend), so you can choose from several backends or implement an adapter for your own backend.

In this chapter

Section Description
Caching Method Return Values This article shows how to cache the return values of methods.
Troubleshooting Caching This article shows how to enable logging of the caching aspect to troubleshoot it.
Removing Items From the Cache This article shows how to invalidate cached return values of methods declaratively and imperatively.
Working with Cache Dependencies This article shows how to invalidate cache items automatically using cache dependencies.
Customizing Cache Keys This article shows how to customize the cache keys that identify cached method return values.
Caching Backends This article shows how to store cached values in various backends.
Synchronizing Local In-Memory Caches for Multiple Servers This article shows how to invalidate all related in-memory caches in a distributed environment.
Caching Special Types with Value Adapters This article describes how to cache return values of methods that cannot be cached directly, such as instances of IEnumerable<T> or Stream.
Preventing Concurrent Execution of Cached Methods This article explains how you can prevent the same method from being executed with the same arguments at the same time by using locking.

See Also

Reference

CacheAttribute
CacheConfigurationAttribute
InvalidateCacheAttribute