Implementing Caching in the Stock Trader: The Position Module
Retired Content |
---|
This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. |
The latest Enterprise Library information can be found at the Enterprise Library site. |
On this page: |
---|
Scenario | Design of the Position Module | Caching Values to Isolated Storage | Proactive Caching | Asking the User for More Space in Isolated Storage |
In this chapter, we'll discuss how caching was implemented in the Stock Trader V2 application. To improve the startup performance, the position module stores the user's position and their market history in isolated storage. At the end of this chapter, we'll discuss how the news module uses interception to transparently cache retrieved news articles to the in-memory cache.
Ed Says: | |
---|---|
|
Scenario
In the position summary view, the users get an overview of the stocks they own, their current market value, and their cost basis. When the user selects a particular stock, a graph displays the market history in a trend line and related news articles are displayed.
Sharon Says: | |
---|---|
|
Design of the Position Module
The following diagram shows a schematic design of the position module.
The position view, with its associated position view model, displays a list of the current account positions, with their current market values. The current account position information from the Position Service Agent and the current market value information from the Market Service Agent is aggregated and continuously updated in the ObservablePosition class.
The two service agents isolate the view models from all the details about how the particular information is being retrieved.
Sharon Says: | |
---|---|
|
Caching Values to Isolated Storage
The position service agent will cache requests for the current account position to isolated storage. Once this information is cached, the application would start much faster. Since the current account position data also contains the market value on the last closing day, we decided that we wanted to invalidate the cache at the start of the next trading day.
Proactive Caching
When the user selects a particular stock, the position view model will notify the market history view model to display the information for that particular stock. The market service agent will then asynchronously retrieve the information from the service. The market history only needs to be updated once a day, so by storing this information in isolated storage, the users can view the market history much faster after starting the application.
Ed Says: | |
---|---|
|
To solve this problem, but also to improve the performance of the trend line, a proactive caching implementation was developed. This means that the market history information would be downloaded and added to the cache on a background thread. When the user selects a particular stock and the market history is not yet downloaded for this stock, the market history for this stock will be downloaded as soon as possible.
The following diagram shows how the proactive cache works:
At the core of the proactive cache is the DownloadRequest class. This download request has two actions: The StartDownloadAction, which will start the download if you call it. It can also have a DownloadCompleteCallback, which will be called when the download has completed. This callback is typically used by the user interface to detect that the download has been completed and that the data can be displayed.
The download request also has a CacheKey property. Before starting the download, the ProactiveCache will check if an item with that CacheKey is already present in the cache. If so, it will return the cached item directly. If the item with that cache key is not found, then the asynchronous download is started. After the download has completed, it will add the downloaded value to the cache under the specified CacheKey so any subsequent calls don't need to download again.
The proactive cache has two public methods: EnqueueDownloadRequest and GetData. The EnqueueDownloadRequest method will add download requests to the download queue, which will be processed by a background thread in the order received. The GetData method will add a download request that will be processed as soon as possible. This method will remember the last request that was added by the GetData method. Only for the last request will the DownloadCompleteCallback be called. This means if the user very quickly selects three different stocks, then the trend line view will only be updated for the last selected stock. The market history for other stocks will eventually be downloaded in the background, so if the user selects them later, the item can be returned from the cache very quickly.
Sharon Says: | |
---|---|
|
Asking the User for More Space in Isolated Storage
When the Stock Trader starts, one of the first actions it performs is to check the isolated storage quota. If the isolated storage quota is set below 10 Mb, it will ask the user to increase it.
In Stock Trader V2, the logic to increase the size of the isolated storage quota is found in the IsolatedStorage service class. This class has methods to query the isolated storage size, and to ask the user for a larger quota. Asking the user for a larger quota must always occur as the result of a button click. In other words, you will need to display a custom message asking the user for additional quota. This is the dialog box that the Stock Trader shows to ask for an increased quota:
If the user agrees, then you can call Silverlight to try to increase the quota. This Silverlight dialog box will ask the user to increase the quota, and if the user agrees, then the quota is increased.
The following code example shows how you can increase the size of the isolated storage quota:
IsolatedStorageFile isolatedStorageFile;
// Use the Quota property to find out what the current quota is.
long quotaInBytes = isolatedStorageFile.Quota;
// Use the IncreaseQuotaTo method to ask the user to increase the quota.
long newQuotaSize = 10*1024*1024; // 10 Mb
bool increased = isolatedStorageFile.IncreaseQuotaTo(newQuotaSize);
Next Topic | Previous Topic | Home
Last built: July 8, 2011