The Microsoft Enterprise Library cache block does not provide sqlcachedependency like system. Web. caching.
Let's add one for it.
We can directly package the sqlcachedependency of system. Web. caching.
Public class sqldependency: icacheitemexpiration
{
Private sqlcachedependency _ sqlcachedependency;
Public sqldependency (string databaseentryname, string tablename)
{
_ Sqlcachedependency = new sqlcachedependency (databaseentryname, tablename );
}
# Region icacheitemexpiration members
Public bool hasexpired ()
{
Return _ sqlcachedependency. haschanged;
}
Public void initialize (cacheitem owningcacheitem)
{}
Public void Policy ()
{}
# Endregion
}
Add the following configuration in Web. config:
<System. Web>
<Caching>
<Sqlcachedependency enabled = "true" polltime = "1000"> // set polltime to 1 second. You only want to test the polltime and want to see the effect immediately.
<Databases>
<Add name = "databasename" connectionstringname = "databasename"/>
</Databases>
</Sqlcachedependency>
</Caching>
</System. Web>
We need to enable the MSSQL cache notification function of the database. Using tools will always make people forget, call, and have a bad memory.
System. Web. caching provides a class of sqlcachedependencyadmin, which can provide our management function.
String connectionstring = "..."
String tablename = "..."
Sqlcachedependencyadmin. enablenotifications (connectionstring); // enable the SQL cache notification function of the database
Sqlcachedependencyadmin. enabletablefornotifications (connectionstring, tablename); // enable the SQL cache notification for a database table
However, this method is highly dependent on the MS sqlserver database and needs to poll the aspnet_sqlcachetablesforchangenotification table in the database every time the pooltime interval to check whether the table data is changed.
-------------------------------------------------------------------
If sqldependency is not required, what methods can be used to effectively remove the cache?
Table 1: Add cache key1 and add cache key2...
Table 2: Add cache key3
Table 3: Add cache key4, add cache key5, and add cache key6...
When table 1 is changed, key1, key2 are removed.
When table 2 is changed, key3 is removed.
When Table 3 is changed, key4, key5, key6 are removed.
---------------------------------------------------------------------- This original method can definitely meet the requirements
Disadvantage: we rely heavily on the cache key value. If the location key value added to the cache is changed, we also need to change it at the location where the cache is removed.
We can use groups
Table 1: Add cache key1, add cache key2...., group1
Table 2: Add cache key3, group2
Table 3: Add cache key4, add cache key5, add cache key6..., Group3
Remove group1 when table 1 is changed
Remove group2 when table 2 is changed
When Table 3 is changed, Group3 is removed.
The results are very good.