How to cache links?

  • Thread starter Thread starter brett
  • Start date Start date
B

brett

How do I add a string to the cache so that it updates if the string
source changes? CacheDependency() seems to want an absolute path. My
string is a full http link.

Thanks,
Brett
 
Brett,

I will assume your link changes due to a change in the data on the
backend.

Whenever you add an item to the cache you can attach the
CacheDependency. This could be a dependency on a database table, a
file or another kind of dependency. I am not sure what you mean by it
requiring a full path. When you add an item to the Cache you use a
unique identifier for the item with the item being any kind of object.
In this case a string will obviously work.

I have often added items to the cache without a dependency because I
just want to hold onto it for a the length of the timeout or until the
cache removes it to clear space for newly added items. Using the cache
without a dependency in this way is sufficient to reduce load on the
database, especially when I know the data is not changing. But if the
data does change you can monitor it with a dependency and attach that
dependency to the cached item.

Be sure to use Cache.Insert(...) instead of Cache.Add(...) and set up
your dependency to do as you want. The Insert replaces an existing
item with the same key. The trick is using a unique key which you can
know before pulling the data used to assemble the cached item. I use a
prefix to give it some context and then usually append the primary key
or query parameters to that prefix to create the key.

See SQL Cache Dependencies here...

http://msdn2.microsoft.com/en-us/library/system.web.caching.sqlcachedependency(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqldependency(VS.80).aspx

I hope this is what you wanted to know. If not, let me know.

Brennan Stehling
http://brennan.offwhite.net/blog/
 
Hi Brennan,

It isn't related to SQL Server. The URL I'm using is defined in a
class called Utilities.cs. It's a static string in that close. I load
the URL into the cache in Application_Start() of Global.asax. If I
need to change this URL, I'd like that updated in the cache with
mininal impact to the web app. So the change will occur by manual
intervention. I'm not sure if CacheDependency() is what I should use
in that case or how to use it in that case.

By full link, I mean something similar to
"http://abc.com/somepage.aspx". It's absolute in other words. Any
suggestions?

Thanks,
Brett
 
this what you would create a cache dependency class for. when you change
the static value, set the status in your dependency class. though if its
a static string I'm not sure what the cache is for.

-- bruce (sqlwork.com)
 
Brett,

Also be aware that since this is a cache it can drop items out anytime
for multiple reasons. You can handle the OnRemove event so you can add
a fresh copy back onto the cache so you always know it is there.

It sounds like using the cache may not be the right approach for you.
If you can detect when something changes and can then update it at that
time you could just set a static property which you can access in your
web application. That way you know it will always be available.

Brennan
 
though if its
a static string I'm not sure what the cache is for.

To avoid disk hits. I have a number of static links used on my site.
Every time a page using these links loads, it will hit the static
reference. Caching avoids that.

Brett
 
I build these links initially. They are build in three parts:

http + rootdomain (depends on server I'm using) + directory and page

I can put the directory/page part in the XML file. Then build the rest
in the Refresh() of Global.asax. Then I write over the existing cache
values. When the XML file changes because I've updated a link, the
CacheDependency() will fire, causing the Refresh() method to execute
and rebuild all links. Should that work fine?

Thanks,
Brett
 
That may be more work than you really need.

Instead you can create a simple UserControl which reads in the contents
of your file and places it into a LiteralControl as the Text property.
You may want to use the OutputCache, but that only has the
SqlCacheDependency. So you will have to manage the cached item and
attach your file cache dependency.

See the example at the link below where it uses the filename to create
the CacheDependency. That sounds exactly what you want.

http://msdn2.microsoft.com/en-us/library/system.web.caching.cachedependency.aspx

It should work with less than 10 lines of code.

Brennan Stehling
http://brennan.offwhite.net/blog/
 
Back
Top