mwrock / RequestReduce

Instantly makes your .net website faster by reducing the number and size of requests with almost no effort.
www.requestreduce.org
Apache License 2.0
228 stars 48 forks source link

Added Ability to Transform Generated FileName #221

Open msarchet opened 12 years ago

msarchet commented 12 years ago

This will allow people to extend the file path out with any kind of custom behavior that they want withtout breaking the hash lookup.

An example of how this can be done in a Global.asax file to add a timestamp when the uri is generated to cause cache invalidation for people who don't have a good scheme in place to modify file names.

Registry.FileNameTransformer = (original) => { var originalExtension = original.Split(new char[] {'.'})[1]; return String.Format("RequestReduce{0}.{1}", DateTime.Now.Ticks ,originalExtension); };

mwrock commented 12 years ago

Hey thanks for submitting this!. Unfortunately I think this would have negatively unintended consequences. The problem occurs with injecting a non-deterministic element into the file name like DateTime.Now.Ticks. There are a few areas in the code that assume that a Resouce Path will end with this string. For example, at app startup, RR queries the IStore.GetSavedUrls for all Urls of a particular resource type. This looks for paths that EndWith the file name. If this contains the ticks at the time of creation, this file name will change at query time. I totally understand this does not exactly jump out at you and its easy to miss.

A couple things here:

  1. I'm just curious why you want to inject time. Normally I'd think this would be part of a versioning or cache busting technique. However, because RR include the hash of the actual file content into the resource path, versioning and caching should not be material here.
  2. Another approach you could take is to add a means of derriving from UriBuilder and make ParseSignature virtual and also add a virtual GenerateSignature. The default implementation is to hash the byte contents of the file.

Does that make sense?

msarchet commented 12 years ago

So for the GetSavedUrls I think the only thing that would be needed to be done would be to not modify the RequestReduced part of the string. The tests pass even with any arbitrary information in the string as long as the hash and this part are correct.

Answer to your first question:

So the reason that you might want to insert something that is non-deterministic is the fact that currently you can't invalidate the cache that is being set by RR if you don't modify the file names that are being used in your pages. This works great if you have complete control over all of the files that request reduce is hashing, but in some cases you don't have this.

So by inserting something non-deterministic into the generated URI whenever you flush the content for the pages it will allow the user to get the changed files.

I understand what you are trying to do, even if the machine get's reset don't have them refetch the files since the generated uri will be the same, but in this cases it will mean that if you flush the files they will refetch and cache again which is useful when some one updates a style sheet that is used across pages.

I'll push up a fix for the RequestReduced part and then tell me what you think.