Permalink
Browse files

Merge pull request #1018 from minad/moneta-replace

replace Padrino::Cache::Store with Moneta
  • Loading branch information...
ujifgc committed Oct 15, 2013
2 parents 8a8cb19 + 7609271 commit 3e551c965f90e985d47d507fea9e6911ce813e3b
Showing with 279 additions and 1,006 deletions.
  1. +0 −13 Gemfile
  2. +27 −31 padrino-cache/README.rdoc
  3. +36 −29 padrino-cache/lib/padrino-cache.rb
  4. +1 −0 padrino-cache/lib/padrino-cache/helpers/cache_store.rb
  5. +6 −5 padrino-cache/lib/padrino-cache/helpers/fragment.rb
  6. +17 −12 padrino-cache/lib/padrino-cache/helpers/page.rb
  7. +68 −0 padrino-cache/lib/padrino-cache/legacy_store.rb
  8. +0 −36 padrino-cache/lib/padrino-cache/parser.rb
  9. +0 −18 padrino-cache/lib/padrino-cache/store.rb
  10. +0 −76 padrino-cache/lib/padrino-cache/store/base.rb
  11. +0 −112 padrino-cache/lib/padrino-cache/store/file.rb
  12. +0 −85 padrino-cache/lib/padrino-cache/store/memcache.rb
  13. +0 −95 padrino-cache/lib/padrino-cache/store/memory.rb
  14. +0 −127 padrino-cache/lib/padrino-cache/store/mongo.rb
  15. +0 −105 padrino-cache/lib/padrino-cache/store/redis.rb
  16. +1 −0 padrino-cache/padrino-cache.gemspec
  17. +0 −37 padrino-cache/test/helper.rb
  18. +0 −58 padrino-cache/test/stores/shared.rb
  19. +0 −28 padrino-cache/test/stores/test_dalli.rb
  20. +0 −18 padrino-cache/test/stores/test_file.rb
  21. +0 −22 padrino-cache/test/stores/test_memcache.rb
  22. +0 −19 padrino-cache/test/stores/test_memory.rb
  23. +0 −32 padrino-cache/test/stores/test_mongo.rb
  24. +0 −30 padrino-cache/test/stores/test_redis.rb
  25. +53 −0 padrino-cache/test/test_legacy_store.rb
  26. +53 −0 padrino-cache/test/test_moneta_store.rb
  27. +12 −12 padrino-cache/test/test_padrino_cache.rb
  28. +1 −1 padrino-core/lib/padrino-core/application/routing.rb
  29. +4 −5 padrino-gen/lib/padrino-gen/generators/app/app.rb.tt
View
13 Gemfile
@@ -46,19 +46,6 @@ group :development do
gem "lumberjack"
end
-group :cache do
- gem "redis", ">= 2.0.0"
- gem "mongo", ">= 1.3.1"
- platforms :mri do
- gem "bson_ext", ">= 1.3.1"
- gem 'dalli', ">= 1.0.2"
- gem "memcached", ">= 0.20.1"
- end
- platform :rbx do
- gem 'dalli', ">= 1.0.2"
- end
-end
-
%w[
padrino
padrino-admin
View
@@ -26,7 +26,7 @@ your needs, you can enable it very easily:
enable :caching
get '/foo', :cache => true do
- expires_in 30 # expire cached version at least every 30 seconds
+ expires 30 # expire cached version at least every 30 seconds
'Hello world'
end
end
@@ -44,7 +44,7 @@ You can also cache on a controller-wide basis:
# Requests to routes within '/admin'
controller '/admin', :cache => true do
- expires_in 60
+ expires 60
get '/foo' do
'Url is /admin/foo'
@@ -75,9 +75,9 @@ You can also provide a custom <tt>cache_key</tt> in any route:
In this way you can manually expire cache with CachedApp.cache.delete(:my_name)
for example from the Post model after an update.
-If you specify <tt>:cache => true</tt> but do not invoke <tt>expires_in</tt>,
+If you specify <tt>:cache => true</tt> but do not invoke <tt>expires</tt>,
the response will be cached indefinitely. Most of the time, you will want to
-specify the expiry of a cache entry by <tt>expires_in</tt>. Even a relatively
+specify the expiry of a cache entry by <tt>expires</tt>. Even a relatively
low value--1 or 2 seconds--can greatly increase application efficiency, especially
when enabled on a very active part of your domain.
@@ -110,7 +110,7 @@ easy to integrate into your application. To turn it on, simply provide the
By default, cached content is persisted with a "file store"--that is, in a
subdirectory of your application root.
-==== <tt>expires_in( seconds )</tt>
+==== <tt>expires( seconds )</tt>
This helper is used within a controller or route to indicate how often cached
<em>page-level</em> content should persist in the cache.
@@ -126,15 +126,15 @@ be executed; rather, its previous output will be sent to the client with a
enable :caching # turns on caching
controller '/blog', :cache => true do
- expires_in 15
+ expires 15
get '/entries' do
'just broke up eating twinkies lol'
end
end
end
-Note that the "latest" method call to <tt>expires_in</tt> determines its value: if
+Note that the "latest" method call to <tt>expires</tt> determines its value: if
called within a route, as opposed to a controller definition, the route's
value will be assumed.
@@ -165,7 +165,7 @@ to be cached. It can be used in within a route:
get :feed, :map => '/:username' do
username = params[:username]
- @feed = cache( "feed_for_#{username}", :expires_in => 3 ) do
+ @feed = cache( "feed_for_#{username}", :expires => 3 ) do
@tweets = Tweet.all( :username => username )
render 'partials/feedcontent'
end
@@ -192,7 +192,7 @@ Note that any other action will reference the same content if it uses the same k
get :feed, :map => '/:username' do
username = params[:username]
- @feed = cache( "feed_for_#{username}", :expires_in => 3 ) do
+ @feed = cache( "feed_for_#{username}", :expires => 3 ) do
@tweets = Tweet.all( :username => username )
render 'partials/feedcontent'
end
@@ -204,7 +204,7 @@ Note that any other action will reference the same content if it uses the same k
get :mobile_feed, :map => '/:username.iphone' do
username = params[:username]
- @feed = cache( "feed_for_#{username}", :expires_in => 3 ) do
+ @feed = cache( "feed_for_#{username}", :expires => 3 ) do
@tweets = Tweet.all( :username => username )
render 'partials/feedcontent'
end
@@ -214,7 +214,7 @@ Note that any other action will reference the same content if it uses the same k
end
end
-The <tt>opts</tt> argument is actually passed to the underlying store. All stores included with Padrino support the <tt>:expires_in</tt> option out of the box.
+The <tt>opts</tt> argument is actually passed to the underlying store. All stores included with Padrino support the <tt>:expires</tt> option out of the box.
Finally, to DRY up things a bit, we might do:
@@ -226,7 +226,7 @@ Finally, to DRY up things a bit, we might do:
controller :tweets do
# This works because all routes in this controller specify :username
before do
- @feed = cache( "feed_for_#{params[:username]}", :expires_in => 3 ) do
+ @feed = cache( "feed_for_#{params[:username]}", :expires => 3 ) do
@tweets = Tweet.all( :username => params[:username] )
render 'partials/feedcontent'
end
@@ -253,35 +253,31 @@ You can set a global caching option or a per app caching options.
=== Global Caching Options
- Padrino.cache = Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1))
- Padrino.cache = Padrino::Cache::Store::Memcache.new(::Dalli::Client.new('127.0.0.1:11211', :exception_retry_limit => 1))
- Padrino.cache = Padrino::Cache::Store::Mongo.new(::Mongo::Connection.new(...)
- Padrino.cache = Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0))
- Padrino.cache = Padrino::Cache::Store::Memory.new(50)
- Padrino.cache = Padrino::Cache::Store::File.new(/my/cache/path)
+ Padrino.cache = Padrino::Cache.new(:LRUHash)
+ Padrino.cache = Padrino::Cache.new(:Memcached)
+ Padrino.cache = Padrino::Cache.new(:Redis)
+ Padrino.cache = Padrino::Cache.new(:File, :dir => Padrino.root('tmp', app_name.to_s, 'cache')) # default choice
You can manage your cache from anywhere in your app:
- Padrino.cache.set('val', 'test')
- Padrino.cache.get('val') # => 'test'
+ Padrino.cache['val'] = 'test'
+ Padrino.cache['val'] # => 'test'
Padrino.cache.delete('val')
- Padrino.cache.flush
+ Padrino.cache.clear
==== Application Caching Options
- set :cache, Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1))
- set :cache, Padrino::Cache::Store::Memcache.new(::Dalli::Client.new('127.0.0.1:11211', :exception_retry_limit => 1))
- set :cache, Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0))
- set :cache, Padrino::Cache::Store::Mongo.new(::Mongo::Connection.new(...))
- set :cache, Padrino::Cache::Store::Memory.new(50)
- set :cache, Padrino::Cache::Store::File.new(Padrino.root('tmp', app_name, 'cache') # default choice
+ set :cache, Padrino::Cache.new(:LRUHash)
+ set :cache, Padrino::Cache.new(:Memcached)
+ set :cache, Padrino::Cache.new(:Redis)
+ set :cache, Padrino::Cache.new(:File, :dir => Padrino.root('tmp', app_name.to_s, 'cache')) # default choice
You can manage your cache from anywhere in your app:
- MyApp.cache.set('val', 'test')
- MyApp.cache.get('val') # => 'test'
+ MyApp.cache['val'] = 'test'
+ MyApp.cache['val'] # => 'test'
MyApp.cache.delete('val')
- MyApp.cache.flush
+ MyApp.cache.clear
== Expiring Cached Content
@@ -316,7 +312,7 @@ do our user a favor and instantly re-render the feed.
expire( "feed since #{last_visit}" ) if @tweets.any? { |t| t.deleted_since?( last_visit ) }
session[:last_visit] = Time.now
- @feed = cache( "feed since #{last_visit}", :expires_in => 60 ) do
+ @feed = cache( "feed since #{last_visit}", :expires => 60 ) do
@tweets = @tweets.find_all { |t| !t.deleted? }
render 'partials/feedcontent'
end
@@ -2,18 +2,20 @@
require 'padrino-core'
require 'padrino-helpers'
FileSet.glob_require('padrino-cache/{helpers}/*.rb', __FILE__)
+require 'moneta'
+require 'padrino-cache/legacy_store'
module Padrino
class << self
##
# Returns the caching engine.
#
# @example
- # # with: Padrino.cache = Padrino::Cache::Store::File.new(/my/cache/path)
- # Padrino.cache.set('val', 'test')
- # Padrino.cache.get('val') # => 'test'
+ # # with: Padrino.cache = Padrino::Cache.new(:File, :dir => /my/cache/path)
+ # Padrino.cache['val'] = 'test'
+ # Padrino.cache['val'] # => 'test'
# Padrino.cache.delete('val')
- # Padrino.cache.flush
+ # Padrino.cache.clear
#
def cache
@_cache
@@ -23,22 +25,20 @@ def cache
# Set the caching engine.
#
# @param value
- # Instance of Padrino::Cache::Store
+ # Instance of Moneta store
#
# @example
- # Padrino.cache = Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1))
- # Padrino.cache = Padrino::Cache::Store::Memcache.new(::Dalli::Client.new('127.0.0.1:11211', :exception_retry_limit => 1))
- # Padrino.cache = Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0))
- # Padrino.cache = Padrino::Cache::Store::Mongo.new(::Mongo::Connection.new('127.0.0.1', 27017).db('padrino'), :username => 'username', :password => 'password', :size => 64, :max => 100, :collection => 'cache')
- # Padrino.cache = Padrino::Cache::Store::Memory.new(50)
- # Padrino.cache = Padrino::Cache::Store::File.new(/my/cache/path)
+ # Padrino.cache = Padrino::Cache.new(:LRUHash)
+ # Padrino.cache = Padrino::Cache.new(:Memcached)
+ # Padrino.cache = Padrino::Cache.new(:Redis)
+ # Padrino.cache = Padrino::Cache.new(:File, :dir => Padrino.root('tmp', app_name.to_s, 'cache')) # default choice
#
# # You can manage your cache from anywhere in your app:
#
- # Padrino.cache.set('val', 'test')
- # Padrino.cache.get('val') # => 'test'
+ # Padrino.cache['val'] = 'test'
+ # Padrino.cache['val'] # => 'test'
# Padrino.cache.delete('val')
- # Padrino.cache.flush
+ # Padrino.cache.clear
#
def cache=(value)
@_cache= value
@@ -52,9 +52,6 @@ def cache=(value)
# of your choosing. Several common caching stores are supported out of the box.
#
module Cache
- autoload :Store, 'padrino-cache/store'
- autoload :Parser, 'padrino-cache/parser'
-
class << self
##
# Register these helpers:
@@ -67,29 +64,28 @@ class << self
#
# By default we use FileStore as showed below:
#
- # set :cache, Padrino::Cache::Store::File.new(File.join(app.root, 'tmp', 'cache'))
+ # set :cache, Padrino::Cache.new(:File, :dir => Padrino.root('tmp', app_name.to_s, 'cache'))
#
# However, you can also change the file store easily in your app.rb:
#
- # set :cache, Padrino::Cache::Store::Memcache.new(::Memcached.new('127.0.0.1:11211', :exception_retry_limit => 1))
- # set :cache, Padrino::Cache::Store::Memcache.new(::Dalli::Client.new('127.0.0.1:11211', :exception_retry_limit => 1))
- # set :cache, Padrino::Cache::Store::Redis.new(::Redis.new(:host => '127.0.0.1', :port => 6379, :db => 0))
- # set :cache, Padrino::Cache::Store::Mongo.new(::Mongo::Connection.new('127.0.0.1', 27017).db('padrino'), :username => 'username', :password => 'password', :size => 64, :max => 100, :collection => 'cache')
- # set :cache, Padrino::Cache::Store::Memory.new(50)
- # set :cache, Padrino::Cache::Store::File.new(Padrino.root('tmp', app_name.to_s, 'cache')) # default choice
+ # set :cache, Padrino::Cache.new(:LRUHash)
+ # set :cache, Padrino::Cache.new(:Memcached)
+ # set :cache, Padrino::Cache.new(:Redis)
+ # set :cache, Padrino::Cache.new(:File, :dir => Padrino.root('tmp', app_name.to_s, 'cache')) # default choice
#
# You can manage your cache from anywhere in your app:
#
- # MyApp.cache.set('val', 'test')
- # MyApp.cache.get('val') # => 'test'
+ # MyApp.cache['val'] = 'test'
+ # MyApp.cache['val'] # => 'test'
# MyApp.cache.delete('val')
- # MyApp.cache.flush
+ # MyApp.cache.clear
#
def registered(app)
app.helpers Padrino::Cache::Helpers::CacheStore
app.helpers Padrino::Cache::Helpers::Fragment
app.helpers Padrino::Cache::Helpers::Page
- app.set :cache, Padrino::Cache::Store::File.new(Padrino.root('tmp', defined?(app.app_name) ? app.app_name.to_s : '', 'cache'))
+ app.set :cache, Padrino::Cache.new(:File,
+ :dir => Padrino.root('tmp', defined?(app.app_name) ? app.app_name.to_s : '', 'cache'))
app.disable :caching
end
alias :included :registered
@@ -99,6 +95,17 @@ def padrino_route_added(route, verb, path, args, options, block)
end
end
- Padrino.cache = Store::Memory.new(50)
+ def self.new(name, options = {})
+ # Activate expiration by default
+ options[:expires] = true unless options.include?(:expires)
+ a = Moneta.new(name, options)
+ Moneta.build do
+ # Use proxy to support deprecated Padrino interface
+ use LegacyStore
+ adapter a
+ end
+ end
+
+ Padrino.cache = Padrino::Cache.new(:LRUHash)
end
end
@@ -8,6 +8,7 @@ def expire(*key)
else
settings.cache.delete(self.class.url(*key))
end
+ nil
end
end
end
@@ -23,7 +23,7 @@ module Fragment
# @param [String] key
# cache key
# @param [Hash] opts
- # cache options, e.g :expires_in
+ # cache options, e.g :expires
# @param [Proc]
# Execution result to store in the cache
#
@@ -36,7 +36,7 @@ module Fragment
# get :feed, :map => '/:username' do
# username = params[:username]
#
- # @feed = cache( "feed_for_#{username}", :expires_in => 3 ) do
+ # @feed = cache( "feed_for_#{username}", :expires => 3 ) do
# @tweets = Tweet.all( :username => username )
# render 'partials/feedcontent'
# end
@@ -47,15 +47,16 @@ module Fragment
# end
# end
#
- def cache(key, opts = nil, &block)
+ # @api public
+ def cache(key, opts = {}, &block)
if settings.caching?
began_at = Time.now
- if value = settings.cache.get(key.to_s)
+ if value = settings.cache[key.to_s]
logger.debug "GET Fragment", began_at, key.to_s if defined?(logger)
concat_content(value.html_safe)
else
value = capture_html(&block)
- settings.cache.set(key.to_s, value, opts)
+ settings.cache.store(key.to_s, value, opts)
logger.debug "SET Fragment", began_at, key.to_s if defined?(logger)
concat_content(value)
end
Oops, something went wrong.

0 comments on commit 3e551c9

Please sign in to comment.