Cloud Computing Design Mode (I)-Cache reservation Mode
Load data from the data storage cache as needed. This mode improves performance and helps maintain consistency between data stored in the basic data stored in the cache.
Background and Problems
Applications use the high-speed cache to optimize the information maintained in the data storage for repeated access. However, it is generally impractical to expect that the cached data will always be exactly the same as the data in the data storage. An application must implement a policy to ensure that the data in the cache is up-to-date, as long as possible, but can also be detected and processed, when the data in the cache has become obsolete.
Solution
Many commercial cache systems provide read-only and write-only/post-write operations. In these systems, applications retrieve data by referencing the cache. If the data is not in the cache, it is transparently retrieved from the data storage and added to the cache. Any data maintained by modifications in the cache is automatically written to the data storage zone and.
If this function is not provided for the cache, it is the responsibility of the application that uses the data cached in the cache.
An application can implement the read-type high-speed cache simulation feature of the cache reservation strategy. This policy effectively caches the data to be loaded. Figure 1 summarizes the steps in this process.
Figure 1-use cache-Save the pattern to store data in High-Speed Buffer Storage
If an application updates the information, it can simulate the write-through policy as follows:
1. Modify to Data Storage
2. items corresponding to void are in the cache.
When this project is next required, you can use the cache reservation policy to retrieve and re-add data from the data storage to the cache for updating data.
Problems and precautions
Consider the following when deciding how to implement this mode:
? The lifecycle of the cached data. Many caches implement an expiration Policy, which results in invalid data and is removed from the cache if it is not accessed within the specified time. It is effective for the cache side, ensuring that the expiration Policy matches the access mode of the application that uses data. Do not make the validity period too short, because this will cause the application to constantly retrieve data from the data storage and add it to the cache. Similarly, do not make the cache data obsolete if the shelf life is so long. Remember, cache is the most effective and relatively static data, or data is frequently read.
? Data eviction. The highest-speed cache has only a limited size than the data storage zone from where the data source is located, and the data will be reclaimed when necessary. Most caches adopt the policy that is least recently used to choose Project eviction, but this may be customized. Configure the global expiration attribute and other performance of the cache, And the expiration attribute of each cache item to help ensure the cache cost efficiency. It may not always be suitable for applications in high-speed cache with a global eviction policy for every project. For example, if cache items are very expensive and retrieved from data storage, it is retained in the cache of this product for more frequently accessed but less expensive items.
? Perfusion cache. In many solutions, pre-filled applications may need to cache data as part of startup processing. If some data has expired, the evicted cache may still be useful except for the pattern.
? Consistency. In addition to the pattern, the execution cache does not guarantee consistency between data storage and high-speed cache. Projects in data storage can be changed externally at any time. Such changes may not be reflected in projects in the cache being loaded into the cache until the next time. In a system, the whole data storage copies data. If synchronization occurs frequently, this problem may become particularly prominent.
? Local (memory) cache. The cache can be a local application instance and stored in the memory. Cache reservation can be useful in this environment if the application accesses the same data for multiple times. However, the local cache is private, so different application instances can each have a copy of the same cached data. This data may quickly become inconsistent between caches, so it may be necessary to refresh the data stored in the expired dedicated cache and more frequently. In these scenarios, it may be appropriate to investigate whether a shared or distributed cache mechanism is used.
When this mode is used
When using this mode:
? The cache does not support native read and write operations.
? Resource requirements are unpredictable. This mode enables applications to load data as needed. It enables any hypothetical data-related application to be advanced.
This mode may not be suitable:
? When the cached dataset is static. If the data will be suitable for available high-speed cache space, the first data in the high-speed cache will be started and applied to prevent data from being stopped.
? Cache session status information for Web applications hosted in the Web farm. In this environment, you should avoid introducing dependencies based on client-server relationships.
Example
In Microsoft Azure, you can use azure cache to create a distributed cache, which can be shared by multiple instances of an application. The getmyentityasync method in the following code example provides the implementation of the cache backup mode based on azure. This method caches objects by using the read method.
One purpose is to determine that an integer ID is used as the key. The getmyentityasync method generates a string value based on this key (the key value string used in the azure cache API) and tries to retrieve this key item from the cache. If a matched project is found, it is returned. If no match exists in the cache, The getmyentityasync method adds the object from a data storage to the cache, then return it (that is, the code for actually obtaining data from the data storage has been omitted because it is a data storage dependency ). Note that the cache item is configured to prevent it from becoming obsolete if it is updated elsewhere.
private DataCache cache;...public async Task<MyEntity> GetMyEntityAsync(int id){ // Define a unique key for this method and its parameters. var key = string.Format("StoreWithCache_GetAsync_{0}", id); var expiration = TimeSpan.FromMinutes(3); bool cacheException = false; try { // Try to get the entity from the cache. var cacheItem = cache.GetCacheItem(key); if (cacheItem != null) { return cacheItem.Value as MyEntity; } } catch (DataCacheException) { // If there is a cache related issue, raise an exception // and avoid using the cache for the rest of the call. cacheException = true; } // If there is a cache miss, get the entity from the original store and cache it. // Code has been omitted because it is data store dependent. var entity = ...; if (!cacheException) { try { // Avoid caching a null value. if (entity != null) { // Put the item in the cache with a custom expiration time that // depends on how critical it might be to have stale data. cache.Put(key, entity, timeout: expiration); } } catch (DataCacheException) { // If there is a cache related issue, ignore it // and just return the entity. } } return entity;}
Note:
This example uses the azure cache API to access the cache information stored and retrieved. For more information about azure cache APIs, see using Microsoft Azure cache on msdn.
The updateentityasync method shown below shows how to invalidate an object in the cache when the value is changed by the application. This is an example of a write-through method. This code updates the original data storage and then specifies the key by calling the Remove Method (the code for this function has been omitted because it will be related to data storage) deletes a cache entry from the cache.
Note:
The sequence of steps in this sequence is important. If the previously updated items in the cache are deleted, the data obtained before the items in the data storage in the client application (because it is not found in the cache) has Changed a small window to include expired data in the cache.
public async Task UpdateEntityAsync(MyEntity entity){ // Update the object in the original data store await this.store.UpdateEntityAsync(entity).ConfigureAwait(false); // Get the correct key for the cached object. var key = this.GetAsyncCacheKey(entity.Id); // Then, invalidate the current cache object this.cache.Remove(key);}private string GetAsyncCacheKey(int objectId){ return string.Format("StoreWithCache_GetAsync_{0}", objectId);}
Msdn: http://msdn.microsoft.com/en-us/library/dn589799.aspx
Cloud Computing Design Mode (I)-Cache reservation Mode