Groups | Blog | Home
all groups > asp.net caching > october 2004 >

asp.net caching : Advice on cache design



Stephen Woolhead
10/23/2004 2:07:12 AM
Hi all, I am looking for some advice on how to implement a caching solution
for use in my asp.net app.

I have a function that returns a simple true or false, but to get the answer
needs to execute appox 20 queries to the database. This function gets
called a lot so I want to store the result in a cache. The problem is that
the function is dependent on three parameters two of which are indexes of
lists stored in the database, a change to any of these lists invalidates the
function's cached results.

What I need to implement is a cache where I can invalidate all cache items
that have a dependency on a certain list index.

At the moment, I have a database table which looks like this

MembershipListID, AccessListID, OperationID, result, timestamp

Everytime this function is called, I check to see if there is a result in
the cache table, if the is I use it otherwise I do the work and an entry to
the cache table.

If the value of one of the lists changes I issue a delete for all records
that use that list ID.

Now I would like to move this to the ASP.NET cache for a bit more speed, and
ease of management but am not sure how to.

My best idea at the moment is to create dummy cache entries for each of the
list indexes and then create a cache dependency on these keys for each
result that goes into the cache, something like this.

MembershipListID = 4
AccessListID = 8
OperationID = 3
Result = true

So I would add the following place holders for creating cache dependencies

MLID#4
ALID#8

I would then add another cache item with the key FR#4#8#3 with the value
true and cache dependencies on MLID#4 and ALID#8

When one of the lists changes, I would remove the place holder key from the
cache, for example MLID#4 and that would remove all the function results
dependant on it.

Is this the best way to solve something like this?

Also while I have your attention, what is the accepted way to namespace your
cache entries? I don't want my cache entries to get confused with another
assemleblies data, so what do I do? Pre/post fix the key with the assembly
name or public key?

Thanks

Stephen

Alvin Bruney [MVP]
10/23/2004 12:34:43 PM
..net version prior to 2.0 do not support cache dependencies at the database
level so you can either use your approache which seems sound to me, or you
can set a global flag to dirty everytime the lists changes. Test this flag
before retrieving cached results. if it is dirty, grab from the database and
update the flag to clean. Just depends on which way you want to go
architecture wise.


[quoted text, click to view]
have a look at MSDN for naming conventions. I do not particularly adhere to
them so i'm not in a position to dispense advice.

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
[quoted text, click to view]

Donald Babcock
11/6/2004 8:40:10 PM


this solution works pretty well, it doable with application level caches
as well...

It works fine until you want to have the page which updates the DB do a
post back, and have the updated Cached data play a roll in the postback
. The nature of ASP.NET processes the postback first, and then applys
the event triggers. One solution is to have events call page_load, but
that is far from efficient.

I have seen a DB flagger writtern for VB.NET that claims to work, I am
working to modify it for C#, if anyone has a working version in C#
please let me know.

Don

*** Sent via Developersdex http://www.developersdex.com ***
Ben Strackany
11/9/2004 4:22:00 PM
Can you write a stored procedure that encapsulates the ~20 database calls
you're doing? Then at least you're only calling the DB once instead 20
times.

--
Ben Strackany
www.developmentnow.com

<a href="http://www.developmentnow.com">dn</a>


[quoted text, click to view]

AddThis Social Bookmark Button