As discussed in Section 4.3, the cache allows for items to be inserted or added to the
Cache along with several other parameters, such as time-based constraints or an instance
of
CacheDependency.
<script runat="server">
Sub Page_Load(object As Sender, e As EventArgs)
Dim stringArray As string {"Rob", "Steve", "Mike", "Anne"}
' Time based dependency for 60 minutes
Cache.Insert("myArray-Time", stringArray, Nothing,
DateTime.Now.AddMinutes(60), TimeSpan.Zero)
' File based dependency
Dim fileDep = New CacheDependency(Server.MapPath("Products.xml"))
Cache.Insert("myArray-File", stringArray, fileDep)
' Key based dependency
Cache.Insert("myKey", "key for dependencies")
Dim keyDep = New CacheDependency(Nothing, "myKey")
Cache.Insert("myArray-Key", stringArray, fileDep)
End Sub
</script>
Comments
In the previous examples, three types of dependencies are created:
n Time-Time-based dependencies specify that an item is to remain in the cache
for the specified duration of time.This is the same functionality found when using
the @OutputCache directive and setting the Duration attribute.
n File-File-based dependencies specify that an item is to remain in the cache until
the named file changes.Any output cached page is automatically made dependent
upon itself. However, it's possible-as demonstrated in this example-to use this
same functionality for entries within the Cache API.
n Key-Key-based dependencies specify that an item is to remain in the cache until
a related cache key changes.This is very powerful because any running ASP.NET
code can affect the cache key to enforce this relationship. For example, cascading
relationships can be established whereby one cache entry is changed or removed
and multiple other cache entries are affected.
Dependencies provide a simple but powerful programming model for controlling
removal of resources from the cache when other resources change.
No comments:
Post a Comment