views:

117

answers:

3

I have form that displays several keywords (standard set of choice lists that changes rarely). There are about 4 such fields and each have about 20 choices or so.

I'm thinking if caching the keywords will be helpful for performance / best practice? Is there a strategy for determining when to cache?

+1  A: 
Gavin Miller
I forgot to mention -- they will be queried from the database and each one individually. The form will be used about 3000 times in a month and have almost the same data for the keywords.There are no constants. 5 fields = 5 separate table lookups
Shaw
So how "expensive" is these queries. Do they take milliseconds to run? seconds? minutes? Typically queries are cheap. To me, this case sounds like it fits the adage "premature optimization is the root of all evil."
Gavin Miller
I agree with LFSR, at 3000 views per month, and as long as they are simple queries, I'd find better places to use cache resources.
Mitchel Sellers
A: 

Use a lazy initialized singeton with appropriate field to store your data. eg of this singleton at http://www.yoda.arachsys.com/csharp/singleton.html

You can have the property to be of type

"IDictionary<string, IList<string>"

if you want them organised by Category and iterable by keyword.

You can use

"IDIctionary<string, IDictionary<string, string>"

IDIctionary

if you want to be search able by category and keyword. If I read your question correctly, this would be an appropriate choice for you

+1  A: 

You could use the ASP.NET Output Cache in your Page Directive.

 <%@ OutputCache Duration="60" VaryByParam="Keyword" %>

This will create a server-side cache of the page for each Keyword GET/POST request, and each cache will last 1 minute.

So if someone visits my-page.aspx?Keyword=Cards asp.net will render the page and save it in memory as HTML for 60 seconds. If someone visits my-page.aspx?Keyword=Books it will create a separate version of the page in HTML and cache it as well.

Shawn Simon