From a5acd4088630fb4c15525f82c63524cf65c76c46 Mon Sep 17 00:00:00 2001 From: Moises Machado Date: Fri, 20 Mar 2009 16:01:24 -0300 Subject: [PATCH] ActionItem955: included plugin for timed cache --- vendor/plugins/timed_cached_fragment/README | 29 +++++++++++++++++++++++++++++ vendor/plugins/timed_cached_fragment/Rakefile | 22 ++++++++++++++++++++++ vendor/plugins/timed_cached_fragment/init.rb | 2 ++ vendor/plugins/timed_cached_fragment/install.rb | 1 + vendor/plugins/timed_cached_fragment/lib/timed_cache_fragment.rb | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ vendor/plugins/timed_cached_fragment/tasks/timed_cache_fragment_tasks.rake | 4 ++++ vendor/plugins/timed_cached_fragment/test/timed_cache_fragment_test.rb | 8 ++++++++ vendor/plugins/timed_cached_fragment/uninstall.rb | 1 + 8 files changed, 116 insertions(+), 0 deletions(-) create mode 100644 vendor/plugins/timed_cached_fragment/README create mode 100644 vendor/plugins/timed_cached_fragment/Rakefile create mode 100644 vendor/plugins/timed_cached_fragment/init.rb create mode 100644 vendor/plugins/timed_cached_fragment/install.rb create mode 100644 vendor/plugins/timed_cached_fragment/lib/timed_cache_fragment.rb create mode 100644 vendor/plugins/timed_cached_fragment/tasks/timed_cache_fragment_tasks.rake create mode 100644 vendor/plugins/timed_cached_fragment/test/timed_cache_fragment_test.rb create mode 100644 vendor/plugins/timed_cached_fragment/uninstall.rb diff --git a/vendor/plugins/timed_cached_fragment/README b/vendor/plugins/timed_cached_fragment/README new file mode 100644 index 0000000..acceab2 --- /dev/null +++ b/vendor/plugins/timed_cached_fragment/README @@ -0,0 +1,29 @@ +TimedCacheFragment +================== + +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. + +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. + +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). + +Example usage: + +# This is last 5 minutes of posts from our forums: + <%cache_timeout('forum_listing',5.minutes.from_now) do %> + + <%end%> + +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. + +Example Usage within controller: + +def list + if is_cache_expired?('forum_listing') + @posts = Post.find(:all) + end +end \ No newline at end of file diff --git a/vendor/plugins/timed_cached_fragment/Rakefile b/vendor/plugins/timed_cached_fragment/Rakefile new file mode 100644 index 0000000..4c554b9 --- /dev/null +++ b/vendor/plugins/timed_cached_fragment/Rakefile @@ -0,0 +1,22 @@ +require 'rake' +require 'rake/testtask' +require 'rake/rdoctask' + +desc 'Default: run unit tests.' +task :default => :test + +desc 'Test the timed_cache_fragment plugin.' +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' + t.pattern = 'test/**/*_test.rb' + t.verbose = true +end + +desc 'Generate documentation for the timed_cache_fragment plugin.' +Rake::RDocTask.new(:rdoc) do |rdoc| + rdoc.rdoc_dir = 'rdoc' + rdoc.title = 'TimedCacheFragment' + rdoc.options << '--line-numbers' << '--inline-source' + rdoc.rdoc_files.include('README') + rdoc.rdoc_files.include('lib/**/*.rb') +end diff --git a/vendor/plugins/timed_cached_fragment/init.rb b/vendor/plugins/timed_cached_fragment/init.rb new file mode 100644 index 0000000..68d4918 --- /dev/null +++ b/vendor/plugins/timed_cached_fragment/init.rb @@ -0,0 +1,2 @@ +# Include hook code here +require 'timed_cache_fragment' \ No newline at end of file diff --git a/vendor/plugins/timed_cached_fragment/install.rb b/vendor/plugins/timed_cached_fragment/install.rb new file mode 100644 index 0000000..f7732d3 --- /dev/null +++ b/vendor/plugins/timed_cached_fragment/install.rb @@ -0,0 +1 @@ +# Install hook code here diff --git a/vendor/plugins/timed_cached_fragment/lib/timed_cache_fragment.rb b/vendor/plugins/timed_cached_fragment/lib/timed_cache_fragment.rb new file mode 100644 index 0000000..5850c54 --- /dev/null +++ b/vendor/plugins/timed_cached_fragment/lib/timed_cache_fragment.rb @@ -0,0 +1,49 @@ +# TimedCacheFragment +module ActionController + module Cache + module TimedCache + #used to store the associated timeout time of cache key + @@cache_timeout_values = {} + #handles standard ERB fragments used in RHTML + def cache_timeout(name={}, expire = 10.minutes.from_now, &block) + unless perform_caching then block.call; return end + key = fragment_cache_key(name) + if is_cache_expired?(key,true) + expire_timeout_fragment(key) + @@cache_timeout_values[key] = expire + end + cache_erb_fragment(block,name) + end + #handles the expiration of timeout fragment + def expire_timeout_fragment(key) + @@cache_timeout_values[key] = nil + expire_fragment(key) + end + #checks to see if a cache has fully expired + def is_cache_expired?(name, is_key = false) + key = is_key ? fragment_cache_key(name) : name + return (!@@cache_timeout_values.has_key?(key)) || (@@cache_timeout_values[key] < Time.now) + end + end + end +end + + +module ActionView + module Helpers + module TimedCacheHelper + def is_cache_expired?(name = nil) + return false if name.nil? + key = fragment_cache_key(name) + return @controller.send('is_cache_expired?', key) + end + def cache_timeout(name,expire=10.minutes.from_now, &block) + @controller.cache_timeout(name,expire,&block) + end + end + end +end + +#add to the respective controllers +ActionView::Base.send(:include, ActionView::Helpers::TimedCacheHelper) +ActionController::Base.send(:include, ActionController::Cache::TimedCache) \ No newline at end of file diff --git a/vendor/plugins/timed_cached_fragment/tasks/timed_cache_fragment_tasks.rake b/vendor/plugins/timed_cached_fragment/tasks/timed_cache_fragment_tasks.rake new file mode 100644 index 0000000..84c0b4a --- /dev/null +++ b/vendor/plugins/timed_cached_fragment/tasks/timed_cache_fragment_tasks.rake @@ -0,0 +1,4 @@ +# desc "Explaining what the task does" +# task :timed_cache_fragment do +# # Task goes here +# end \ No newline at end of file diff --git a/vendor/plugins/timed_cached_fragment/test/timed_cache_fragment_test.rb b/vendor/plugins/timed_cached_fragment/test/timed_cache_fragment_test.rb new file mode 100644 index 0000000..34fd4a8 --- /dev/null +++ b/vendor/plugins/timed_cached_fragment/test/timed_cache_fragment_test.rb @@ -0,0 +1,8 @@ +require 'test/unit' + +class TimedCacheFragmentTest < Test::Unit::TestCase + # Replace this with your real tests. + def test_this_plugin + flunk + end +end diff --git a/vendor/plugins/timed_cached_fragment/uninstall.rb b/vendor/plugins/timed_cached_fragment/uninstall.rb new file mode 100644 index 0000000..9738333 --- /dev/null +++ b/vendor/plugins/timed_cached_fragment/uninstall.rb @@ -0,0 +1 @@ +# Uninstall hook code here -- libgit2 0.21.2