IDistributedCache Interface
All NoSQL server types provide a similar interface like “store this value for this key”, “give me value corresponding to this key” etc.
Serenity provides its distributed cache support through a common interface to not depend on a specific kind of NoSQL database:
First overload of Set method that takes key and value arguments is used to store a key/value pair in distributed cache.
If we wanted to keep some value for a predetermined duration, we could use the second overload of Get method:
TimeSpan.FromMinutes(10));
Operation on distributed cache systems are usually not atomic and they provide no transactional systems at all.
Same key value can be changed by multiple servers at same time and override each others value in random order.
Such a code block won’t function as expected. Inside the duration between reading value (get) and setting it to increment LastID
value (set), another server might have read the same LastID value. Thus two servers could use same ID value.
For this purpose, you can use Increment method:
int GetTheNextIDValue()
return IoC.Resolve<IDistributedCache>().Increment("LastID");
}
Increment function acts just like Interlocked.Increment method that is used in thread synchronization. It increases an identity value but blocks other requests while doing it, and returns the incremented value. So even if two WEB servers incremented same key in exact same moment, they end up with different ID values.