Commit a5acd4088630fb4c15525f82c63524cf65c76c46

Authored by Moises Machado
Committed by Antonio Terceiro
1 parent 3d4039f4

ActionItem955: included plugin for timed cache

vendor/plugins/timed_cached_fragment/README 0 → 100644
... ... @@ -0,0 +1,29 @@
  1 +TimedCacheFragment
  2 +==================
  3 +
  4 +In Rails, caching is one of those things that is made pretty easy, but can also be difficult when you want to do complex tasks with the caching -- such as expiring it. There are two ways with expire_fragment within the logic of your controllers or using an Observers.
  5 +
  6 +This plugin allows caching to be expired on predefined time limit. The idea is that the caching takes cared of the expiration instead of writing conditions in the controller. I understand that it goes against the idea of why the caching mechanism does not have them now.
  7 +
  8 +To the plugin. Its an extension of cache method that has logic for the time limit. The method to use within views is called cache_timeout. The first argument of the method is the regular Hash/String of the cache key and the second argument is the timeout value (being a Time class value).
  9 +
  10 +Example usage:
  11 +
  12 +# This is last 5 minutes of posts from our forums:
  13 + <%cache_timeout('forum_listing',5.minutes.from_now) do %>
  14 + <ul>
  15 + <% for post in @posts %>
  16 + <li><b><%=post.title%></b> - by <%=post.author.username%> at <%=post.created_at></p></li>
  17 + <%end%>
  18 + </ul>
  19 + <%end%>
  20 +
  21 +To enforce the timeout cache value within the controller don't use the read_fragment method -- unless really needed. The method that is provided to check that the timeout value has is expired is is_cache_expired?. The method takes the argument of the cache key.
  22 +
  23 +Example Usage within controller:
  24 +
  25 +def list
  26 + if is_cache_expired?('forum_listing')
  27 + @posts = Post.find(:all)
  28 + end
  29 +end
0 30 \ No newline at end of file
... ...
vendor/plugins/timed_cached_fragment/Rakefile 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +require 'rake'
  2 +require 'rake/testtask'
  3 +require 'rake/rdoctask'
  4 +
  5 +desc 'Default: run unit tests.'
  6 +task :default => :test
  7 +
  8 +desc 'Test the timed_cache_fragment plugin.'
  9 +Rake::TestTask.new(:test) do |t|
  10 + t.libs << 'lib'
  11 + t.pattern = 'test/**/*_test.rb'
  12 + t.verbose = true
  13 +end
  14 +
  15 +desc 'Generate documentation for the timed_cache_fragment plugin.'
  16 +Rake::RDocTask.new(:rdoc) do |rdoc|
  17 + rdoc.rdoc_dir = 'rdoc'
  18 + rdoc.title = 'TimedCacheFragment'
  19 + rdoc.options << '--line-numbers' << '--inline-source'
  20 + rdoc.rdoc_files.include('README')
  21 + rdoc.rdoc_files.include('lib/**/*.rb')
  22 +end
... ...
vendor/plugins/timed_cached_fragment/init.rb 0 → 100644
... ... @@ -0,0 +1,2 @@
  1 +# Include hook code here
  2 +require 'timed_cache_fragment'
0 3 \ No newline at end of file
... ...
vendor/plugins/timed_cached_fragment/install.rb 0 → 100644
... ... @@ -0,0 +1 @@
  1 +# Install hook code here
... ...
vendor/plugins/timed_cached_fragment/lib/timed_cache_fragment.rb 0 → 100644
... ... @@ -0,0 +1,49 @@
  1 +# TimedCacheFragment
  2 +module ActionController
  3 + module Cache
  4 + module TimedCache
  5 + #used to store the associated timeout time of cache key
  6 + @@cache_timeout_values = {}
  7 + #handles standard ERB fragments used in RHTML
  8 + def cache_timeout(name={}, expire = 10.minutes.from_now, &block)
  9 + unless perform_caching then block.call; return end
  10 + key = fragment_cache_key(name)
  11 + if is_cache_expired?(key,true)
  12 + expire_timeout_fragment(key)
  13 + @@cache_timeout_values[key] = expire
  14 + end
  15 + cache_erb_fragment(block,name)
  16 + end
  17 + #handles the expiration of timeout fragment
  18 + def expire_timeout_fragment(key)
  19 + @@cache_timeout_values[key] = nil
  20 + expire_fragment(key)
  21 + end
  22 + #checks to see if a cache has fully expired
  23 + def is_cache_expired?(name, is_key = false)
  24 + key = is_key ? fragment_cache_key(name) : name
  25 + return (!@@cache_timeout_values.has_key?(key)) || (@@cache_timeout_values[key] < Time.now)
  26 + end
  27 + end
  28 + end
  29 +end
  30 +
  31 +
  32 +module ActionView
  33 + module Helpers
  34 + module TimedCacheHelper
  35 + def is_cache_expired?(name = nil)
  36 + return false if name.nil?
  37 + key = fragment_cache_key(name)
  38 + return @controller.send('is_cache_expired?', key)
  39 + end
  40 + def cache_timeout(name,expire=10.minutes.from_now, &block)
  41 + @controller.cache_timeout(name,expire,&block)
  42 + end
  43 + end
  44 + end
  45 +end
  46 +
  47 +#add to the respective controllers
  48 +ActionView::Base.send(:include, ActionView::Helpers::TimedCacheHelper)
  49 +ActionController::Base.send(:include, ActionController::Cache::TimedCache)
0 50 \ No newline at end of file
... ...
vendor/plugins/timed_cached_fragment/tasks/timed_cache_fragment_tasks.rake 0 → 100644
... ... @@ -0,0 +1,4 @@
  1 +# desc "Explaining what the task does"
  2 +# task :timed_cache_fragment do
  3 +# # Task goes here
  4 +# end
0 5 \ No newline at end of file
... ...
vendor/plugins/timed_cached_fragment/test/timed_cache_fragment_test.rb 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +require 'test/unit'
  2 +
  3 +class TimedCacheFragmentTest < Test::Unit::TestCase
  4 + # Replace this with your real tests.
  5 + def test_this_plugin
  6 + flunk
  7 + end
  8 +end
... ...
vendor/plugins/timed_cached_fragment/uninstall.rb 0 → 100644
... ... @@ -0,0 +1 @@
  1 +# Uninstall hook code here
... ...