merb-cache 0.9.7 → 0.9.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. data/LICENSE +2 -2
  2. data/README +207 -143
  3. data/Rakefile +55 -10
  4. data/TODO +0 -2
  5. data/lib/merb-cache/cache.rb +84 -0
  6. data/lib/merb-cache/core_ext/enumerable.rb +9 -0
  7. data/lib/merb-cache/core_ext/hash.rb +20 -0
  8. data/lib/merb-cache/merb_ext/controller.rb +167 -0
  9. data/lib/merb-cache/stores/fundamental/abstract_store.rb +101 -0
  10. data/lib/merb-cache/stores/fundamental/file_store.rb +112 -0
  11. data/lib/merb-cache/stores/fundamental/memcached_store.rb +112 -0
  12. data/lib/merb-cache/stores/strategy/abstract_strategy_store.rb +123 -0
  13. data/lib/merb-cache/stores/strategy/action_store.rb +56 -0
  14. data/lib/merb-cache/stores/strategy/adhoc_store.rb +69 -0
  15. data/lib/merb-cache/stores/strategy/gzip_store.rb +63 -0
  16. data/lib/merb-cache/stores/strategy/page_store.rb +64 -0
  17. data/lib/merb-cache/stores/strategy/sha1_store.rb +62 -0
  18. data/lib/merb-cache.rb +8 -7
  19. data/spec/merb-cache/cache_spec.rb +88 -0
  20. data/spec/merb-cache/core_ext/enumerable_spec.rb +22 -0
  21. data/spec/merb-cache/core_ext/hash_spec.rb +20 -0
  22. data/spec/merb-cache/merb_ext/controller_spec.rb +284 -0
  23. data/spec/merb-cache/stores/fundamental/abstract_store_spec.rb +166 -0
  24. data/spec/merb-cache/stores/fundamental/file_store_spec.rb +186 -0
  25. data/spec/merb-cache/stores/fundamental/memcached_store_spec.rb +243 -0
  26. data/spec/merb-cache/stores/strategy/abstract_strategy_store_spec.rb +78 -0
  27. data/spec/merb-cache/stores/strategy/action_store_spec.rb +189 -0
  28. data/spec/merb-cache/stores/strategy/adhoc_store_spec.rb +225 -0
  29. data/spec/merb-cache/stores/strategy/gzip_store_spec.rb +51 -0
  30. data/spec/merb-cache/stores/strategy/page_store_spec.rb +111 -0
  31. data/spec/merb-cache/stores/strategy/sha1_store_spec.rb +75 -0
  32. data/spec/spec_helper.rb +69 -72
  33. metadata +42 -31
  34. data/lib/merb-cache/cache-action.rb +0 -144
  35. data/lib/merb-cache/cache-fragment.rb +0 -95
  36. data/lib/merb-cache/cache-page.rb +0 -203
  37. data/lib/merb-cache/cache-store/database-activerecord.rb +0 -88
  38. data/lib/merb-cache/cache-store/database-datamapper.rb +0 -79
  39. data/lib/merb-cache/cache-store/database-sequel.rb +0 -78
  40. data/lib/merb-cache/cache-store/database.rb +0 -144
  41. data/lib/merb-cache/cache-store/dummy.rb +0 -106
  42. data/lib/merb-cache/cache-store/file.rb +0 -194
  43. data/lib/merb-cache/cache-store/memcache.rb +0 -199
  44. data/lib/merb-cache/cache-store/memory.rb +0 -168
  45. data/lib/merb-cache/merb-cache.rb +0 -165
  46. data/lib/merb-cache/merbtasks.rb +0 -6
  47. data/spec/config/database.yml +0 -14
  48. data/spec/controller.rb +0 -101
  49. data/spec/log/merb_test.log +0 -433
  50. data/spec/merb-cache-action_spec.rb +0 -162
  51. data/spec/merb-cache-fragment_spec.rb +0 -100
  52. data/spec/merb-cache-page_spec.rb +0 -150
  53. data/spec/merb-cache_spec.rb +0 -15
  54. data/spec/views/cache_controller/action1.html.erb +0 -4
  55. data/spec/views/cache_controller/action2.html.haml +0 -4
data/spec/spec_helper.rb CHANGED
@@ -1,84 +1,81 @@
1
- $TESTING=true
2
- $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
3
- require "rubygems"
4
- require "merb-core"
1
+ $:.push File.join(File.dirname(__FILE__), '..', 'lib')
5
2
 
6
- require "merb-cache"
7
- require File.dirname(__FILE__) / "controller"
3
+ # Deps
4
+ require 'rubygems'
5
+ require 'merb-core'
6
+ require 'merb-action-args'
7
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'merb-cache')
8
8
 
9
- require "merb-haml"
9
+ # We want logging!
10
+ Merb.logger = Merb::Logger.new(File.join(File.dirname(__FILE__), '..', 'log', 'merb_test.log'))
10
11
 
11
- def set_database_adapter(adapter)
12
- config_file = File.dirname(__FILE__) / "config/database.yml"
13
- config = IO.read(config_file)
14
- config.gsub!(/:adapter:\s+.*$/, ":adapter: #{adapter}")
15
- File.open(config_file, "w+") do |c| c.write(config) end
12
+ Merb.start :environment => "test", :adapter => "runner"
13
+
14
+ require "merb-core/test"
15
+ Spec::Runner.configure do |config|
16
+ config.include Merb::Test::Helpers
17
+ #config.include Merb::Test::ControllerHelper
18
+ config.include Merb::Test::RouteHelper
16
19
  end
17
20
 
18
- def use_cache_store(store, orm = nil)
19
- Merb::Plugins.config[:merb_cache] = {
20
- :store => store,
21
- :cache_directory => File.dirname(__FILE__) / "tmp/cache",
22
- :cache_html_directory => File.dirname(__FILE__) / "tmp/html",
23
- :no_tracking => false
24
- }
25
- FileUtils.rm_rf(Dir.glob(File.dirname(__FILE__) / "/tmp"))
26
- case store
27
- when "dummy"
28
- when "file"
29
- when "memory"
30
- when "memcache"
31
- require "memcache"
32
- when "database"
33
- case orm
34
- when "datamapper"
35
- Merb.environment = "test"
36
- Merb.logger = Merb::Logger.new("log/merb_test.log")
37
- set_database_adapter("sqlite3")
38
- require "merb_datamapper"
39
- when "activerecord"
40
- Merb.logger = Merb::Logger.new("log/merb_test.log")
41
- set_database_adapter("sqlite3")
42
- require "merb_activerecord"
43
- when "sequel"
44
- set_database_adapter("sqlite")
45
- require "merb_sequel"
46
- else
47
- raise "Unknown orm: #{orm}"
21
+ class DummyStore < Merb::Cache::AbstractStore
22
+ cattr_accessor :vault
23
+ attr_accessor :options
24
+
25
+ def initialize(config = {})
26
+ super(config)
27
+ @options = config
28
+ @@vault = {}
29
+ end
30
+
31
+ def writable?(*args)
32
+ true
33
+ end
34
+
35
+ def read(key, parameters = {})
36
+
37
+ if @@vault.keys.include?(key)
38
+ @@vault[key].find {|data, timestamp, conditions, params| params == parameters}
48
39
  end
49
- else
50
- raise "Unknown cache store: #{store}"
51
40
  end
52
- end
53
41
 
54
- store = "file"
55
- case ENV["STORE"] || store
56
- when "file"
57
- use_cache_store "file"
58
- when "memory"
59
- use_cache_store "memory"
60
- when "memcache"
61
- use_cache_store "memcache"
62
- when "datamapper"
63
- use_cache_store "database", "datamapper"
64
- when "sequel"
65
- use_cache_store "database", "sequel"
66
- when "activerecord"
67
- use_cache_store "database", "activerecord"
68
- when "dummy"
69
- use_cache_store "dummy"
70
- else
71
- puts "Invalid cache store: #{ENV["store"]}"
72
- exit
73
- end
42
+ def data(key, parameters = {})
43
+ read(key, parameters)[0] if read(key, parameters)
44
+ end
74
45
 
75
- require "fileutils"
76
- FileUtils.mkdir_p(Merb::Plugins.config[:merb_cache][:cache_html_directory])
77
- FileUtils.mkdir_p(Merb::Plugins.config[:merb_cache][:cache_directory])
46
+ def time(key, parameters = {})
47
+ read(key, parameters)[1] if read(key, parameters)
48
+ end
78
49
 
79
- Merb.start :environment => "test", :adapter => "runner"
50
+ def conditions(key, parameters = {})
51
+ read(key, parameters)[2] if read(key, parameters)
52
+ end
80
53
 
81
- require "merb-core/test"
82
- Spec::Runner.configure do |config|
83
- config.include Merb::Test::RequestHelper
54
+ def write(key, data = nil, parameters = {}, conditions = {})
55
+ (@@vault[key] ||= []) << [data, Time.now, conditions, parameters]
56
+ true
57
+ end
58
+
59
+ def fetch(key, parameters = {}, conditions = {}, &blk)
60
+ @@vault[[key, parameters]] ||= blk.call
61
+ end
62
+
63
+ def exists?(key, parameters = {})
64
+ @@vault.has_key? [key, parameters]
65
+ end
66
+
67
+ def delete(key, parameters = {})
68
+ @@vault.delete([key, parameters]) unless @@vault[[key, parameters]].nil?
69
+ end
70
+
71
+ def delete_all
72
+ @@vault = {}
73
+ end
84
74
  end
75
+
76
+ #TODO change this to a work queue per class called in an after aspect
77
+ class Merb::Controller
78
+ def run_later
79
+ yield
80
+ end
81
+ end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merb-cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.7
4
+ version: 0.9.8
5
5
  platform: ruby
6
6
  authors:
7
- - Alex Boussinet
7
+ - Ben Burkert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-09-09 00:00:00 +03:00
12
+ date: 2008-10-06 00:00:00 +03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,10 +20,10 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.9.7
23
+ version: 0.9.8
24
24
  version:
25
25
  description: Merb plugin that provides caching (page, action, fragment, object)
26
- email: alex.boussinet@gmail.com
26
+ email: ben@benburkert.com
27
27
  executables: []
28
28
 
29
29
  extensions: []
@@ -38,35 +38,46 @@ files:
38
38
  - Rakefile
39
39
  - TODO
40
40
  - lib/merb-cache
41
- - lib/merb-cache/cache-action.rb
42
- - lib/merb-cache/cache-fragment.rb
43
- - lib/merb-cache/cache-page.rb
44
- - lib/merb-cache/cache-store
45
- - lib/merb-cache/cache-store/database-activerecord.rb
46
- - lib/merb-cache/cache-store/database-datamapper.rb
47
- - lib/merb-cache/cache-store/database-sequel.rb
48
- - lib/merb-cache/cache-store/database.rb
49
- - lib/merb-cache/cache-store/dummy.rb
50
- - lib/merb-cache/cache-store/file.rb
51
- - lib/merb-cache/cache-store/memcache.rb
52
- - lib/merb-cache/cache-store/memory.rb
53
- - lib/merb-cache/merb-cache.rb
54
- - lib/merb-cache/merbtasks.rb
41
+ - lib/merb-cache/cache.rb
42
+ - lib/merb-cache/core_ext
43
+ - lib/merb-cache/core_ext/enumerable.rb
44
+ - lib/merb-cache/core_ext/hash.rb
45
+ - lib/merb-cache/merb_ext
46
+ - lib/merb-cache/merb_ext/controller.rb
47
+ - lib/merb-cache/stores
48
+ - lib/merb-cache/stores/fundamental
49
+ - lib/merb-cache/stores/fundamental/abstract_store.rb
50
+ - lib/merb-cache/stores/fundamental/file_store.rb
51
+ - lib/merb-cache/stores/fundamental/memcached_store.rb
52
+ - lib/merb-cache/stores/strategy
53
+ - lib/merb-cache/stores/strategy/abstract_strategy_store.rb
54
+ - lib/merb-cache/stores/strategy/action_store.rb
55
+ - lib/merb-cache/stores/strategy/adhoc_store.rb
56
+ - lib/merb-cache/stores/strategy/gzip_store.rb
57
+ - lib/merb-cache/stores/strategy/page_store.rb
58
+ - lib/merb-cache/stores/strategy/sha1_store.rb
55
59
  - lib/merb-cache.rb
56
- - spec/config
57
- - spec/config/database.yml
58
- - spec/controller.rb
59
60
  - spec/log
60
- - spec/log/merb_test.log
61
- - spec/merb-cache-action_spec.rb
62
- - spec/merb-cache-fragment_spec.rb
63
- - spec/merb-cache-page_spec.rb
64
- - spec/merb-cache_spec.rb
61
+ - spec/merb-cache
62
+ - spec/merb-cache/cache_spec.rb
63
+ - spec/merb-cache/core_ext
64
+ - spec/merb-cache/core_ext/enumerable_spec.rb
65
+ - spec/merb-cache/core_ext/hash_spec.rb
66
+ - spec/merb-cache/merb_ext
67
+ - spec/merb-cache/merb_ext/controller_spec.rb
68
+ - spec/merb-cache/stores
69
+ - spec/merb-cache/stores/fundamental
70
+ - spec/merb-cache/stores/fundamental/abstract_store_spec.rb
71
+ - spec/merb-cache/stores/fundamental/file_store_spec.rb
72
+ - spec/merb-cache/stores/fundamental/memcached_store_spec.rb
73
+ - spec/merb-cache/stores/strategy
74
+ - spec/merb-cache/stores/strategy/abstract_strategy_store_spec.rb
75
+ - spec/merb-cache/stores/strategy/action_store_spec.rb
76
+ - spec/merb-cache/stores/strategy/adhoc_store_spec.rb
77
+ - spec/merb-cache/stores/strategy/gzip_store_spec.rb
78
+ - spec/merb-cache/stores/strategy/page_store_spec.rb
79
+ - spec/merb-cache/stores/strategy/sha1_store_spec.rb
65
80
  - spec/spec_helper.rb
66
- - spec/views
67
- - spec/views/cache_controller
68
- - spec/views/cache_controller/action1.html.erb
69
- - spec/views/cache_controller/action2.html.haml
70
81
  has_rdoc: true
71
82
  homepage: http://merbivore.com
72
83
  post_install_message:
@@ -1,144 +0,0 @@
1
- class Merb::Cache
2
- cattr_accessor :cached_actions
3
- self.cached_actions = {}
4
- end
5
-
6
- module Merb::Cache::ControllerClassMethods
7
- # Mixed in Merb::Controller. Provides methods related to action caching.
8
-
9
- # Registers an action for action caching via before and after filters.
10
- #
11
- # ==== Parameters
12
- # action<Symbol>:: The name of the action to register.
13
- # from_now<~minutes>:: The number of minutes (from now) the cache should persist.
14
- # opts<Hash>::
15
- # Filter options (see <tt>Filter Options</tt> in <tt>Merb::AbstractController</tt>). The filters created apply :only
16
- # to specified <tt>action</tt>.
17
- #
18
- # ==== Examples
19
- # cache_action :mostly_static
20
- # cache_action :barely_dynamic, 10
21
- # cache_action :list, :if => Proc.new { |controller| !controller.params[:id].empty? }
22
- # cache_action :show, :unless => proc { |controller| !controller.params[:id].empty? }
23
- def cache_action(action, from_now = nil, opts = {})
24
- cache_actions([action, from_now, opts])
25
- end
26
-
27
- # Register multiple actions for action caching via before and after filters.
28
- #
29
- # ==== Parameter
30
- # actions<Symbol,Array[Symbol,~minutes],Hash>:: See #cache_action.
31
- #
32
- # ==== Example
33
- # cache_actions(
34
- # :mostly_static,
35
- # [:barely_dynamic, 10],
36
- # [:conditional, { :if => proc { |controller| controller.params[:id].empty? } }]
37
- # )
38
- def cache_actions(*actions)
39
- actions.each do |action, from_now, opts|
40
- from_now, opts = nil, from_now if Hash === from_now
41
- opts ||= {}
42
-
43
- before("cache_#{action}_before", opts.merge(:only => action))
44
- after("cache_#{action}_after", opts.merge(:only => action))
45
- alias_method "cache_#{action}_before", :cache_action_before
46
- alias_method "cache_#{action}_after", :cache_action_after
47
-
48
- _actions = Merb::Cache.cached_actions[controller_name] ||= {}
49
- _actions[action] = from_now
50
- end
51
- true
52
- end
53
- end
54
-
55
- module Merb::Cache::ControllerInstanceMethods
56
- # Mixed in Merb::Controller. Provides methods related to action caching
57
-
58
- # Checks whether a cache entry exists
59
- #
60
- # ==== Parameter
61
- # options<String,Hash>:: The options that will be passed to #key_for
62
- #
63
- # ==== Returns
64
- # true if the cache entry exists, false otherwise
65
- #
66
- # ==== Example
67
- # cached_action?(:action => 'show', :params => [params[:page]])
68
- def cached_action?(options)
69
- key = Merb::Controller._cache.key_for(options, controller_name, true)
70
- Merb::Controller._cache.store.cached?(key)
71
- end
72
-
73
- # Expires the action identified by the key computed after the parameters
74
- #
75
- # ==== Parameter
76
- # options<String,Hash>:: The options that will be passed to #expire_key_for
77
- #
78
- # ==== Examples
79
- # expire_action(:action => 'show', :controller => 'news')
80
- # expire_action(:action => 'show', :match => true)
81
- def expire_action(options)
82
- Merb::Controller._cache.expire_key_for(options, controller_name, true) do |key, match|
83
- if match
84
- Merb::Controller._cache.store.expire_match(key)
85
- else
86
- Merb::Controller._cache.store.expire(key)
87
- end
88
- end
89
- true
90
- end
91
-
92
- # You can call this method if you need to prevent caching the action
93
- # after it has been rendered.
94
- def abort_cache_action
95
- @capture_action = false
96
- end
97
-
98
- private
99
-
100
- # Called by the before and after filters. Stores or recalls a cache entry.
101
- # The key is based on the result of request.path
102
- # If the key with "/" then it is removed
103
- # If the key is "/" then it will be replaced by "index"
104
- #
105
- # ==== Parameters
106
- # data<String>:: the data to put in cache using the cache store
107
- #
108
- # ==== Examples
109
- # If request.path is "/", the key will be "index"
110
- # If request.path is "/news/show/1", the key will be "/news/show/1"
111
- # If request.path is "/news/show/", the key will be "/news/show"
112
- def _cache_action(data = nil)
113
- controller = controller_name
114
- action = action_name.to_sym
115
- actions = Merb::Controller._cache.cached_actions[controller]
116
- return unless actions && actions.key?(action)
117
- path = request.path.chomp("/")
118
- path = "index" if path.empty?
119
- if data
120
- from_now = Merb::Controller._cache.cached_actions[controller][action]
121
- Merb::Controller._cache.store.cache_set(path, data, from_now)
122
- else
123
- @capture_action = false
124
- _data = Merb::Controller._cache.store.cache_get(path)
125
- throw(:halt, _data) unless _data.nil?
126
- @capture_action = true
127
- end
128
- true
129
- end
130
-
131
- # before filter
132
- def cache_action_before
133
- # recalls a cached entry or set @capture_action to true in order
134
- # to grab the response in the after filter
135
- _cache_action
136
- end
137
-
138
- # after filter
139
- def cache_action_after
140
- # takes the body of the response and put it in cache
141
- # if the cache entry expired, if it doesn't exist or status is 200
142
- _cache_action(body) if @capture_action && status == 200
143
- end
144
- end
@@ -1,95 +0,0 @@
1
- module Merb::Cache::ControllerInstanceMethods
2
- # Mixed in Merb::Controller. Provides methods related to fragment caching
3
-
4
- # Checks whether a cache entry exists
5
- #
6
- # ==== Parameter
7
- # options<String,Hash>:: The options that will be passed to #key_for
8
- #
9
- # ==== Returns
10
- # true if the cache entry exists, false otherwise
11
- #
12
- # ==== Example
13
- # cached_action?("my_key")
14
- def cached?(options)
15
- key = Merb::Controller._cache.key_for(options, controller_name)
16
- Merb::Controller._cache.store.cached?(key)
17
- end
18
-
19
- # ==== Example
20
- # In your view:
21
- # <%- cache("my_key") do -%>
22
- # <%= partial :test, :collection => @test %>
23
- # <%- end -%>
24
- def cache(options, from_now = nil, &block)
25
- key = Merb::Controller._cache.key_for(options, controller_name)
26
- Merb::Controller._cache.store.cache(self, key, from_now, &block)
27
- end
28
-
29
- # Fetch data from cache
30
- #
31
- # ==== Parameter
32
- # options<String,Hash>:: The options that will be passed to #key_for
33
- #
34
- # ==== Returns
35
- # data<Object,NilClass>::
36
- # nil is returned if the cache entry is not found
37
- #
38
- # ==== Example
39
- # if cache_data = cache_get("my_key")
40
- # @var1, @var2 = *cache_data
41
- # else
42
- # @var1 = MyModel.big_query1
43
- # @var2 = MyModel.big_query2
44
- # cache_set("my_key", nil, [@var1, @var2])
45
- # end
46
- def cache_get(options)
47
- key = Merb::Controller._cache.key_for(options, controller_name)
48
- Merb::Controller._cache.store.cache_get(key)
49
- end
50
-
51
- # Store data to cache
52
- #
53
- # ==== Parameter
54
- # options<String,Hash>:: The options that will be passed to #key_for
55
- # object<Object>:: The object(s) to put in cache
56
- # from_now<~minutes>::
57
- # The number of minutes (from now) the cache should persist
58
- #
59
- # ==== Returns
60
- # data<Object,NilClass>::
61
- # nil is returned if the cache entry is not found
62
- #
63
- # ==== Example
64
- # if cache_data = cache_get("my_key")
65
- # @var1, @var2 = *cache_data
66
- # else
67
- # @var1 = MyModel.big_query1
68
- # @var2 = MyModel.big_query2
69
- # cache_set("my_key", nil, [@var1, @var2])
70
- # end
71
- def cache_set(options, object, from_now = nil)
72
- key = Merb::Controller._cache.key_for(options, controller_name)
73
- Merb::Controller._cache.store.cache_set(key, object, from_now)
74
- end
75
-
76
- # Expires the entry identified by the key computed after the parameters
77
- #
78
- # ==== Parameter
79
- # options<String,Hash>:: The options that will be passed to #key_for
80
- #
81
- # ==== Examples
82
- # expire("my_key")
83
- # expire(:key => "my_", :match => true)
84
- # expire(:key => "my_key", :params => [session[:me], params[:ref]])
85
- def expire(options)
86
- Merb::Controller._cache.expire_key_for(options, controller_name) do |key, match|
87
- if match
88
- Merb::Controller._cache.store.expire_match(key)
89
- else
90
- Merb::Controller._cache.store.expire(key)
91
- end
92
- end
93
- true
94
- end
95
- end