Preventing Simultaneous Logons - Data Caching to Our Rescue
(Page 3 of 4 )
Caching may be defined as the process of keeping information in memory that takes a relatively long time to fetch for quick access the next time it is needed. There are really only two differences between the Application and the Cache objects.
- The Cache object is thread-safe unlike the Application object, i.e. one doesn’t need to explicitly lock or unlock the Cache collection before adding or removing an item. However, the objects in the Cache collections will still need to be thread-safe themselves.
- Items in the Cache collection are automatically removed if they expire, if memory in the server becomes limited, or one of their dependent objects or files changes. This means one can freely use the cache without worrying about wasting valuable server memory, as ASP.NET will remove items as needed.
Adding an Object to the Cache collection
There are several ways to insert an item in the Cache collection. You can simply assign it to a key name (as you would with the Session or the Application Collection). But this approach is generally not recommended, as it wouldn’t allow you to control the amount of time the object will be retained in the Cache. A better way of inserting an item into the Cache collection is to use its insert method.
A little on these parameters is in the table below:
Cache.Insert Parameters
| Parameter | Description |
| Key | A string that assigns a name to this cached item in the collection. |
| Item | The object you want to cache. |
| Dependencies | A “CacheDependency” object that allows you to create a dependency for this item in the cache. If you don't want to create a dependent item - just specify “Nothing” for this parameter. |
| AbsoluteExpiration | A “DateTime” object representing the time at which the item will be removed from the cache. |
| SlidingExpiration | A “TimeSpan” object represents how long ASP.NET will wait between requests before removing a cached item. |
| CacheItemPriority | Specifies how important it is for the cache item to remain in the cache. It can have “AboveNormal”, “BelowNormal”,”Default”, “High”, “Low”, “Normal” or “NotRemovable” as its value. |
| CacheItemRemovedCallback | The callback delegate provides a means for you to create your own function that is automatically called when the item is removed from the cache. |
Usually we won't use all these parameters at once. For example, we cannot set both a sliding expiration and an absolute expiration policy at the same time. If we want to use an absolute expiration, set the slidingExpiration parameter to TimeSpan.Zero.
Here is an example:
Cache
.Insert("testItem", testobj, Nothing, DateTime.Now.AddMinutes(60), TimeSpan.Zero)
Absolute expiration is best recommended in situations when you know the information in a given item can only be considered valid for a specific amount of time. On the other hand, sliding expiration is more useful when you know that a cached item will always remain valid but should still be allowed to expire if not being used.
To set the slidingExpliration policy, set the absoluteExpiration parameter to DateTime.Max as shown below.
Cache
.Insert("testItem", testobj, Nothing, DateTime.MaxValue, TimeSpan.FromMinutes(10))
Next: The Solution >>
More ASP.NET Articles
More By Vadivel Mohanakrishnan