ASP.NET cache object - asp.net

I have a MetaTags table on a MS-SQL database. The Master page of my site reeds this info everytime a page is load.
I'd like to use a ASP.NET cache object in order to improve performance, so I don't have to access the db everytime.
Someone gave me the idea of using a time based expiration cache and also a sql based expiration trigger.
Could someone please help me to decide which aproach is better and give me an example of some lines of code to develop it?
Thanks a lot.

It sounds like they are describing the SqlCacheDependencyclass SqlCacheDependency MSDN Article
You could also just repopulate the cache yourself when the item expires. Notify an Application When an Item Is Removed from the Cache

Related

Priming the asp.net output cache

Is there a way to programmatically prime the asp.net output cache? I've investigated the caching API and can't seem to find an obvious way to do this. Has anyone tried something like this? If so, what method did you use?
I gave some thought to this last year and ended up concluding that it was not that important for the case, but if it's important for you website, all you have to do is to simply call the webpages from somewhere like Application_Start (after all code has run) event but you shouldn't stop there!
The cache will eventually expire and to avoid that you should set up some way to cache the pages again before any clients requests that page.
Make the outputcache dependent on someother object in cache and set an expiration callback.
Thus, when that cache object expires, so does your pages and you should make http requests to the pages you want to recache and so on.
I'm answering to this question, but the amount of effort and question marks I still have in my mind lead me to advise not to go through with this...
UPDATE
The only kind of dependency you may set in outputcache is sql dependency. Use it if you want, but if you would need to depend your outputcache on some other business object, then this might get very difficult. I could tell you that you could set a database object and depend your database on it and expire it yourself using some kind of timer.
Man, the longer I write the more solutions and difficulties I find! I can't write a book for something that is not worthy your precious time. Believe me you that the usefulness for this will be nearly zero.
Priming the cache is as others have suggested as easy as requesting the pages you want cached. Of course if you do this programmaticly it will only request the HTML and not all the linked resources (CSS, JavaScript, Images...) which is a good thing to avoid wasted bandwidth.
For many websites the items that are cached which consume the biggest performance penalties are common to many or all pages. For example a navigation system on a large CMS or storefront may query the database and do a bunch of rendering work which can then be cached for all pages. Also a big part of the initial load in ASP.net is when the website if first accessed and loaded into memory. Both of these issues can be addressed by even calling a single page on your site, but there is nothing stopping you from making a list of URLs and calling each one periodically.
If your cache policy is set for a 20 minutes timeout, maybe request each page once every 17-18 minutes.
Here are some resources with source code to help you get started:
Good Simple Primer on requesting web URL in C#
Website Monitoring Windows Service
Asyncronous Website Monitor
As I mentioned before, you can easily extend these to "foreach" over an array or list of URLs to be requested.

How to see how many online users are on my ASP.Net MVC3 website?

What's the easiest way to see how many users are currently online on my website?
I'd like to display and set this information inside of my _Layout.cshtml file.
I'm using the latest version of ASP.Net MVC3 along with C#.
Actually it can be handled in the same way as in ASP.NET.
Main scenario is to increment some static variable or variable in Application[] collection on Session_Start and decrease this value on Session_End to handle number of online users.
Application state can be used in this case. Hold a global variable which increments on each new session and decreases on session expiry

ASP.NET config data

I'm working with ASP.NET and c#.
I don't know where to store the configuration setting for my web app.
Every time a page loads the app reads the master page. On the master page, everytime I need to get the following data:
Meta Tags for the site
The users have to be able to change this data in their CMS, so I think the best for this is to store in the database. So every time a user request a page the web app is going to query the database.
Some configuration like the master page we are using (we use several for several templates), and some other config. We think we can store this in the web.config since users can't modify these values.
In the past we used an xml file to read the Meta Tags, but we ythink is better to use the database.
Also, does it cost a lot for the web app to access the web.config in every page? I mean, to do "ConfigurationManager.AppSettings["variable"];"
Thanks a lot !!!
ConfigurationManager.AppSettings["variable"] value is remained and picked from cache, until web.config changes.
you can also store application setting in xml file in App_Data folder, and mentioned in web.config like:
<appSettings configSource="App_Data\Config\siteConfig.xml" />
For Metatags a database storage will be better. You could use the ASP.NET cache object to improve performance so you don't have to get the info from DB on every request. You could use a time based expiration for the data or even sql based expiration trigger.
The web.config file is not accessed every time an item is read. It's stored on memory so there is no performance hit on using it.
Web.Config
Ok, but if web.config is stored in memory and the site has tons of visits, is this good for performance?
Meta Tags
can you give me an example on how to use ASP.NET cache object to store META-TAGS?
In this case with a lot of visits is also better to use cache instead of access to database on every request?
The cache object is for every user/session on all the users share that object? It'd be better that all the users use the same cache object. This way the firsat one access to the database and the rest get the cache object. Is possible to do this way?
Thanks a lot for you answers

Clear ASP.NET OutputCache across web applications

Is it possible to clear the output cache of one asp.net web application from inside another asp.net web application?
Reason being... We have several wep applications structured like...
http://www.website.com/intranet/cms/
http://www.website.com/area1/
http://www.website.com/area2/
Pages in /area1/ and /area2/ are cached and are managed through /intranet/cms/. When a page is edited using /intranet/cms/ I want to clear it out of the cache in the appropriate /area#/ application.
I already tried using a VaryByCustom that looks up a guid stored in the HttpContext.Cache but that seems to be cached per web application, that doesn't work.
Really if there were any way of passing data between web applications on a single server, that would solve my problem, since I can use that + VaryByCustom.
Thanks!
-Mike Thomas
The way I've done this in the past is to have a "hidden" page (in each of the /areaX sites) that does the flushing, reloading, etc. The page validates a shared secret query parameter before doing anything (to avoid DoS attacks). If valid the page would output an "OK" message once the operation is complete; generates a 404 error if the secret is invalid.
If you want the flush to be on a per-item or per-group basis then add a second parameter that identifies that item/group.
This method is also server technology independent, and can be triggered by other management tools if required.
One way I know of doing this is by using a shared resource as a dependency, usually a file. When the file is changed, the cache is cleared. I think you can use HttpResponse.AddFileDependency for this.
However, in these cases it's usually better to use an out-of-process cache such as memcached. I haven't tested it myself, but this link deals on using memcached with OutputCache.

ASP.Net Context.User.Identity weirdness

I have an ASP.Net 3.0 SP1 app that uses Form Authentication.
While testing, I noticed that if I viewed a page that another user was viewing, the other users name would be displayed in the control on my master page. The Context.User.Identity is also for the other user.
If I switch to different page that no one else is viewing the Context.User.Identity is correct.
I stumped and would appreciate suggestions.
Thanks in advance.
Chris
Maybe because output caching is enabled for the page: if the page is cached server-side with VaryByParam=none, all users will get the same copy from the cache.
I can only think of two things that can cause this:
You're storing user-specific data in a place shared between requests (e.g. in a static(C#)/shared(VB) variable, in the ASP.NET Cache, in the Application object, ...)
You have output caching enabled.
Check for:
OutputCache directives in your aspx and ascx files,
system.web/caching element in your web.config file(s),
Calls to the HttpCacheability.SetCacheability method.
If you can't find the problem:
Try creating a simplified version of your application until you get the simplest possible version that still reproduces the undesirable behaviour.
During this process of simplification you'll likely discover the problem for yourself. If not, post some code from the simplified version.
Make sure you are not using a link that comes with the authentication ticket when using a cookieless browser.
Also make sure to review any other that might be sharing the data among requests. Just like DOK said, but remember Application isn't the only way you could be doing that.
It looks like the issue was caused because I setting targetframe="_self" or Target="_self". I removed all these and everything seem to be working fine.
One other note: If I were to refresh the page it would also display the page with the correct user.

Resources