Introduction to the Caching Application Block
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.|
This topic includes a series of brief sections that provide information to help you decide whether the Caching Application Block is suitable for your requirements. This topic includes the following sections:
- Common Scenarios
- Example Application Code
- Highlights of the Caching Application Block
- Determining When to Use the Caching Application Block
- Alternatives to Using the Caching Application Block
In addition to this introductory material, the documentation also contains the following topics:
- Developing Applications Using the Caching Application Block. This topic first explains how to configure the Caching Application Block and add it to your application. It then explains how to select a backing store.
- Key Scenarios. This section demonstrates how to use the application block to perform typical caching operations.
- Design of the Caching Application Block. This topic explains the decisions that went into designing the application block and the rationale behind those decisions.
- Extending and Modifying the Caching Application Block. This topic explains how to extend the application block by adding your own backing store and your own expiration policies. It also explains how to modify it by changing the source code.
- Deployment and Operations. This topic explains how to deploy and update the application block assemblies.
- Caching QuickStart. This topic explains how to install and configure the QuickStart application and contains a series of walkthroughs that demonstrate how to incorporate common caching operations into an application.
For details of the system requirements for the Caching Application Block, see System Requirements. For details of the dependencies for the Caching Application Block, see Application Block Dependencies.
The Caching Application Block is suitable if you encounter any of the following situations:
- You must repeatedly access static data or data that rarely changes.
- Data access is expensive in terms of creation, access, or transportation.
- Data must always be available, even when the source, such as a server, is not available.
You can use the Caching Application Block with any of the following application types:
- Windows Forms
- Console application
- Windows service
- COM+ server
- ASP.NET Web application or Web service if you need features not included in the ASP.NET cache
You should deploy the Caching Application Block within a single application domain. Each application domain can have one or multiple caches, either with or without backing stores. Caches cannot be shared among different application domains.
The Caching Application Block is optimized for performance and is both thread safe and exception safe. You can extend it to include your own expiration policies and your own backing store.
The following code shows how to add an item to a cache and retrieve an item from the cache. It creates an object of type Product and then adds it to the cache with a scavenging priority of 2, an instruction not to refresh the item if it expires, and an expiration date of 5 minutes from the last time the item was accessed.
|The code does not include the Product class definition.|
ICacheManager productsCache = CacheFactory.GetCacheManager(); string id = "ProductOneId"; string name = "ProductXYName"; int price = 50; Product product = new Product(id, name, price); productsCache.Add(product.ProductID, product, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromMinutes(5))); // Retrieve the item. product = (Product) productsCache.GetData(id);
The Enterprise Library Caching Application Block includes the following features:
- You can use the graphical Enterprise Library configuration tools to manage configuration settings.
- You can configure a persistent storage location, using either isolated storage or the Enterprise Library Data Access Application Block, whose state is synchronized with the in-memory cache.
- You can extend the application block by creating custom expiration policies and storage locations.
- You receive assurance that the application block performs in a thread-safe manner.
The Caching Application Block is designed to work in the most common data-caching situation, which is when the application and the cache exist on the same system. This means that the cache is local and should be used only by that application. When it operates within these guidelines, the application block is ideal for addressing the following situations:
- The situation requires a consistent form of caching across different application environments. For example, by using the Caching Application Block, developers can write similar code to implement caching in application components hosted in Internet Information Services (IIS), Enterprise Services, and smart client environments. Also, the same cache configuration options exist for all environments.
- The situation requires a configurable and persistent backing store. The Caching Application Block supports both isolated storage and database backing stores. Developers can create additional backing store providers and add them to the Caching Application Block using its configuration settings. The application block can also symmetrically encrypt a cache item's data before it is persisted to a backing store.
- The situation requires that changing the cache configuration settings will not require application source code changes. Developers first write the code that uses one or more named caches. System operators and developers can then configure each of these named caches differently using the Enterprise Library configuration tools.
- Cache items require any of the following expiration settings: absolute time, sliding time, extended time format (for example, every evening at midnight), file dependency, or never expired. For more information about using the expiration settings, see Design of the Expiration Process.
- Developers want to modify the Caching Application Block source code. For more information about modifying the Caching Application Block, see Guidelines for Modifying the Application Blocks.
In addition, the Caching Application Block provides a development model consistent with the other Enterprise Library Application Blocks. The Caching Application Block integrates seamlessly with the Data Access Application Block for backing store functionality. In the same manner, the Security Application Block includes caching capability that is provided by the Caching Application Block. Developers and operators configure the application block using the Enterprise Library configuration tools.
Situations that would be better served by other caching solutions are when there are multiple applications using the cache or when the cache and the application are not on the same system. For example, you cannot synchronize caching across a Web farm. However, you can replace the CacheManager class with a custom class if you need to fundamentally change the behavior of the application block. For more information, see Extending the Caching Application Block.
The .NET Framework includes the ASP.NET cache in the System.Web namespace. ASP.NET application developers access this cache through the System.Web.HTTPContext.Cache object. The ASP.NET cache was developed and tuned for ASP.NET applications. However, this cache can also be used outside of an ASP.NET application by accessing the System.Web.HTTPRuntime.Cache object. The ASP.NET cache requires the System.Web assembly. Developers should verify that the assembly is supported on the required platforms and in the target environments.
Although you can encrypt data cached in the backing stores, the Caching Application Block does not support encryption of data that is cached in memory. If a malicious user finds a way of compromising the system and accessing the memory of your application's process, he or she would be able to access information stored in the cache. If this is a significant threat to your application, do not store sensitive information, such as credit card numbers or passwords, in the cache.