https://github.com/robhurring/timed_file_store
Rails cache file store with timed expirations built in (simple method)
https://github.com/robhurring/timed_file_store
Last synced: 10 months ago
JSON representation
Rails cache file store with timed expirations built in (simple method)
- Host: GitHub
- URL: https://github.com/robhurring/timed_file_store
- Owner: robhurring
- Created: 2009-01-19T15:05:58.000Z (over 17 years ago)
- Default Branch: master
- Last Pushed: 2009-01-19T16:19:57.000Z (over 17 years ago)
- Last Synced: 2024-04-13T19:19:39.466Z (over 2 years ago)
- Language: Ruby
- Homepage:
- Size: 78.1 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README
Awesome Lists containing this project
README
==About
This is just a subclass of the default FileStore caching option but with a few methods overridden to allow
you to expire the cache. It is extremely basic, but was necessary for my project so I'll release it into the
wild. Keep in mind that this hasn't been heavily tested and there are some quirks. But what do you expect from
a quick-and-dirty hack? :)
More on this: http://blog.ubrio.us/ruby/ruby-rails/simple-rails-time-based-fragment-caching-with-file-store/
==Features
# Can expire from the controller or view
# Super lightweight
# No modification necessary if you are already using FileStore
# Expires caches automatically
==Setup
In your +environment.rb+ file just set the cache_store to :timed_file_store
config.cache_store = :timed_file_store, File.join(RAILS_ROOT, 'tmp', 'cache')
==Usage
===Expiring Cache From the Controller
Having the controller control the expiration of the cache is good if you are passing along any objects to the
views since it will not re-run the code if it is already cached.
# reports_controller
# reports/1
@report = Report.find(params[:id])
unless fragment_exists?("report_#{@report.id}", :time_to_live => 1.week)
@report.run!
end
# views/reports/show.html.erb -- or whatever view file
<% cache("report_#{@report.id}") do -%>;
<%= @report.name %>
<% end -%>;
===Expiring Cache From the View
If you aren't running any intense code in the controller and just want to keep a certain view fragment
cached for a while, this should work. (** I didn't test this since I only needed the previous method, but it should work -- maybe with some slight tweaks? :)
# views/wherever/whatever.html.erb
<% cache("whatever", :time_to_live => 1.hour) do -%>;
<% end -&>;
Not sure how useful that would be when considering action and page caching... but its possible.