Provides a means for a named cache to interact with a backing store (such as a database) to perform read-through, write-though, refresh-ahead,
and write-behind operations.
Namespace: Soss.Client
Assembly: soss_namedcache (in soss_namedcache.dll) Version: 6.2.0.0
Syntax
The IBackingStore type exposes the following members.
Methods
Name | Description | |
---|---|---|
Erase |
Removes an object from the backing store.
| |
GetCreatePolicy |
Provides a policy object to be used when a Load(CachedObjectId) operation inserts an object into the named cache. If the method returns
null then the named cache's DefaultCreatePolicy will be used.
| |
Load |
Loads an object from the backing store. Returns null if there is no value in the backing store for the specified id.
| |
Store |
Persists an object to the backing store.
|
Remarks
Error handling in this example has been omitted for brevity, but production code should catch and log exceptions. Also, consider subscribing to the EventDeliveryException event to be notified of low-level errors than may occur prior to the invocation of an IBackingStore method and otherwise go unreported (such as an object deserialization error in a write-behind event).
Examples
using System; using System.Data.SqlClient; using Soss.Client; class Program { // Configures a named cache to use read-through operations to automatically populate a cache and // refresh-ahead operations to refresh its contents every 5 seconds. static void Main(string[] args) { NamedCache priceCache = CacheFactory.GetCache("Prices"); // Configure the "Prices" cache for backing store support by setting its DefaultCreatePolicy and then // associating an IBackingStore instance with the named cache using SetBackingStoreAdapter(). This only // needs to be done at the application's startup--subsequent calls to CacheFactory.GetCache("Prices") // will return the same NamedCache instance with the same backing store configuration. priceCache.DefaultCreatePolicy.BackingStoreMode = BackingStoreAsyncPolicy.RefreshAhead; priceCache.DefaultCreatePolicy.BackingStoreInterval = TimeSpan.FromSeconds(5); BackingStoreAdapter adapter = new BackingStoreAdapter(); priceCache.SetBackingStoreAdapter(adapter, new BackingStorePolicy(true, true, false)); // Retrieve an object from the cache. Read-through will transparently retrieve it from the DB: decimal toyPrice = (decimal)priceCache["TickleMeElmo"]; Console.WriteLine(toyPrice); Console.WriteLine("Hit enter to stop."); Console.ReadLine(); } } class BackingStoreAdapter : IBackingStore { const string connString = "Data Source=localhost;Initial Catalog=BackingStoreSample;Integrated Security=true"; public object Load(CachedObjectId id) { Console.WriteLine("Loading/refreshing {0} price in cache.", id.GetStringKey()); string cmdText = string.Format("SELECT Price FROM PriceList WHERE ProductId = '{0}'", id.GetStringKey()); SqlConnection conn = new SqlConnection(connString); SqlCommand selectCommand = new SqlCommand(cmdText, conn); conn.Open(); decimal price = (decimal)selectCommand.ExecuteScalar(); conn.Close(); return price; } public void Store(CachedObjectId id, object value) { // The value parameter is the price value being stored in the cache. string cmdText = string.Format("UPDATE PriceList SET Price = {0} WHERE ProductId = '{1}'", value.ToString(), id.GetStringKey()); SqlConnection conn = new SqlConnection(connString); SqlCommand updateCommand = new SqlCommand(cmdText, conn); conn.Open(); updateCommand.ExecuteNonQuery(); conn.Close(); } public void Erase(CachedObjectId id) { // This Erase method won't ever be invoked because we haven't enabled write-through or write-behind. It's defined // here for illustrative purposes. string cmdText = string.Format("DELETE FROM PriceList WHERE ProductId = '{0}'", id.GetStringKey()); SqlConnection conn = new SqlConnection(connString); SqlCommand delCommand = new SqlCommand(cmdText, conn); conn.Open(); delCommand.ExecuteNonQuery(); conn.Close(); } public CreatePolicy GetCreatePolicy(CachedObjectId id) { // Returning null causes read-through to use the NamedCache's default create policy when // adding the object to the cache. return null; } }
See Also