Enterprise Library In-depth analysis and flexible application (2): implementation of cache and database synchronization through SqlDependency
For a truly enterprise-class application, caching is certainly a factor that has to be taken into account, using the caching for enhanced performance (reduction of IO operations based on persistent storage), in a rational and efficient manner. Scalability (caching data, easing pressure on resources such as database) and availability (caching data to address network problems over time, Web Service accessibility issues, database crashes, and so on). The caching application Block of the Enterprise Library provides us with an easy-to-use, extensible framework for implementing caching. It is easy to implement caching based management and programming with the help of caching application Block,administrator and developer. Since the essence of caching is to keep relatively stable data in memory to avoid IO operations on persistent storage IO operations, there are two thorny issues: Load balance problem; persistent Storage and in-memory data synchronization issues. This article provides a solution to the problem of implementing data and cache synchronization in SQL Server through SqlDependency.
Expiration policy of Cache item
By default, the cache item that is added to CacheManager by the CAB (hereinafter referred to as the caching Application block, noting that it is not composite UI Application Block) is never expired , but CacheManager allows you to apply a different expiration policy using a Icacheitemexpiration object when adding the cache item. The CAB has a set of some of the following classes implemented icacheitemexpiration to provide a different way to expire:
Absolutetime: Sets the absolute expiration time of cache item for cache item.
Extendedformattime: This expiration policy is implemented through an expression that expires every minute (*****:5 * represents minute, hour, date, month, year), and the 5th minute of each hour expires (5****); The 2nd-month-old:00 expires (0 0 2 * *).
Filedependency: Binds the cache item to a file and determines whether the file has been updated since the cache item was added by detecting the last update time for file. The cache item expires if file has been updated.
Neverexpired: Never expires.
Slidingtime: A sliding time, cache item each acquisition will extend the life cycle to set the time end, when the cache item last time to obtain, exceeding the set time, the cache item expires.
For expired cache item, will be cleaned up in time. So to implement our opening request: To implement synchronization of data in SQL Server and data in cache, we can create an expiration policy for cache item that is based on changes in SQL Server data. In other words, and filedependency, when persistent storage (database) data changes this detection, the cache automatically expires. However, for the modification and deletion of files, we can easily determine the date of the last update of the file or whether it exists. The detection of changes to table data in the database is not that simple. However, SQL Server provides a SqlDependency build that helps us easily implement this functionality.