Shaw
Shaw

Reputation: 1494

Caching with ASP.NET

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?

Upvotes: 1

Views: 308

Answers (3)

Shawn
Shawn

Reputation: 19793

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.

Upvotes: 1

user35559
user35559

Reputation: 1048

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

Upvotes: 0

Gavin Miller
Gavin Miller

Reputation: 43815

To start, you ought to look at the cost for those keywords.

  • Are you querying them from the database, each one individually?
  • Are you querying them as a group?
  • Are they constants that you're simply writing out?

In general when optimizing (IE caching) look for items that are going to return the most bang for your buck.

Also look at the old 80-20 rule; ~80 items of data are a small drop in the bucket, whereas a list of 800,000 items is worth looking at.

Upvotes: 1

Related Questions