/
Cache-Aside Pattern
Cache-Aside Pattern
Problem
Repeated access to info held in a data store.
Cached data to be consistent with data in the data store.
Solution
Caching systems provide read-through and write-through/write-behind operations.
Determine whether the item is currently held in the cache.
If the item is not currently in the cache, read the item from the data store.
Store a copy of the item in the cache.
Caching in practice
private static ConnectionMultiplexer Connection;
// Redis connection string information
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
string cacheConnection = ConfigurationManager.AppSettings["CacheConnection"].ToString();
return ConnectionMultiplexer.Connect(cacheConnection);
});
public static ConnectionMultiplexer Connection => lazyConnection.Value;
If we need to invalidate an object in the cache when the value is changed by the app. → We need to update the original data and then remove the cached item from the cache.
public async Task UpdateEntityAsync(MyEntity entity)
{
// Update the object in the original data store.
await this.store.UpdateEntityAsync(entity).ConfigureAwait(false);
// Invalidate the current cache object.
var cache = Connection.GetDatabase();
var id = entity.Id;
var key = $"MyEntity:{id}"; // The key for the cached object.
await cache.KeyDeleteAsync(key).ConfigureAwait(false); // Delete this key from the cache.
}
, multiple selections available,
Related content
Saga Pattern
Saga Pattern
More like this
CQRS Pattern
CQRS Pattern
More like this
External Configuration Store Pattern
External Configuration Store Pattern
More like this
Retry Pattern
Retry Pattern
More like this
How to manage connections ?
How to manage connections ?
More like this
Competing Consumers Pattern
Competing Consumers Pattern
More like this