Cache is server-side state that is similar to application state it is shared with all clients. Cache is different from application state in that cache is much more flexible: There are many options to define when the state should be invalidated. Instead of reading a file with every request, or reading the database, the data can be stored inside the cache.
For the cacheing, the namespace System.Web.Caching and the class Cache are needed.
Adding an object to the cache is shown in the following example:
Parameter #1: defines the name of the cache item.
Parameter #2: is the object that should be cached.
Parameter #3: dependencies can be defined, e.g., the cache item can be dependent on a file. When the file changes, the cache object is invalidated.
Parameter #4: requires a sliding time that invalidates the cache item after it hasn’t been accessed for the time defined with the sliding expiration.
Parameter #5: invalidating the cache after the cache item hasn’t been used for 10 minutes.
Parameter #6: CacheItemPriority is an enumeration for setting the cache priority.
Parameter #7: it is possible to define a method that should be invoked when the cache item is removed.
For the cacheing, the namespace System.Web.Caching and the class Cache are needed.
Adding an object to the cache is shown in the following example:
var myobj;
Cache.Add("mycache",
myobj, null, DateTime.MaxValue,
TimeSpan.FromMinutes(10), CacheItemPriority.Normal, null);
Parameter #1: defines the name of the cache item.
Parameter #2: is the object that should be cached.
Parameter #3: dependencies can be defined, e.g., the cache item can be dependent on a file. When the file changes, the cache object is invalidated.
Parameter #4: requires a sliding time that invalidates the cache item after it hasn’t been accessed for the time defined with the sliding expiration.
Parameter #5: invalidating the cache after the cache item hasn’t been used for 10 minutes.
Parameter #6: CacheItemPriority is an enumeration for setting the cache priority.
Parameter #7: it is possible to define a method that should be invoked when the cache item is removed.
No comments:
Post a Comment