Sponsorship is available! If you’d like to sponsor CodeOpinion.com and have your product or service advertised exclusively (no AdSense) on every post, contact me.I discovered the Orleans Smart Cache Pattern by listening to a talk by John Azariah and Sergey Bykov. The idea is that you can use Orleans as a distributed cache in front of (permanent) storage. This is really ideal if you have a read heavy system, which most are. You could optionally also choose to buffer your writes when you make state changes. Ultimately you will reduce load on your storage by accessing data/state from memory.
Blog Post Series:
- Part 1 – Practical Orleans
- Part 2 – Grains and Silos
- Part 3 – Smart Cache Pattern
- Part 4 – Event Sourced Grain
- Part 5 – EventStore for Grain Persistence
State Holder GrainThe idea is we are going to create a Grain that will be used for holding/containing our state. There are just two methods, setting and getting our state as defined in
ImplementationFor this example, I’m creating a POCO
CustomerStateto hold some customer data. Then create a
ICustomerGrainthat just extends
IStateHolderGrainwhere T is our POCO
CustomerState. Our concrete class
CustomerGrainthat implements that new
ICustomerGrain. Lastly I created a CustomerStateService which just contains some static methods that create and retrieve our POCO.
UsageAs per my previous post, I’m using ASP.NET Core with Botwin Middleware for a route endpoint. From here I can use the
CustomerStateServiceto create and fetch out our data.