Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [379]

By Root 5213 0
specified time

String[], DateTime

An array of file paths monitored starting at the specified time

String[], String[]

An array of file paths, and an array of cache keys

String[], String[], CacheDependency

An array of file paths, an array of cache keys, and a separate CacheDependency object

String[], String[], DateTime

An array of file paths and an array of cache keys monitored starting at the specified time

String[], String[], CacheDependency, DateTime

An array of file paths, an array of cache keys, and a separate instance of the CacheDependency class monitored starting at the specified time

Any change in any of the monitored objects invalidates the current item. Note that you can set a time to start monitoring for changes. By default, monitoring begins right after the item is stored in the cache. A CacheDependency object can be made dependent on another instance of the same class. In this case, any change detected on the items controlled by the separate object results in a broken dependency and the subsequent invalidation of the present item.

In the following code snippet, the item is associated with the timestamp of a file. The net effect is that any change made to the file that affects the timestamp invalidates the item, which will then be removed from the cache.

var dependency = new CacheDependency(filename);

Cache.Insert(key, value, dependency);

Bear in mind that the CacheDependency object needs to take file and directory names expressed through absolute file system paths.

Defining a Removal Callback


Item removal is an event independent from the application’s behavior and control. The difficulty with item removal is that because the application is oblivious to what has happened, it attempts to access the removed item later and gets only a null value back. To work around this issue, you can either check for the item’s existence before access is attempted or, if you think you need to know about removal in a timely manner, register a callback and reload the item if it’s invalidated. This approach makes particularly good sense if the cached item just represents the content of a tracked file or query.

The following code demonstrates how to read the contents of a Web server’s file and cache it with a key named MyData. The item is inserted with a removal callback. The callback simply re-reads and reloads the file if the removal reason is DependencyChanged.

void Load_Click(Object sender, EventArgs e)

{

AddFileContentsToCache("data.xml");

}

void AddFileContentsToCache(String fileName)

{

var file = Server.MapPath(fileName);

var reader = new StreamReader(file);

var data = reader.ReadToEnd();

reader.Close();

CreateAndCacheItem(data, file);

// Display the contents through the UI

contents.Text = Cache["MyData"].ToString();

}

void CreateAndCacheItem(Object data, String file)

{

var removal = new CacheItemRemovedCallback(ReloadItemRemoved);

var dependency = new CacheDependency(file);

Cache.Insert("MyData", data, dependency, Cache.NoAbsoluteExpiration,

Cache.NoSlidingExpiration, CacheItemPriority.Normal, removal);

}

void ReloadItemRemoved(String key, Object value,

CacheItemRemovedReason reason)

{

if (reason == CacheItemRemovedReason.DependencyChanged)

{

// At this time, the item has been removed. We get fresh data and

// re-insert the item

if (key == "MyData")

AddFileContentsToCache("data.xml");

// This code runs asynchronously with respect to the application,

// as soon as the dependency gets broken. To test it, add some

// code here to trace the event

}

}

If the underlying file has changed, the dependency-changed event is notified and the new contents are automatically loaded. So the next time you read from the cache, you get fresh data. If the cached item is removed, any successive attempt to read returns null. Here’s some code that shows you how to read from the cache and remove a given item:

void Read_Click(Object sender, EventArgs e)

{

var data = Cache["MyData"];

if (data == null)

{

contents.Text = "[No data available]";

return;

}

//

Return Main Page Previous Page Next Page

®Online Book Reader