DEV Community

Discussion on: Distributed Caching in ASP.NET Core with Redis

Collapse
 
turnerj profile image
James Turner

Nice article! I've been doing a bunch of caching work recently and one thing to keep in mind is distributed locking. Helps avoid unnecessary pressure on the systems that are filling the cache.

In Cache Tower, I use the presence of a key already being set to see whether I should call the factory method or not:

var hasLock = await Database.StringSetAsync(lockKey, RedisValue.EmptyString, expiry: Options.LockTimeout, when: When.NotExists);
Enter fullscreen mode Exit fullscreen mode

Once the new value is cached, I use the pub/sub feature of Redis to inform the instances of the application that the cache is refreshed. This then allows those waiting requests to just ask Redis for the new value rather than process it themselves.

As an aside, noticed you're from Adelaide too - Hi! 👋