ASP.NET output caching - dynamically update dependencies - asp.net

I have an ASP.NET application which requires output caching. I need to invalidate the cached items when the data returned from a web service changes, so a simple duration is not good enough.
I have been doing a bit of reading about cache dependencies and think I have the right idea. It looks like I will need to create a cache dependency to my web service.
To associate the page output with this dependency I think I should use the following method:
Response.AddCacheItemDependency(cacheKey);
The thing I am struggling with is what I should add to the cache?
The dependency my page has is to a single value returned by the web service. My current thinking is that I should create a Custom Cache Dependency via subclassing CacheDependency, and store the current value in the cache. I can then use Response.AddCacheItemDependency to form the dependency.
I can then periodically check the value and for a NotifyDependencyChange in order to invalidate my cached HTTP response.
The problem is, I need to ensure that the cache is flushed immediately, so a periodic check is not good enough. How can I ensure that my dependant object in the cache which represents the value returned by the web service is re-evaluated before the HTTP response is fetched from the cache?
Regards, Colin E.

I believe you are on the right track with your cache dependency. However, if you don't "periodically check" the return value of the web service, how can you know when it returns a new value? You may need to set up a web service in the other direction, so that when the value changes in the other system, it can call your system and invalidate the old cache and stick in the new value.

You can manually invalidate a cached page using:
System.Web.HttpResponse.RemoveOutputCacheItem(path)

Related

Memcached Persistence on Tapestry5

I've searched endlessly and am unable to find a way to implement Memcached on Tapestry5 such that I instantiate only 1 "MemcachedClient" object for the entire application.
I have a page whose responses need to be cached.
I want to create only 1 instance of a "MemcachedClient" on this server (regardless of session) and query the cache using the same object instance.
I've tried #SessionState but this does not help, since my "user" is a load generator and hence each request results in a new object instance.
Any advice please?
Edit: The same goes for any other variable that I want to persist across visits, regardless of session. How can this be achieved?
You'll need to define a service in tapestry-ioc (services are singletons by default)
This will be done in your IOC module either with a builder method or via ServiceBinder.bind(...)

prevent ejection of output cached page

I'm trying to prevent output cached page from being ejected from my asp.net output cache unless another one can replace it. In other words, if a page has expired (based on Duration property of OutputCache profile) but accessing the page now results in an exception, I just want to serve the old, outdated page. I was hoping the custom cache providers in ASP.NET 4 would help my situation but they don't get far enough up the pipeline.
Here's what I know:
System.Web.Caching.OutputCache is really hard to read through
A custom cache provider hooks in after all dependencies and is fed a CachedVary object.
The utcExpiry date passed into the custom cache provider's Add and Set methods is just DateTime.Max
The actual expiration of the cache item happens higher up in the pipeline
Even for expired items, the custom cache provider's Get method is still called. It returns the cached CachedVary object and then my controller's action method is called. After this, the Add method is called with the CachedVary object and Set method is passed an actual cached page data object.
Can someone with more experience hacking outputcache steer my in a direction here? Can I accomplish what I'm looking for with ASP.NET outputcache? Must I roll my own?
i ended up blogging about how i accomplished this here: http://statichippo.com/blog/archive/2011/09/25/graceful-degradation-via-asp-net-outputcacheprovider.aspx

Implement second level cache in ASP.Net

Is there any way to use caching in ASP.Net except SQL Server second level cache. As it is the first time to work with caching I want any way with an example. I have found that NHibernate implements this but we are using .netTiers as an application framework.
The Session cache seems to be the appropriate caching mechanism here. The Session cache is a fault-tolerant cache of objects.
Inserting an object
Session["Username"] = "Matt";
Reading an object
string username = (string)Session["Username"];
Removing an object
Session.Remove("Username");
I say fault-tolerant because if the value with the key you specify doesn't exist in the Session cache, it will not through an exception, it will return null. You need to consider that when implementing your code.
One thing to note, if you are using Sql Server or State Server, the objects you can put in the cache need to be serializable.
Memcached is also a very good way to go, as it is very flexible. It is a windows service that runs on any number of machines and your app can talk to the instances to store and retrieve from the cache. Good Article Here

How to make per- http Request cache in ASP.NET 3.5

We using ASP.NET 3.5 (Controls-based approach) and need to have storage specific for one http request only.
Thread-specific cache with keys from session id won't work because threads are supposed to be pooled and therefore I have a chance to have data from some previous request in cache, which is undesirable in my case. I always need to have brand new storage for each request available through whole request.
Any ideas how to do it in ASP.NET 3.5?
We have used HttpContext.Current.Items collection to do RequestScope caching. It works well.
just to clarify what ggonsalv was referring to
http://msdn.microsoft.com/en-us/library/system.web.httpcontext.items.aspx
HttpContext.Items["key"] = value;
UPDATE: the mvc specific version
http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.httpcontext(v=VS.100).aspx
How about using the Context collection. This allows data to be shared between all your controls but only lasts for the request.
Use it like this
context.Items("base_url") = "default.aspx"

IIS Integrated Request Processing Pipeline -- Modify Request

I want to implement an ISAPI filter like feature using HttpModule in IIS7 running under IIS Integrated Request Processing Pipeline mode.
The goal is to look at the incoming request at the Web Server level, and inject some custom HttpHeaders into the request. (for ex: HTTP\_EAUTH\_ID)
And later in the page lifecycle of an ASPX page, i should be able to use that variable as
string eauthId = Request.ServerVariables["HTTP\_EAUTH\_ID"].ToString();
So implementing this module at the Web Server level, is it possible to alter the ServerVariables collection ??
HttpRequest.ServerVariables Property is a read-only collection. So, you cannot directly modify that. I would suggest storing your custom data in httpcontext (or global application object or your database) from your httpmodule and then reading that shared value in the aspx page.
If you still want to modify server variables, there is a hack technique mentioned in this thread using Reflection.
I believe the server variables list only contains the headers sent from the browser to the server.
You won't be able to modify either the HttpRequest.Headers or the HttpRequest.ServerVariables collection. You will however be able to tack on your information to any of:
HttpContext.Current.Items
HttpContext.Current.Response.Headers
Unfortunately, Request.Params, Request.QueryString, Request.Cookies, Request.Form (and almost any other place you'd think of stuffing it is read only.
I'd strongly advise against using reflection if this is a HttpModule you're planning on installing into IIS 7. Given that this code will be called for (potentially) every request that goes through the web server it'll need to be really fast and reflection just isn't going to cut it (unless you have very few users).
Good luck!

Resources