Cache
StateThe ASP.NET Cache
has the same scope of Application
, meaning that both can be accessed by all page requests. The primary difference is that Cache
enables holding information in memory, which avoids the need of re-creating and retrieving objects. This is good if you want to maintain updatable objects but could cause overhead (always considering that the bigger the amount of data to transfer is, the lower is the performance) because it requires memory, so it should be used when actually needed or when you ensure that performance is acceptable. The following is an example of storing and retrieving information with Cache
:
Cache("MyUpdatableDataKey") = "My updatable data"
Dim myUpdatableData As String = CStr(Cache("MyUpdatableDataKey"))
There is also an alternative way for adding objects to the cache, which is the Cache.Add
method that provides the ability of setting advanced settings for the object, as demonstrated in this code:
The most interesting settings are the expiration mode and the priority. The first one can be Cache.NoAbsoluteExpiration
(like in the preceding code), which means that the data will always be available during the page lifetime, whereas Cache.SlidingExpiration
means that the data will be removed after it is not accessed for the specified amount of time. Priority is also important in case you have lots of objects in memory and ASP.NET is about to encounter out-of-memory problems. At this point ASP.NET begins evicting items according to its priority. (An object with lower priority is evicted before another one with high priority.)
3.149.231.128