× Please, log in to give us a feedback. Click here to login

You must be logged to download. Click here to login


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

OutputCache - Course ASP.NET 4 and Visual Studio 2010 - Part 66 | .net courses

If you need programmatic control of the attributes within the OutputCache directive, you can use Response.Cache.HttpCachePolicy.

Duration: 17 minutes

Summary: In this video, we continue our discussion about caching. As I specified in the previous video, caching could be on the client (browser), webserver, or the proxy server. Using the Location attribute of the OutputCachedirective, you can specify the location of the caching using any of the valuesprovided (none, Client, Server, ClientAndServer…). There is also a cachingdependency on files that you can specify (Response.AddFileDependency()) so whena file changes you can remove the associated page from the cache. You canremove a page programmatically from the cache using RemoveOutputCacheItem()method. If you need programmatic control of the attributes within theOutputCache directive, you can use Response.Cache.HttpCachePolicy.

If you need to change the caching strategyfor many files in the web site, you can specify a cache profile in web.config,and make all the pages use the same profile. That way when you need to changethe settings you can change them in one place. Finally, I will show you how touse the Substitution control to have portion of the page NOT cached.

Methodologyof the development of example: Good coding standardand simplified design to illustrate the key points.

TechnologyUsed: ASP.NET and Visual Studio 2010

Keyword: ASP.NET 4, Visual Studio 2010, Caching, VaryByParam, Substitution,Client, Server, ClientAndServer, DownStream, AddFileDependency,HttpCachePolicy, Cache Profile.

Video transcription: Welcome back everybody. This is Ayad Boudiab and I'm still doing my discussion on advanced ASP.net features. And specifically, we've been talking about caching. In the previous video, I introduced you to caching and I showed you a few examples on how some pages can be cached.

In today's video, I need to talk more about caching, just give you some examples on where the location of the cache and how we can have file dependency caching and how we can remove the cache, if we want to. Also, I'm going to show you how we can set that up in web.config and basically how we can have partial caching.

So, the first thing that I need to start with is, we can specify the location where the page is cached. For example, if you have some critical information that you don't want to show to the user, then you can specify that the cache location is actually on the server but not on the browser. Because we have three cases in here where the caches could be. It could be on the server; it could be on the proxy server; or it could be on the browser. So, these are the three locations where that happens, and the location attribute would allow you to specify where the cache is.

Let's say, for example, I'm here on outputcache and it says duration equals ten, right? And then, I can specify the location where my cache is. I can specify any. I could be on the browser, on the proxy server, or on the web server, which could be on the client, which is the browser. It could be downstream, which is the browser and the proxy server but not the web server. It could be none, I don't want to load the caching in that case. It could be on the web server, or it could be on the server end client, excluding the proxy server.

You can specify the location of the caching and be more detailed about that. You can say duration ten seconds, vary by param equal none, and then you can specify where the caching would be. I believe the default in that case would be on the browser, web server, and the proxy server. That's as far as the location of the caching.

Now, can I make the caching depend on a certain file? Sure. I could have a file, for example, an XML file of some clients or just any text file that's on my website. And when this file changes, I can remove the copy from the cache because, in that case, we are assuming that the cache is not valid anymore. We need to remove it. Then, we can make it depend on a specific file.

Now, how do you do that? Sometimes, for example, on the page load, you can specify response that add file dependency. The method, add file dependency in the response object will allow you to make this page depend on a specific file. Of course, the add file dependency would take the parameter, which represents the name of the file that you're depending on. So, the add file dependency then helps me depend on a specific file. When that file changes, I'll remove the associated page from the cache.

Now, can you remove an item from the cache? Yes. For example, let's say I'm on a specific website. The website displays all the books that I'm interested in, and then one of the books has been removed for some reason. The book is not there anymore, it has been deleted. Now, this page would actually need to be refreshed. The book does not exist on that page anymore. So, I need to remove that page from the cache.

Programmatically, I can do so when I delete the book. I can say remove outputcache item, and I can remove that item from the cache and that page is not there anymore. So programmatically, I can specify that I need to remove a certain item from the cache. As a matter of fact, the attributes that we specified here, for example, duration, vary by param and all this?

These attributes, although you can specify them here within the page, you can also specify them programmatically because in the response, that cache object, you have HTTP cache policy class that you can use to specify what those parameters are, so you can control your caching programmatically instead of declaratively in here.

But obviously, the declaratively is a little bit better and faster and cleaner, but you can have more control of the caching using the HTTP cache policy that you have in the response, that cache object. So then, we talked about the location of the cache. We said the location is client, which is the browser, the proxy server or the web server. You can specify where this caching should take place, and I believe by default it's on all three.

You can also make the cache depend on a certain file. When the file changes, the caching would be removed. You could remove the caching programmatically using the method remove outputcache item. And also, the attributes that we specified here, we can remove them programmatically using the HTTP cache policy of the response, that cache object.

Now, the other thing is, I have a page in here and I specified my outputcache and I specified my attributes. Well, this is kind of straightforward for a specific page, but let's say you have 30 pages in here that you need to specify the caching for. It would be tedious to just go in and change this cache policy for every one of those pages.

And obviously when something changes down the road, and something will change down the road, you have to go to those 30 pages and change the caching strategy that you had for all those 30 pages. That's kind of tedious, right? So we need a way to specify that caching in one place. Well, this is when the web.config comes to the rescue.

In the web.config, I can specify some of these caching policies that I have. Within system.web, I can specify here, for example, in this location, I can say caching. In the caching, I have the outputcache settings that I can change. I can specify a specific profile for that caching.

I can add that profile, and I can give him a name. For example, I want to cache every ten minutes. So cache ten minutes. That's the name of that profile. And, as I said, my duration should be 10 minutes times 60 seconds; that would be 600 seconds. And in this case, I want to specify that vary by param is none.

So, I specified my cache duration should be ten minutes, and I specified that using the duration attribute. I gave that a name for that profile and vary by param equals none. I specified my caching profile, but obviously this one by itself doesn't do anything, right? I specified it here, but who's using it? Nobody yet. So in this case then, I need to go to a specific page and say to the page: hey, you're using this profile.

So, I need to go back to my caching one, the SPX page. Here's my outputcache. I can remove the duration from there, and I can specify what my cache profile is. Cache profile equal and then I can say it is cache ten minutes, the profile that I had earlier.

Now, this page is using the caching profile that I just showed you, and now I can go to the web.config, I can modify this profile and the page will pick it up automatically. I don't have to go and change it on all those 30 pages that I was talking about. So, then the web.config helps me set some caching policies. These caching policies could be applied to specific pages by specifying the cache profile on that outputcache directive.

Now, we specify the cache profile for those pages, now what else can we do? Let's move into another example in this case that talks about partial caching because, let's face it, when you're working with a page, you hardly ever have a static page that you're caching. You could have many static pages but overall, the pages, part of them could be dynamic, other parts could be static.

For example, the advertisements. We have different advertisements that are randomly generated and displayed on the page. Now, the advertisement control in that case is not cached because obviously we don't want the same advertisement to be served to every user. We need different advertisements randomly selected and provided to that user. So, that control then is not cached. I could have a portion of the page to be cached and other portions not cached.

Let's see how we can do that. We have in here called caching one. That's at another page called caching two. On that page, I'm going to drop a label. I'll give it some ID. It'll be real time. And then on the code, I would say lblTime.text equal and then say date time.now.tostring and specify that the time T.

Now, you've said we've done this before, right? Yeah, we've done that before. So far, it's the same page like the one that we were talking about earlier. Now going back here to the top of the page, I can specify my directive, and I can specify output caching to be duration equals ten and then vary by param equals none. So, that's this page is cached every ten seconds. That's the page in the browser. Bear with me here, I'm going to show you an example.

But let's view the page in the browser. The page loads up. It should give me the time and then, from there, I'm going to reload the page. It says 58. I'm refreshing the page, nothing happening. Refreshing every ten seconds, I get the time back again. Refreshing, nothing happens. And now refreshed again. All right.

This page, then, is being cached and it's being served from the cache for me every ten seconds, it's just going to be refreshed again and the whole page is reloaded.

Now, there is in here, in your controls, there is a substitution control. Substitution control is the one that you can use if you don't want that portion of the page to be cached. So, for example in here, I could say using the substitution control, and then I'm going to drop the substitution control right underneath it. Let's go to the source.

Here's the substitution control. Substitution, substitution one, that's the control right there. And then in here, I have some properties for this substitution control. The important property that I'm interested in is the method name. I can specify the method name for that specific control. Let's say call it, for example, get my time. It gives me the time that I'm interested in.

Now, this method is the one that returns back the time for me, like the other one returning the time. All right, I got my time. Let's go to the source code and write that method. So, this method is public, must be static and it returns a string because it's the time in string format, get my time. This method is taking an HTTP context, named context. That's how the signature of the method is. It's a public method, static method, returns a string in this case because I'm returning a string which is the date and the time in a string format, and then the method is called get my time with HTTP context as a parameter.

So, I'm going to return date time.now.tostring in a T format. That's the method get time. It returns the time. Now, remember the method is being called, that's the method name for the substitution control. Let's try to click on the page, view it in the browser. 5:29:01, 5:29:01, refreshing. You notice that every time I refresh, the one with the substitution is changing, but the top one is only changing every ten seconds. So, the substitution is changing, the other one will change eventually. See there, 20 right now, 21, 22, 23, 24, 5, 6, or 7, 8, 9, 30.

So, then in that case then, you notice that the one with the substitution control is the one that does not get cached. Only the other portion, the rest of the page, is being cached. But not the one with the substitution control. So, this is how we can use partial caching. A portion of the page is being cached but in our case in here, a portion of it is not being cached, which is for the substitution control. And the rest is being cached for the duration of ten seconds.

That's how we can work with partial caching and, as you noticed, I explained for you how we can specify the location where the page is cached. We can have a dependency on a specific file using add file dependency. We can remove an outputcache from the item by using the method remove item, put cache item. We can also specify the configurations in the web.config, so we don't have to change them in every page. And we can use the substitution control so we don't have to cache a specific control within a page.

I hope this discussion is helpful to you. Please stay tuned for more upcoming videos. Thank you very much for listening. I'm Ayad Boudiab and have a good day.

Senior Software Engineer with experience in C#, JavaScript, jQuery, Knockout JS, Kendo UI, HTML5, and SQL Server. Ayad has more than 20 years experience in technical training and teaching. Contributor to MrBool for about 7 years.

What did you think of this post?
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 2,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 1,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,33
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!

> More info about MrBool Credits
You must be logged to download.

Click here to login