forked from padrino/padrino-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
237 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ padrino_gems = [ | |
"padrino-helpers", | ||
"padrino-mailer", | ||
"padrino-admin", | ||
"padrino-cache", | ||
"padrino" | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'fileutils' | ||
require 'padrino-core' | ||
require 'padrino-helpers' | ||
FileSet.glob_require('padrino-cache/{helpers}/*.rb', __FILE__) | ||
|
||
module Padrino | ||
module Cache | ||
## | ||
# Register these helpers: | ||
# | ||
# Padrino::Cache::FragmentHelpers | ||
# Padrino::Cache::PageHelpers | ||
# | ||
# for Padrino::Application | ||
# | ||
|
||
autoload :Store, 'padrino-cache/store' | ||
|
||
class << self | ||
def registered(app) | ||
app.helpers Padrino::Cache::Helpers::CacheStore | ||
app.helpers Padrino::Cache::Helpers::Fragment | ||
app.helpers | ||
app.set :cache_store, Padrino::Cache::Store::File.new(File.join(app.root, 'tmp', 'cache')) | ||
end | ||
alias :included :registered | ||
|
||
def padrino_route_added(route, verb, path, args, options, block) | ||
Padrino::Cache::Helpers::Page.padrino_route_added(route, verb, path, args, options, block) | ||
end | ||
end | ||
end # Helpers | ||
end # Padrino |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Padrino | ||
module Cache | ||
module Helpers | ||
module CacheStore | ||
def expire(key) | ||
self.class.cache_store.delete(key) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Padrino | ||
module Cache | ||
module Helpers | ||
module Fragment | ||
include Padrino::Helpers::OutputHelpers | ||
|
||
def cache(key, &block) | ||
if value = self.class.cache_store.get(key) | ||
concat_content(value) | ||
else | ||
value = capture_html(&block) | ||
self.class.cache_store.set(key, value) | ||
concat_content(value) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Padrino | ||
module Cache | ||
module Helpers | ||
module Page | ||
def self.padrino_route_added(route, verb, path, args, options, block) | ||
if route.cache | ||
raise "Cachable routes must be GET or HEAD" unless %w(GET HEAD).include?(verb) | ||
route.add_before_filter(Proc.new { | ||
value = self.class.cache_store.get(env['PATH_INFO']) | ||
halt 200, value if value | ||
}) | ||
route.add_after_filter(Proc.new { |something| | ||
self.class.cache_store.set(request.env['PATH_INFO'], @_response_buffer) | ||
}) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module Padrino | ||
module Cache | ||
module Store | ||
autoload :File, 'padrino-cache/store/file' | ||
autoload :Memcache, 'padrino-cache/store/memcache' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Padrino | ||
module Cache | ||
module Store | ||
class File | ||
def initialize(root) | ||
@root = root | ||
end | ||
|
||
def get(key) | ||
init | ||
::File.exist?(path_for_key(key)) ? ::File.read(path_for_key(key)) : nil | ||
end | ||
|
||
def set(key, value) | ||
init | ||
::File.open(path_for_key(key), 'w') { |f| f << value.to_s } if value | ||
end | ||
|
||
def delete(key) | ||
init | ||
FileUtils.rm_rf(path_for_key(key)) | ||
end | ||
|
||
private | ||
def path_for_key(key) | ||
::File.join(@root, Rack::Utils.escape(key.to_s)) | ||
end | ||
|
||
def init | ||
unless @init | ||
FileUtils.rm_rf(@root) | ||
FileUtils.mkdir_p(@root) | ||
@init = true | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
begin | ||
require 'memcached' | ||
rescue LoadError | ||
raise "You must install memecached to use the Memecache cache store backend" | ||
end | ||
|
||
module Padrino | ||
module Cache | ||
module Store | ||
class File | ||
def initialize(*args) | ||
@backend = Memcached.new(*args) | ||
end | ||
|
||
def get(key) | ||
@backend.get(key) | ||
rescue Memcached::NotFound | ||
nil | ||
end | ||
|
||
def set(key, value) | ||
@backend.set(key, value) | ||
end | ||
|
||
def delete(key) | ||
@backend.delete(key) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,50 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/helper') | ||
|
||
class TestPadrinoCache < Test::Unit::TestCase | ||
should "probably rename this file and start testing for real" do | ||
should 'cache a fragment' do | ||
called = false | ||
mock_app do | ||
register Padrino::Cache | ||
get("/foo"){ cache(:test) { called ? halt(500) : (called = 'test fragment') } } | ||
end | ||
get "/foo" | ||
assert_equal 200, status | ||
assert_equal 'test fragment', body | ||
get "/foo" | ||
assert_equal 200, status | ||
assert_equal 'test fragment', body | ||
assert_not_equal called, false | ||
end | ||
|
||
should 'cache a page' do | ||
called = false | ||
mock_app do | ||
register Padrino::Cache | ||
get('/foo', :cache => true){ called ? halt(500) : (called = 'test fragment') } | ||
end | ||
get "/foo" | ||
assert_equal 200, status | ||
assert_equal 'test fragment', body | ||
get "/foo" | ||
assert_equal 200, status | ||
assert_equal 'test fragment', body | ||
assert_not_equal false, called | ||
end | ||
|
||
should 'delete from the cache' do | ||
called = false | ||
mock_app do | ||
register Padrino::Cache | ||
get('/foo', :cache => true){ called ? 'test fragment again' : (called = 'test fragment') } | ||
get('/delete_foo'){ expire('/foo') } | ||
end | ||
get "/foo" | ||
assert_equal 200, status | ||
assert_equal 'test fragment', body | ||
get "/delete_foo" | ||
get "/foo" | ||
assert_equal 200, status | ||
assert_equal 'test fragment again', body | ||
assert_not_equal false, called | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters