padrino-cache 0.6.1 → 0.9.16

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -19,3 +19,5 @@ rdoc
19
19
  pkg
20
20
 
21
21
  ## PROJECT::SPECIFIC
22
+ *.rdb
23
+ test/tmp/*
data/LICENSE CHANGED
@@ -17,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
17
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
18
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
19
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc CHANGED
@@ -1,22 +1,7 @@
1
- = padrino-cache
2
-
3
- == Installation
4
-
5
- To install the 'full-stack' padrino framework, simply grab the latest version from gemcutter:
6
-
7
- $ sudo gem install padrino --source http://gemcutter.org
8
-
9
- This will install the necessary padrino gems to get you started.
10
- Now you are ready to use this gem to enhance your existing Sinatra projects or build new Padrino applications.
11
-
12
- You can also install only the padrino-cache gem for more fine-grained use:
13
-
14
- $ sudo gem install padrino-cache --source http://gemcutter.org
15
-
16
- == Usage
1
+ = Painless Page and Fragment Caching (padrino-cache)
17
2
 
18
3
  Not implemented yet.
19
4
 
20
5
  == Copyright
21
6
 
22
- Copyright (c) 2010 Padrino. See LICENSE for details.
7
+ Copyright (c) 2010 Padrino. See LICENSE for details.
data/Rakefile CHANGED
@@ -1,61 +1,5 @@
1
- require 'rubygems'
2
- require 'rake'
3
-
4
- GEM_VERSION = File.read(File.dirname(__FILE__) + '/VERSION')
5
-
6
- begin
7
- require 'jeweler'
8
- Jeweler::Tasks.new do |gem|
9
- gem.name = "padrino-cache"
10
- gem.summary = "Page and fragment caching for Padrino"
11
- gem.description = "Caching support for memcached, page and fragment"
12
- gem.email = "nesquena@gmail.com"
13
- gem.homepage = "http://github.com/padrino/padrino-framework/tree/master/padrino-cache"
14
- gem.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
15
- gem.add_runtime_dependency "sinatra", ">= 0.9.2"
16
- gem.add_runtime_dependency "padrino-core", "= #{GEM_VERSION}"
17
- gem.add_development_dependency "haml", ">= 2.2.1"
18
- gem.add_development_dependency "shoulda", ">= 0"
19
- gem.add_development_dependency "mocha", ">= 0.9.7"
20
- gem.add_development_dependency "rack-test", ">= 0.5.0"
21
- gem.add_development_dependency "webrat", ">= 0.5.1"
22
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
23
- end
24
- Jeweler::GemcutterTasks.new
25
- rescue LoadError
26
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
27
- end
28
-
29
- require 'rake/testtask'
30
- Rake::TestTask.new(:test) do |test|
31
- test.libs << 'test'
32
- test.pattern = 'test/**/test_*.rb'
33
- test.verbose = true
34
- end
35
-
36
- begin
37
- require 'rcov/rcovtask'
38
- Rcov::RcovTask.new do |test|
39
- test.libs << 'test'
40
- test.pattern = 'test/**/test_*.rb'
41
- test.verbose = true
42
- end
43
- rescue LoadError
44
- task :rcov do
45
- abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
46
- end
47
- end
1
+ # coding:utf-8
2
+ RAKE_ROOT = __FILE__
48
3
 
49
- task :test => :check_dependencies
50
-
51
- task :default => :test
52
-
53
- require 'rake/rdoctask'
54
- Rake::RDocTask.new do |rdoc|
55
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
56
-
57
- rdoc.rdoc_dir = 'rdoc'
58
- rdoc.title = "padrino-admin #{version}"
59
- rdoc.rdoc_files.include('README*')
60
- rdoc.rdoc_files.include('lib/**/*.rb')
61
- end
4
+ require 'rubygems'
5
+ require File.expand_path(File.dirname(__FILE__) + '/../gem_rake_helper')
@@ -0,0 +1,15 @@
1
+ module Padrino
2
+ module Cache
3
+ module Helpers
4
+ module CacheStore
5
+ def expire(*key)
6
+ if key.size == 1 and key.first.is_a?(String)
7
+ self.class.cache_store.delete(key)
8
+ else
9
+ self.class.cache_store.delete(self.class.url(*key))
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ module Padrino
2
+ module Cache
3
+ module Helpers
4
+ module Fragment
5
+ include Padrino::Helpers::OutputHelpers
6
+
7
+ def cache(key, opts = nil, &block)
8
+ if self.class.caching?
9
+ if value = self.class.cache_store.get(key)
10
+ concat_content(value)
11
+ else
12
+ value = capture_html(&block)
13
+ self.class.cache_store.set(key, value, opts)
14
+ concat_content(value)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,31 @@
1
+ module Padrino
2
+ module Cache
3
+ module Helpers
4
+ module Page
5
+ def expires_in(time)
6
+ @_last_expires_in = time
7
+ end
8
+ def self.padrino_route_added(route, verb, path, args, options, block)
9
+ if route.cache and %w(GET HEAD).include?(verb)
10
+ route.add_before_filter(Proc.new {
11
+ if self.class.caching?
12
+ value = self.class.cache_store.get(route.cache.respond_to?(:call) ? route.cache.call(request) : env['PATH_INFO'])
13
+ halt 200, value if value
14
+ end
15
+ })
16
+ route.add_after_filter(Proc.new { |something|
17
+ if self.class.caching?
18
+ if @_last_expires_in
19
+ self.class.cache_store.set(route.cache.respond_to?(:call) ? route.cache.call(request) : env['PATH_INFO'], @_response_buffer, :expires_in => @_last_expires_in)
20
+ @_last_expires_in = nil
21
+ else
22
+ self.class.cache_store.set(route.cache.respond_to?(:call) ? route.cache.call(request) : env['PATH_INFO'], @_response_buffer)
23
+ end
24
+ end
25
+ })
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,61 @@
1
+ module Padrino
2
+ module Cache
3
+ module Store
4
+ class File
5
+ def initialize(root)
6
+ @root = root
7
+ end
8
+
9
+ def get(key)
10
+ init
11
+ if ::File.exist?(path_for_key(key))
12
+ contents = ::File.read(path_for_key(key))
13
+ expires_in, body = contents.split("\n", 2)
14
+ expires_in = expires_in.to_i
15
+ if expires_in == -1 or Time.new.to_i < expires_in
16
+ body
17
+ else
18
+ delete(key)
19
+ nil
20
+ end
21
+ else
22
+ nil
23
+ end
24
+ end
25
+
26
+ def set(key, value, opts = nil)
27
+ init
28
+ if opts && opts[:expires_in]
29
+ expires_in = opts[:expires_in].to_i
30
+ expires_in = Time.new.to_i + expires_in if expires_in < EXPIRES_EDGE
31
+ else
32
+ expires_in = -1
33
+ end
34
+ ::File.open(path_for_key(key), 'w') { |f| f << expires_in.to_s << "\n" << value.to_s } if value
35
+ end
36
+
37
+ def delete(key)
38
+ init
39
+ FileUtils.rm_rf(path_for_key(key))
40
+ end
41
+
42
+ def flush
43
+ FileUtils.rm_rf(@root)
44
+ end
45
+
46
+ private
47
+ def path_for_key(key)
48
+ ::File.join(@root, Rack::Utils.escape(key.to_s))
49
+ end
50
+
51
+ def init
52
+ unless @init
53
+ FileUtils.rm_rf(@root)
54
+ FileUtils.mkdir_p(@root)
55
+ @init = true
56
+ end
57
+ end
58
+ end # File
59
+ end # Store
60
+ end # Cache
61
+ end # Padrino
@@ -0,0 +1,43 @@
1
+ begin
2
+ require 'memcached'
3
+ rescue LoadError
4
+ raise "You must install memecached to use the Memecache cache store backend"
5
+ end
6
+
7
+ module Padrino
8
+ module Cache
9
+ module Store
10
+ class Memcache
11
+ def initialize(*args)
12
+ @backend = Memcached.new(*args)
13
+ rescue
14
+ raise
15
+ end
16
+
17
+ def get(key)
18
+ @backend.get(key)
19
+ rescue Memcached::NotFound
20
+ nil
21
+ end
22
+
23
+ def set(key, value, opts = nil)
24
+ if opts && opts[:expires_in]
25
+ expires_in = opts[:expires_in].to_i
26
+ expires_in = Time.new.to_i + expires_in if expires_in < EXPIRES_EDGE
27
+ @backend.set(key, value, expires_in)
28
+ else
29
+ @backend.set(key, value)
30
+ end
31
+ end
32
+
33
+ def delete(key)
34
+ @backend.delete(key)
35
+ end
36
+
37
+ def flush
38
+ @backend.flush
39
+ end
40
+ end # Memcached
41
+ end # Store
42
+ end # Cache
43
+ end # Padrino
@@ -0,0 +1,50 @@
1
+ module Padrino
2
+ module Cache
3
+ module Store
4
+ class Memory
5
+ def initialize(size = 5000)
6
+ @size, @entries, @index = size, [], {}
7
+ end
8
+
9
+ def get(key)
10
+ if @index.key?(key) and value = @index[key]
11
+ expires_in, body = value
12
+ if expires_in == -1 or Time.new.to_i < expires_in
13
+ set(key, body, :expires_in => expires_in)
14
+ body
15
+ else
16
+ delete(key)
17
+ nil
18
+ end
19
+ else
20
+ nil
21
+ end
22
+ end
23
+
24
+ def set(key, value, opts = nil)
25
+ delete(key) if @index.key?(key)
26
+ if opts && opts[:expires_in]
27
+ expires_in = opts[:expires_in].to_i
28
+ expires_in = Time.new.to_i + expires_in if expires_in < EXPIRES_EDGE
29
+ else
30
+ expires_in = -1
31
+ end
32
+ @entries.push(key)
33
+ @index[key] = [expires_in, value]
34
+
35
+ while @entries.size > @size
36
+ delete(@entries.shift)
37
+ end
38
+ end
39
+
40
+ def delete(key)
41
+ @index.delete(key)
42
+ end
43
+
44
+ def flush
45
+ @index = Hash.new
46
+ end
47
+ end # Memory
48
+ end # Store
49
+ end # Cache
50
+ end # Padrino
@@ -0,0 +1,38 @@
1
+ begin
2
+ require 'redis'
3
+ rescue LoadError
4
+ raise "You must install redis to use the Redis cache store backend"
5
+ end
6
+
7
+ module Padrino
8
+ module Cache
9
+ module Store
10
+ class Redis
11
+ def initialize(*args)
12
+ @backend = ::Redis.new(*args)
13
+ end
14
+
15
+ def get(key)
16
+ @backend.get(key)
17
+ end
18
+
19
+ def set(key, value, opts = nil)
20
+ if opts && opts[:expires_in]
21
+ expires_in = opts[:expires_in].to_i
22
+ @backend.setex(key, expires_in, value)
23
+ else
24
+ @backend.set(key, value)
25
+ end
26
+ end
27
+
28
+ def delete(key)
29
+ @backend.del(key)
30
+ end
31
+
32
+ def flush
33
+ @backend.flushdb
34
+ end
35
+ end # Redis
36
+ end # Store
37
+ end # Cache
38
+ end # Padrino
@@ -0,0 +1,12 @@
1
+ module Padrino
2
+ module Cache
3
+ module Store
4
+ EXPIRES_EDGE = 86400
5
+
6
+ autoload :File, 'padrino-cache/store/file'
7
+ autoload :Memcache, 'padrino-cache/store/memcache'
8
+ autoload :Memory, 'padrino-cache/store/memory'
9
+ autoload :Redis, 'padrino-cache/store/redis'
10
+ end # Store
11
+ end # Cache
12
+ end # Padrino
data/lib/padrino-cache.rb CHANGED
@@ -0,0 +1,35 @@
1
+ require 'fileutils'
2
+ require 'padrino-core'
3
+ require 'padrino-helpers'
4
+ #FileSet.glob_require('padrino-cache/{helpers}/*.rb', __FILE__)
5
+ Dir.glob(File.dirname(__FILE__) + '/padrino-cache/helpers/*.rb') {|file| require file}
6
+
7
+ module Padrino
8
+ module Cache
9
+ ##
10
+ # Register these helpers:
11
+ #
12
+ # Padrino::Cache::FragmentHelpers
13
+ # Padrino::Cache::PageHelpers
14
+ #
15
+ # for Padrino::Application
16
+ #
17
+
18
+ autoload :Store, 'padrino-cache/store'
19
+
20
+ class << self
21
+ def registered(app)
22
+ app.helpers Padrino::Cache::Helpers::CacheStore
23
+ app.helpers Padrino::Cache::Helpers::Fragment
24
+ app.helpers Padrino::Cache::Helpers::Page
25
+ app.set :cache_store, Padrino::Cache::Store::File.new(File.join(app.root, 'tmp', 'cache'))
26
+ app.disable :caching
27
+ end
28
+ alias :included :registered
29
+
30
+ def padrino_route_added(route, verb, path, args, options, block)
31
+ Padrino::Cache::Helpers::Page.padrino_route_added(route, verb, path, args, options, block)
32
+ end
33
+ end
34
+ end # Helpers
35
+ end # Padrino
@@ -1,67 +1,19 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
- # -*- encoding: utf-8 -*-
1
+ require File.expand_path("../../padrino-core/lib/padrino-core/version.rb", __FILE__)
5
2
 
6
3
  Gem::Specification.new do |s|
7
- s.name = %q{padrino-cache}
8
- s.version = "0.6.1"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
4
+ s.name = "padrino-cache"
5
+ s.rubyforge_project = "padrino-cache"
11
6
  s.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
12
- s.date = %q{2010-01-14}
13
- s.description = %q{Caching support for memcached, page and fragment}
14
- s.email = %q{nesquena@gmail.com}
15
- s.extra_rdoc_files = [
16
- "README.rdoc"
17
- ]
18
- s.files = [
19
- ".document",
20
- ".gitignore",
21
- "LICENSE",
22
- "README.rdoc",
23
- "Rakefile",
24
- "VERSION",
25
- "lib/padrino-cache.rb",
26
- "padrino-cache.gemspec",
27
- "test/helper.rb",
28
- "test/test_padrino_cache.rb"
29
- ]
30
- s.homepage = %q{http://github.com/padrino/padrino-framework/tree/master/padrino-cache}
7
+ s.email = "padrinorb@gmail.com"
8
+ s.summary = "Page and fragment caching for Padrino"
9
+ s.homepage = "http://www.padrinorb.com"
10
+ s.description = "Caching support for memcached, page and fragment"
11
+ s.required_rubygems_version = ">= 1.3.6"
12
+ s.version = Padrino.version
13
+ s.date = Time.now.strftime("%Y-%m-%d")
14
+ s.extra_rdoc_files = Dir["*.rdoc"]
15
+ s.files = %w(.document .gitignore LICENSE README.rdoc Rakefile padrino-cache.gemspec) + Dir.glob("{bin,lib,test}/**/*")
31
16
  s.rdoc_options = ["--charset=UTF-8"]
32
- s.require_paths = ["lib"]
33
- s.rubygems_version = %q{1.3.5}
34
- s.summary = %q{Page and fragment caching for Padrino}
35
-
36
- if s.respond_to? :specification_version then
37
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
38
- s.specification_version = 3
39
-
40
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
41
- s.add_runtime_dependency(%q<sinatra>, [">= 0.9.2"])
42
- s.add_runtime_dependency(%q<padrino-core>, ["= 0.6.1"])
43
- s.add_development_dependency(%q<haml>, [">= 2.2.1"])
44
- s.add_development_dependency(%q<shoulda>, [">= 0"])
45
- s.add_development_dependency(%q<mocha>, [">= 0.9.7"])
46
- s.add_development_dependency(%q<rack-test>, [">= 0.5.0"])
47
- s.add_development_dependency(%q<webrat>, [">= 0.5.1"])
48
- else
49
- s.add_dependency(%q<sinatra>, [">= 0.9.2"])
50
- s.add_dependency(%q<padrino-core>, ["= 0.6.1"])
51
- s.add_dependency(%q<haml>, [">= 2.2.1"])
52
- s.add_dependency(%q<shoulda>, [">= 0"])
53
- s.add_dependency(%q<mocha>, [">= 0.9.7"])
54
- s.add_dependency(%q<rack-test>, [">= 0.5.0"])
55
- s.add_dependency(%q<webrat>, [">= 0.5.1"])
56
- end
57
- else
58
- s.add_dependency(%q<sinatra>, [">= 0.9.2"])
59
- s.add_dependency(%q<padrino-core>, ["= 0.6.1"])
60
- s.add_dependency(%q<haml>, [">= 2.2.1"])
61
- s.add_dependency(%q<shoulda>, [">= 0"])
62
- s.add_dependency(%q<mocha>, [">= 0.9.7"])
63
- s.add_dependency(%q<rack-test>, [">= 0.5.0"])
64
- s.add_dependency(%q<webrat>, [">= 0.5.1"])
65
- end
66
- end
67
-
17
+ s.require_path = "lib"
18
+ s.add_runtime_dependency("padrino-core", Padrino.version)
19
+ end
data/test/helper.rb CHANGED
@@ -1,67 +1,25 @@
1
- require 'rubygems'
2
- require 'test/unit'
3
- require 'shoulda'
4
- require 'mocha'
5
- require 'rack/test'
6
- require 'webrat'
1
+ ENV['PADRINO_ENV'] = 'test'
2
+ PADRINO_ROOT = File.dirname(__FILE__) unless defined? PADRINO_ROOT
7
3
 
4
+ require File.expand_path('../../../load_paths', __FILE__)
5
+ require File.join(File.dirname(__FILE__), '..', '..', 'padrino-core', 'test', 'helper')
8
6
  require 'padrino-cache'
7
+ require 'fileutils'
8
+ require 'uuid'
9
9
 
10
10
  class Test::Unit::TestCase
11
- include Rack::Test::Methods
12
- include Webrat::Methods
13
- include Webrat::Matchers
14
11
 
15
- Webrat.configure do |config|
16
- config.mode = :rack
17
- end
18
-
19
- def stop_time_for_test
20
- time = Time.now
21
- Time.stubs(:now).returns(time)
22
- return time
23
- end
12
+ def executable_on_path(binary)
13
+ @matches = []
24
14
 
25
- # assert_has_tag(:h1, :content => "yellow") { "<h1>yellow</h1>" }
26
- # In this case, block is the html to evaluate
27
- def assert_has_tag(name, attributes = {}, &block)
28
- html = block && block.call
29
- matcher = HaveSelector.new(name, attributes)
30
- raise "Please specify a block!" if html.blank?
31
- assert matcher.matches?(html), matcher.failure_message
32
- end
15
+ ENV['PATH'].split(":").each do |path|
16
+ bintest = File.executable?("#{path}/#{binary}")
17
+ pathmatch = "#{path}/#{binary}"
18
+ @matches << pathmatch if bintest == true
19
+ end
33
20
 
34
- # assert_has_no_tag, tag(:h1, :content => "yellow") { "<h1>green</h1>" }
35
- # In this case, block is the html to evaluate
36
- def assert_has_no_tag(name, attributes = {}, &block)
37
- html = block && block.call
38
- attributes.merge!(:count => 0)
39
- matcher = HaveSelector.new(name, attributes)
40
- raise "Please specify a block!" if html.blank?
41
- assert matcher.matches?(html), matcher.failure_message
42
- end
21
+ @matches.length == 1 ? @matches.first : false
43
22
 
44
- # Silences the output by redirecting to stringIO
45
- # silence_logger { ...commands... } => "...output..."
46
- def silence_logger(&block)
47
- orig_stdout = $stdout
48
- $stdout = log_buffer = StringIO.new
49
- block.call
50
- $stdout = orig_stdout
51
- log_buffer.rewind && log_buffer.read
52
23
  end
53
24
 
54
- # Asserts that a file matches the pattern
55
- def assert_match_in_file(pattern, file)
56
- assert File.exist?(file), "File '#{file}' does not exist!"
57
- assert_match pattern, File.read(file)
58
- end
59
25
  end
60
-
61
- module Webrat
62
- module Logging
63
- def logger # :nodoc:
64
- @logger = nil
65
- end
66
- end
67
- end
@@ -1,7 +1,186 @@
1
- require File.dirname(__FILE__) + '/helper'
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
2
 
3
3
  class TestPadrinoCache < Test::Unit::TestCase
4
- should "probably rename this file and start testing for real" do
4
+ should 'cache a fragment' do
5
+ called = false
6
+ mock_app do
7
+ register Padrino::Cache
8
+ enable :caching
9
+ get("/foo"){ cache(:test) { called ? halt(500) : (called = 'test fragment') } }
10
+ end
11
+ get "/foo"
12
+ assert_equal 200, status
13
+ assert_equal 'test fragment', body
14
+ get "/foo"
15
+ assert_equal 200, status
16
+ assert_equal 'test fragment', body
17
+ assert_not_equal called, false
18
+ end
19
+
20
+ should 'cache a page' do
21
+ called = false
22
+ mock_app do
23
+ register Padrino::Cache
24
+ enable :caching
25
+ get('/foo', :cache => true){ called ? halt(500) : (called = 'test page') }
26
+ end
27
+ get "/foo"
28
+ assert_equal 200, status
29
+ assert_equal 'test page', body
30
+ get "/foo"
31
+ assert_equal 200, status
32
+ assert_equal 'test page', body
33
+ assert_not_equal false, called
34
+ end
35
+
36
+ should 'delete from the cache' do
37
+ called = false
38
+ mock_app do
39
+ register Padrino::Cache
40
+ enable :caching
41
+ get('/foo', :cache => true){ called ? 'test page again' : (called = 'test page') }
42
+ get('/delete_foo'){ expire('/foo') }
43
+ end
44
+ get "/foo"
45
+ assert_equal 200, status
46
+ assert_equal 'test page', body
47
+ get "/delete_foo"
48
+ get "/foo"
49
+ assert_equal 200, status
50
+ assert_equal 'test page again', body
51
+ assert_not_equal false, called
52
+ end
53
+
54
+ should 'accept custom cache keys' do
55
+ mock_app do
56
+ register Padrino::Cache
57
+ enable :caching
58
+ get('/foo', :cache => proc{|req| "cached"}){ 'test' }
59
+ get('/bar', :cache => proc{|req| "cached"}){ halt 500 }
60
+ end
61
+ get "/foo"
62
+ assert_equal 200, status
63
+ assert_equal 'test', body
64
+ get "/bar"
65
+ assert_equal 200, status
66
+ assert_equal 'test', body
67
+ end
68
+
69
+ should 'delete based on urls' do
70
+ called = false
71
+ mock_app do
72
+ register Padrino::Cache
73
+ enable :caching
74
+ get(:foo, :with => :id, :cache => true) { called ? 'test page again' : (called = 'test page') }
75
+ get(:delete_foo, :with => :id) { expire(:foo, params[:id]) }
76
+ end
77
+ get "/foo/12"
78
+ assert_equal 200, status
79
+ assert_equal 'test page', body
80
+ get "/delete_foo/12"
81
+ get "/foo/12"
82
+ assert_equal 200, status
83
+ assert_equal 'test page again', body
84
+ end
5
85
 
86
+ should 'accept allow controller-wide caching' do
87
+ called = false
88
+ mock_app do
89
+ controller :cache => true do
90
+ register Padrino::Cache
91
+ enable :caching
92
+ get("/foo"){ called ? halt(500) : (called = 'test') }
93
+ end
94
+ end
95
+ get "/foo"
96
+ assert_equal 200, status
97
+ assert_equal 'test', body
98
+ get "/foo"
99
+ assert_equal 200, status
100
+ assert_equal 'test', body
6
101
  end
7
- end
102
+
103
+ should 'allow cache disabling on a per route basis' do
104
+ called = false
105
+ mock_app do
106
+ register Padrino::Cache
107
+ enable :caching
108
+ controller :cache => true do
109
+ get("/foo", :cache => false){ called ? 'test again' : (called = 'test') }
110
+ end
111
+ end
112
+ get "/foo"
113
+ assert_equal 200, status
114
+ assert_equal 'test', body
115
+ get "/foo"
116
+ assert_equal 200, status
117
+ assert_equal 'test again', body
118
+ end
119
+
120
+ should 'allow expiring for pages' do
121
+ called = false
122
+ mock_app do
123
+ register Padrino::Cache
124
+ enable :caching
125
+ controller :cache => true do
126
+ get("/foo") {
127
+ expires_in 1
128
+ called ? 'test again' : (called = 'test')
129
+ }
130
+ end
131
+ end
132
+ get "/foo"
133
+ assert_equal 200, status
134
+ assert_equal 'test', body
135
+ get "/foo"
136
+ assert_equal 200, status
137
+ assert_equal 'test', body
138
+ sleep 2
139
+ get "/foo"
140
+ assert_equal 200, status
141
+ assert_equal 'test again', body
142
+ end
143
+
144
+ should 'allow expiring for fragments' do
145
+ called = false
146
+ mock_app do
147
+ register Padrino::Cache
148
+ enable :caching
149
+ controller do
150
+ get("/foo") {
151
+ expires_in 1
152
+ cache(:test, :expires_in => 2) do
153
+ called ? 'test again' : (called = 'test')
154
+ end
155
+ }
156
+ end
157
+ end
158
+ get "/foo"
159
+ assert_equal 200, status
160
+ assert_equal 'test', body
161
+ get "/foo"
162
+ assert_equal 200, status
163
+ assert_equal 'test', body
164
+ sleep 2
165
+ get "/foo"
166
+ assert_equal 200, status
167
+ assert_equal 'test again', body
168
+ end
169
+
170
+ should 'allow disabling of the cache' do
171
+ called = false
172
+ mock_app do
173
+ register Padrino::Cache
174
+ disable :caching
175
+ controller :cache => true do
176
+ get("/foo"){ called ? halt(500) : (called = 'test') }
177
+ end
178
+ end
179
+ get "/foo"
180
+ assert_equal 200, status
181
+ assert_equal 'test', body
182
+ get "/foo"
183
+ assert_equal 500, status
184
+ end
185
+
186
+ end
@@ -0,0 +1,98 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/helper')
2
+
3
+ COMMON_TESTS = <<-HERE_DOC
4
+ should 'set and get a value' do
5
+ @cache.set('val', 'test')
6
+ assert_equal 'test', @cache.get('val')
7
+ end
8
+
9
+ should "return nil trying to get a value that doesn't exist" do
10
+ assert_equal nil, @cache.get('test')
11
+ end
12
+
13
+ should "set a value that expires" do
14
+ @cache.set('val', 'test', :expires_in => 1)
15
+ assert_equal 'test', @cache.get('val')
16
+ sleep 2
17
+ assert_equal nil, @cache.get('val')
18
+ end
19
+
20
+ should 'delete a value' do
21
+ @cache.set('val', 'test')
22
+ assert_equal 'test', @cache.get('val')
23
+ @cache.delete('val')
24
+ assert_equal nil, @cache.get('val')
25
+ end
26
+ HERE_DOC
27
+
28
+ begin
29
+ # we're just going to assume memcached is running on the default port
30
+ Padrino::Cache::Store::Memcache.new('127.0.0.1:11211', :exception_retry_limit => 1).set('ping','alive')
31
+
32
+ class TestMemcacheStore < Test::Unit::TestCase
33
+ def setup
34
+ @cache = Padrino::Cache::Store::Memcache.new('127.0.0.1:11211', :exception_retry_limit => 1)
35
+ @cache.flush
36
+ end
37
+
38
+ def teardown
39
+ @cache.flush
40
+ end
41
+
42
+ eval COMMON_TESTS
43
+ end
44
+ rescue
45
+ warn "Skipping memcached tests"
46
+ end
47
+
48
+
49
+ begin
50
+ Padrino::Cache::Store::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0).set('ping','alive')
51
+ class TestRedisStore < Test::Unit::TestCase
52
+ def setup
53
+ @cache = Padrino::Cache::Store::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0)
54
+ @cache.flush
55
+ end
56
+
57
+ def teardown
58
+ @cache.flush
59
+ end
60
+
61
+ eval COMMON_TESTS
62
+ end
63
+ rescue
64
+ warn "Skipping redis tests"
65
+ end
66
+
67
+ class TestFileStore < Test::Unit::TestCase
68
+ def setup
69
+ @apptmp = "#{Dir.tmpdir}/padrino-tests/#{UUID.new.generate}"
70
+ FileUtils.mkdir_p(@apptmp)
71
+ @cache = Padrino::Cache::Store::File.new(@apptmp)
72
+ end
73
+
74
+ def teardown
75
+ @cache.flush
76
+ end
77
+
78
+ eval COMMON_TESTS
79
+ end
80
+
81
+ class TestInMemoryStore < Test::Unit::TestCase
82
+ def setup
83
+ @cache = Padrino::Cache::Store::Memory.new(50)
84
+ end
85
+
86
+ def teardown
87
+ @cache.flush
88
+ end
89
+
90
+ eval COMMON_TESTS
91
+
92
+ should "only store 50 entries" do
93
+ 51.times { |i| @cache.set(i.to_s, i.to_s) }
94
+ assert_equal nil, @cache.get('0')
95
+ assert_equal '1', @cache.get('1')
96
+ end
97
+
98
+ end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 9
9
+ - 16
10
+ version: 0.9.16
5
11
  platform: ruby
6
12
  authors:
7
13
  - Padrino Team
@@ -12,81 +18,27 @@ autorequire:
12
18
  bindir: bin
13
19
  cert_chain: []
14
20
 
15
- date: 2010-01-14 00:00:00 +01:00
21
+ date: 2010-09-24 00:00:00 +02:00
16
22
  default_executable:
17
23
  dependencies:
18
- - !ruby/object:Gem::Dependency
19
- name: sinatra
20
- type: :runtime
21
- version_requirement:
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: 0.9.2
27
- version:
28
24
  - !ruby/object:Gem::Dependency
29
25
  name: padrino-core
30
- type: :runtime
31
- version_requirement:
32
- version_requirements: !ruby/object:Gem::Requirement
26
+ prerelease: false
27
+ requirement: &id001 !ruby/object:Gem::Requirement
28
+ none: false
33
29
  requirements:
34
30
  - - "="
35
31
  - !ruby/object:Gem::Version
36
- version: 0.6.1
37
- version:
38
- - !ruby/object:Gem::Dependency
39
- name: haml
40
- type: :development
41
- version_requirement:
42
- version_requirements: !ruby/object:Gem::Requirement
43
- requirements:
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- version: 2.2.1
47
- version:
48
- - !ruby/object:Gem::Dependency
49
- name: shoulda
50
- type: :development
51
- version_requirement:
52
- version_requirements: !ruby/object:Gem::Requirement
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: "0"
57
- version:
58
- - !ruby/object:Gem::Dependency
59
- name: mocha
60
- type: :development
61
- version_requirement:
62
- version_requirements: !ruby/object:Gem::Requirement
63
- requirements:
64
- - - ">="
65
- - !ruby/object:Gem::Version
66
- version: 0.9.7
67
- version:
68
- - !ruby/object:Gem::Dependency
69
- name: rack-test
70
- type: :development
71
- version_requirement:
72
- version_requirements: !ruby/object:Gem::Requirement
73
- requirements:
74
- - - ">="
75
- - !ruby/object:Gem::Version
76
- version: 0.5.0
77
- version:
78
- - !ruby/object:Gem::Dependency
79
- name: webrat
80
- type: :development
81
- version_requirement:
82
- version_requirements: !ruby/object:Gem::Requirement
83
- requirements:
84
- - - ">="
85
- - !ruby/object:Gem::Version
86
- version: 0.5.1
87
- version:
32
+ hash: 27
33
+ segments:
34
+ - 0
35
+ - 9
36
+ - 16
37
+ version: 0.9.16
38
+ type: :runtime
39
+ version_requirements: *id001
88
40
  description: Caching support for memcached, page and fragment
89
- email: nesquena@gmail.com
41
+ email: padrinorb@gmail.com
90
42
  executables: []
91
43
 
92
44
  extensions: []
@@ -99,13 +51,21 @@ files:
99
51
  - LICENSE
100
52
  - README.rdoc
101
53
  - Rakefile
102
- - VERSION
103
- - lib/padrino-cache.rb
104
54
  - padrino-cache.gemspec
55
+ - lib/padrino-cache/helpers/cache_store.rb
56
+ - lib/padrino-cache/helpers/fragment.rb
57
+ - lib/padrino-cache/helpers/page.rb
58
+ - lib/padrino-cache/store/file.rb
59
+ - lib/padrino-cache/store/memcache.rb
60
+ - lib/padrino-cache/store/memory.rb
61
+ - lib/padrino-cache/store/redis.rb
62
+ - lib/padrino-cache/store.rb
63
+ - lib/padrino-cache.rb
105
64
  - test/helper.rb
106
65
  - test/test_padrino_cache.rb
66
+ - test/test_stores.rb
107
67
  has_rdoc: true
108
- homepage: http://github.com/padrino/padrino-framework/tree/master/padrino-cache
68
+ homepage: http://www.padrinorb.com
109
69
  licenses: []
110
70
 
111
71
  post_install_message:
@@ -114,21 +74,29 @@ rdoc_options:
114
74
  require_paths:
115
75
  - lib
116
76
  required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
117
78
  requirements:
118
79
  - - ">="
119
80
  - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
120
84
  version: "0"
121
- version:
122
85
  required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
123
87
  requirements:
124
88
  - - ">="
125
89
  - !ruby/object:Gem::Version
126
- version: "0"
127
- version:
90
+ hash: 23
91
+ segments:
92
+ - 1
93
+ - 3
94
+ - 6
95
+ version: 1.3.6
128
96
  requirements: []
129
97
 
130
- rubyforge_project:
131
- rubygems_version: 1.3.5
98
+ rubyforge_project: padrino-cache
99
+ rubygems_version: 1.3.7
132
100
  signing_key:
133
101
  specification_version: 3
134
102
  summary: Page and fragment caching for Padrino
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.6.1