Implementing Generic Caching(objectreference.net)

submitted by Vort3XVort3X(1335) 4 years ago

I develop for a large, high-availability website, with hundreds of thousands of daily users. As such, we need to cache a lot of data in our web-server memory (which is cheap) to save numerous hits to our main database cluster (which is very expensive). I would imagine the desire to improve performance by saving on database hits is common across many web applications – and caching frequently used data is often seen as one of the best ways to solve this problem.

6 comments |category: |Views: 232

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by simonechsimonech(10.1k) 4 years ago 0

Commenting here since the original site is broken and commenting doesn't work:
------------------
How do you deal with different parameters?
This seems to work when you retrieve always the exact same object, for example, the list with the top 10 news in a online newspaper
What if you need to retrieve, for example, a list of news by category? here there is now way to pass in a parameter.

Reply

posted by bzbettybzbetty(0) 4 years ago 0

Also commenting here since the original site is broken.
--------------------------

What's the advantage of using a constructor like that vs a static property that basically contains the same code, this would mean you wouldn't need to create an instance of the SafeCache class to use it (however minor that is) I do think it would make the syntax of using it a little cleaner

Would also mean you could type this instead no?
DropDownList1.DataSource = SafeCache<Widgets>.Object.WidgetIds;
DropDownList1.DataBind();

That said the Activator.CreateInstance might be more of a performance hit anyway.

Have you also thought about abstracting your caching strategy out a little? basically meaning you rely on HttpContext.Current.Cache existing, if you were using this in a BusinessLogic class that you wanted to unit test I'm guessing you'd either have to go to extra effort to get it working (create a HttpContext it could use), change it to not use the cache if HttpContext.Current is null, or use some form of dependency injection to replace the httpcontext implementation with another custom one (say a static hash table)

Reply

posted by Davbis93Davbis93(17) 4 years ago 0

Hi,

Regarding your suggestion of using a static property - I completely agree, and have updated the code sample accordingly.

Also, regarding the HttpContext dependency, I originally had a singleton implementation, which I removed from this post for the sake of brevity, however you've inspired me to put this back in, as some may find it useful.

simonech, Given the example you suggested, if using this, you'd be forced to cache the "whole newspaper" and use something like: SafeCache<Newspaper>.Object.Categories(2).

However, I think it might be possible to have a different version of this, which is parameterized generically, by having:

public SafeCache<Type, Parameter>
{
public Type GetObject(Parameter param)
{
//Get and cache including param.ToString() somehow in the cache-key
}
}

I'm sure there's probably a more flexible way of doing this.

Thanks for your ideas,
Dave

Reply

posted by simonechsimonech(10.1k) 4 years ago 0

FYI: commenting seems to be working now

Reply

posted by mojtabamojtaba(0) 4 years ago 0

that is a good article about caching.
mojtaba from <a href="http://www.vestasoftware.com">Vestasoft</a>

Reply

posted by mojtabamojtaba(0) 4 years ago 0

i didn't knew that this software changes the hyper links automatically
i meant http://www.vestasoftware.com

Reply

information Login or create an account to comment on this story