Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7819 (new defect)

Opened 1 year ago

Last modified 4 months ago

[PATCH] Fragment caching based have some problems with different URLs

Reported by: TaQ Assigned to: core
Priority: high Milestone: 2.x
Component: ActionPack Version: edge
Severity: major Keywords:
Cc:

Description

I was facing some problems with fragment caching. Let's suppose I have a website called mywebsite.com. My customers use to reach my website typing http://www.mywebsite.com on their browsers. While they are browsing some pages, I can see the fragments (where I used <% cache do %><% end %>) on the app_root/tmp/cache/ directory, ok, works fine.

But I use to go to my website WITHOUT "www", so I type http://mywebsite.com, take a look on my fragments directory, and, surprise, I have a www.mywebsite.com under my app_root/tmp/cache directory. And worst yet, if my DNS goes down and I use my website IP address to go there, I have another directory on the cache.

So, we have some problems here because we're caching 3 times the same info, what can be a disk size problem and a bigger problem if we use the non-www version to make some changes and update (expire) just one version of the fragments. The other ones will remains outdated and that will make some mess.

I was checking caching.rb and noticed that the fragment caching path is created by the fragment_cache_key method and I changed it to verify if the parameter hash have a :only_path option. If not, it's added one with the true value.

The :only_path option, as the document says, returns the relative URL omitting the protocol, host name, and port, so all that remains are just the controller, action and parameters. So, suppose I have a "test" controller and a "show" action, the app_root/tmp/cache/ directory will have a subdirectory called "test" with a "show.cache" file inside of there, no matter for which URL I accessed it, because it stripped it.

Attachments

caching.patch (0.6 kB) - added by TaQ on 03/21/07 04:20:18.
test_controller_test.rb (0.6 kB) - added by TaQ on 03/21/07 04:20:57.
test_controller.rb (143 bytes) - added by TaQ on 03/21/07 04:21:43.
test.rhtml (132 bytes) - added by TaQ on 03/21/07 04:22:06.

Change History

03/20/07 14:45:42 changed by TaQ

  • severity changed from normal to major.

03/20/07 14:53:05 changed by TaQ

Sorry, this part is incorrect:

and, surprise, I have a www.mywebsite.com under my app_root/tmp/cache directory

should be

and, surprise, I have a mywebsite.com under my app_root/tmp/cache directory

So, I have three directories under app_root/tmp/cache:

1 - www.mywebsite.com

2 - mywebsite.com

3 - 0.0.0.0 <- some IP here

Each one with different fragments.

03/21/07 04:17:51 changed by TaQ

  • version changed from 1.2.3 to edge.
  • component changed from ActiveRecord to ActionPack.

Here are a more decent patch and a test using open-uri (to test with localhost:3000 and 127.0.0.1:3000). Please run the test before and after the patch is applied and check the /tmp/cache directory on each run. Before the patch applied it should fail because will found two directories there.

03/21/07 04:20:18 changed by TaQ

  • attachment caching.patch added.

03/21/07 04:20:57 changed by TaQ

  • attachment test_controller_test.rb added.

03/21/07 04:21:43 changed by TaQ

  • attachment test_controller.rb added.

03/21/07 04:22:06 changed by TaQ

  • attachment test.rhtml added.

03/21/07 04:26:02 changed by TaQ

  • summary changed from Fragment caching based have some problems with different URLs to [PATCH] Fragment caching based have some problems with different URLs.

03/07/08 18:04:58 changed by deimos1986

+1