Skip to Content

In this blog, I will be talking about Technical Cache.  As more and more devices are increasingly online, it makes more sense to optimize the server communication for Online Stores.  Yes, you guessed it right.  Technical Cache is only applicable for Online Stores.  This feature is not applicable for Offline Stores.  Technical Cache provides a simple mechanism whereby responses from an online OData request is cached for later use.  This allows the user to now read from the cache instead of making another round trip request to the server thereby optimizing bandwidth utilization.  Additionally an application can use the cache content to improve user experience.  You will find in this blog, how a developer can add the necessary implementation to make use of the Technical Cache.

What is the difference between Technical Cache and Offline Store?

You might be wondering the difference between a Technical Cache and Offline Store.  In an Offline Store, data is persisted locally on the device as well.  So why do we need a Technical Cache?  A lot of mobile applications do not require offline capabilities.  These devices are mostly online and the application itself is not mission critical.  For applications like these, it makes no sense to add offline capabilities since they are more complex.  However, you may still want some kind of persistence that is easy to implement that allows basic functionality when there is no network coverage. Additionally, the cache content can improve user experience as well.  The Technical Cache provides this solution for online applications to somewhat use a persistent store without all the complexities of an Offline Store.  Note that the Technical Cache provides very basic functionality as compared to the Offline Store.  For example, the Technical Cache can only be used to query the cache if the request URL is (literally) identical.

Some keywords and concepts you need to know regarding Technical Cache

When discussing about Technical Cache, you need to understand some keywords and concepts.  I have not yet talked about Offline Stores. I will be talking about Offline Stores in my next blog.  Just know that Technical Cache is not applicable for Offline Stores.  This now leaves us with only the Online Store. When opening an Online Store, you now have the option of enabling the Technical Cache.  For backwards compatibility, the default is disabled.

Assume the Online Store is opened with the Technical Cache enabled.  In this scenario only, the Online Store can switch to passive mode. In all other scenarios, switching to passive mode will result in an exception.

Store

Opening method

Technical Cache

Can switch to Passive Mode

Online Store

UseCache = true

Enabled

Yes

Online Store

UseCache = false (OR)

Not Explicitly set

Disabled

No

Offline Store

Not Applicable

Not Applicable

Not Applicable

So, what is passive mode? In simple terms, during passive mode the Online Store does not make a request to the backend server.  It simply reads from the cache and presents the response to the caller.

Store

Passive Mode

Online Store (cache enabled)

Reads from cache

Online Store (cache disabled)

Not Applicable

If there is passive mode, is there an active mode also?  Yes. In simple terms, during active mode the Online Store makes a request to the backend server.  However, there is a small difference between how an Online Store with cache disabled behaves compared to an Online Store with cache enabled. An Online Store with cache disabled simply makes a request to the backend server.  However, an Online Store with cache enabled in addition to making a request to backend server, also reads from the cache.  It returns both the responses to the caller.  The cache is then updated with the response from the server.

Store

Active Mode

Online Store (cache enabled)

Sends roundtrip request to backend

Also reads from cache

Updates cache with latest server response

Online Store (cache disabled)

Sends roundtrip request to backend

How to open Online Store with Technical Cache enabled?

Opening an Online Store with Technical Cache enabled is fairly straightforward.  The following code snippet enables the Technical Cache in an Online Store.  In addition, values for CacheSize in KB and CacheCleanupPercent can be specified.  CacheSize specifies the maximum size of the cache, while CacheCleanupPercent specifies the percentage of cache content to be cleared when CacheSize is exceeded.

this.Store = new ODataStore(URL, new ODataStore.StoreOptions

{

   UseCache = true,

   EncryptionKey = “encryptionKey”,

   RequestFormat = ODataStore.EntityFormat.Json

});

Making an HTTP request in active mode

Assume we have an Online Store opened with Technical Cache enabled.  The next step is to make an HTTP request to retrieve data from the backend. Making an HTTP request sends a round trip request to backend and also reads from the cache.  The first time an HTTP request is made to a resource, the cache is not available.  A null value indicates a non-existing cache content.

var execution = Store.ScheduleReadEntitySet(collectionName);

// Read from backend

var response = await execution.Response;

   this.EntitySet = (ODataEntitySet)((IODataResponseSingle)response).Payload;

// Read from cache

var cacheResponse = await execution.CacheResponse;

if (cacheResponse != null)

   this.CachedEntitySet = (ODataEntitySet)((IODataResponseSingle)cacheResponse).Payload;

else

   // null indicates non-existing cache – possibly first time

   this.CachedEntitySet = new ODataEntitySet();

After the first request, the cache is now populated with the server response.  Making an HTTP request results in making a roundtrip to the backend and reading from the cache.  If data has not changed in the backend since the last time the cache was updated, the server response and the cache response would be identical.

Note:  The HTTP request should be literally identical for the cache to work.

For example, the initial HTTP request is “Products?$top=5”.

  • The server response returns the top 5 rows.
  • The cache response is null (since it is non-existent).
  • The cache is then populated with the server response (in this case, with the top 5 rows)

Your subsequent HTTP request is “Products?$top=5” (Assume nothing has changed on the backend)

  • The server response returns the top 5 rows again.
  • The cache response now also returns top 5 rows (These rows were cached from the previous request).
  • The cache is now updated with server response (Nothing has changed on backend, so no updates necessary.  If data has changed on the backend, then the cache is now updated with the new values).

You now make another HTTP request.  This time it is “Products?$top=4”.  It is not literally identical.

  • The server response returns the top 4 rows.
  • The cache response is null (since it is non-existent).
  • The cache is then populated with the server response (and associated with this request).
  • The cache now contains values for 2 requests (“Products?$top=5” and “Products?$top=4”)

Making an HTTP request in passive mode

Assume that network connection is lost and you cannot make a roundtrip to the backend.  So you only want to read from the cache.  The first step is putting the Online Store in passive mode.  This is accomplished by the following snippet of code.

this.Store.IsPassive = true;

When the Online Store is in passive mode, request to the backend server is not made.  It only reads from the cache.

var execution = Store.ScheduleReadEntitySet(collectionName);

// Read from cache only – Online Store is in passive mode !

var cacheResponse = await execution.CacheResponse;

if (cacheResponse != null)

   this.CachedEntitySet = (ODataEntitySet)((IODataResponseSingle)cacheResponse).Payload;

else

   // null indicates non-existing cache – possibly first time

   this.CachedEntitySet = new ODataEntitySet();

The cache content is now returned to the caller.  Reading from the cache is much faster than making a roundtrip to the backend.  So even when network connection is present, you can switch the Online Store to passive mode and read from the cache (as long as the application does not require the latest data).

Can Technical Cache work with delta tokens?

Technical Cache can also work with delta tokens.  The SMP SDK is smart enough to update the cache based on the delta feed from the server response.  This is incredibly cool, because now you can utilize the power of delta tokens and also the handy feature of Technical Cache to optimize your online application.

Technical Cache is a pretty handy feature to improve performance and provide very basic offline functionality to an online application.  In the next blog, I will be talking about features of an offline application and how to implement an offline application.

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

Leave a Reply