merb-cache 0.9.7 → 0.9.8

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,165 +0,0 @@
1
- require "merb-cache/cache-action"
2
- require "merb-cache/cache-page"
3
- require "merb-cache/cache-fragment"
4
-
5
- class Merb::Cache
6
- attr_reader :config, :store
7
-
8
- class StoreNotFound < Exception
9
- def initialize(cache_store)
10
- super("cache_store (#{cache_store}) not found (not implemented?)")
11
- end
12
- end
13
-
14
- DEFAULT_CONFIG = {
15
- :cache_html_directory => Merb.dir_for(:public) / "cache",
16
-
17
- #:store => "database",
18
- #:table_name => "merb_cache",
19
-
20
- #:disable => "development", # disable merb-cache in development
21
- #:disable => true, # disable merb-cache in all environments
22
-
23
- :store => "file",
24
- :cache_directory => Merb.root_path("tmp/cache"),
25
-
26
- #:store => "memcache",
27
- #:host => "127.0.0.1:11211",
28
- #:namespace => "merb_cache",
29
- #:track_keys => true,
30
-
31
- #:store => "memory",
32
- # store could be: file, memcache, memory, database, dummy, ...
33
- }
34
-
35
- # Called in the after_app_loads loop and instantiate the right backend
36
- #
37
- # ==== Raises
38
- # Store#NotFound::
39
- # If the cache_store mentionned in the config is unknown
40
- def start
41
- @config = DEFAULT_CONFIG.merge(Merb::Plugins.config[:merb_cache] || {})
42
- if @config[:disable] == true || Merb.environment.to_s == @config[:disable].to_s
43
- config[:disable_page_caching] = true
44
- config[:store] = "dummy"
45
- end
46
- @config[:cache_html_directory] ||= Merb.dir_for(:public) / "cache"
47
- require "merb-cache/cache-store/#{@config[:store]}"
48
- @store = Merb::Cache.const_get("#{@config[:store].capitalize}Store").new
49
- Merb.logger.info("Using #{@config[:store]} cache")
50
- rescue LoadError
51
- raise Merb::Cache::StoreNotFound, @config[:store].inspect
52
- end
53
-
54
- # Compute a cache key and yield it to the given block
55
- # It is used by the #expire_page, #expire_action and #expire methods.
56
- #
57
- # ==== Parameters
58
- # options<String, Hash>:: The key or the Hash that will be used to build the key
59
- # controller<String>:: The name of the controller
60
- # controller_based<Boolean>:: only used by action and page caching
61
- #
62
- # ==== Options (options)
63
- # :key<String>:: The complete or partial key that will be computed.
64
- # :action<String>:: The action name that will be used to compute the key
65
- # :controller<String>:: The controller name that will be part of the key
66
- # :params<Array[String]>::
67
- # The params will be joined together (with '/') and added to the key
68
- # :match<Boolean, String>::
69
- # true, it will try to match multiple cache entries
70
- # string, shortcut for {:key => "mykey", :match => true}
71
- #
72
- # ==== Examples
73
- # expire(:key => "root_key", :params => [session[:me], params[:id]])
74
- # expire(:match => "root_key")
75
- # expire_action(:action => 'list')
76
- # expire_page(:action => 'show', :controller => 'news')
77
- #
78
- # ==== Returns
79
- # The result of the given block
80
- #
81
- def expire_key_for(options, controller, controller_based = false)
82
- key = ""
83
- if options.is_a? Hash
84
- case
85
- when key = options[:key]
86
- when action = options[:action]
87
- controller = options[:controller] || controller
88
- key = "/#{controller}/#{action}"
89
- when match = options[:match]
90
- key = match
91
- end
92
- if _params = options[:params]
93
- key += "/" + _params.join("/")
94
- end
95
- yield key, !options[:match].nil?
96
- else
97
- yield controller_based ? "/#{controller}/#{options}" : options, false
98
- end
99
- end
100
-
101
- # Compute a cache key based on the given parameters
102
- # Only used by the #cached_page?, #cached_action?, #cached?, #cache,
103
- # #cache_get and #cache_set methods
104
- #
105
- # ==== Parameters
106
- # options<String, Hash>:: The key or the Hash that will be used to build the key
107
- # controller<String>:: The name of the controller
108
- # controller_based<Boolean>:: only used by action and page caching
109
- #
110
- # ==== Options (options)
111
- # :key<String>:: The complete or partial key that will be computed.
112
- # :action<String>:: The action name that will be used to compute the key
113
- # :controller<String>:: The controller name that will be part of the key
114
- # :params<Array[String]>::
115
- # The params will be joined together (with '/') and added to the key
116
- #
117
- # ==== Examples
118
- # cache_set("my_key", @data)
119
- # cache_get(:key => "root_key", :params => [session[:me], params[:id]])
120
- #
121
- # ==== Returns
122
- # The computed key
123
- def key_for(options, controller, controller_based = false)
124
- key = ""
125
- if options.is_a? Hash
126
- case
127
- when key = options[:key]
128
- when action = options[:action]
129
- controller = options[:controller] || controller
130
- key = "/#{controller}/#{action}"
131
- end
132
- if _params = options[:params]
133
- key += "/" + _params.join("/")
134
- end
135
- else
136
- key = controller_based ? "/#{controller}/#{options}" : options
137
- end
138
- key
139
- end
140
-
141
- module ControllerInstanceMethods
142
- # Mixed in Merb::Controller and provides expire_all for action and fragment caching.
143
- def expire_all
144
- Merb::Controller._cache.store.expire_all
145
- end
146
- end
147
- end
148
-
149
- module Merb
150
- class Controller
151
- cattr_reader :_cache
152
- @@_cache = Merb::Cache.new
153
- # extends Merb::Controller with new instance methods
154
- include Merb::Cache::ControllerInstanceMethods
155
- class << self
156
- # extends Merb::Controller with new class methods
157
- include Merb::Cache::ControllerClassMethods
158
- end
159
- end
160
- end
161
-
162
- Merb::BootLoader.after_app_loads do
163
- # the cache starts after the application is loaded
164
- Merb::Controller._cache.start
165
- end
@@ -1,6 +0,0 @@
1
- namespace :merb_cache do
2
- desc "Do something for merb-cache"
3
- task :default do
4
- puts "merb-cache doesn't do anything"
5
- end
6
- end
@@ -1,14 +0,0 @@
1
- ---
2
- :development: &defaults
3
- :adapter: sqlite3
4
- :database: tmp/merb-cache.sqlite3
5
- :log_stream: log/sql.log
6
- :log_level: 0
7
-
8
- :test:
9
- <<: *defaults
10
- :database: tmp/merb-cache.sqlite3
11
-
12
- :production:
13
- <<: *defaults
14
- :database: tmp/merb-cache.sqlite3
data/spec/controller.rb DELETED
@@ -1,101 +0,0 @@
1
- class CacheController < Merb::Controller
2
- self._template_root = File.dirname(__FILE__) / "views"
3
-
4
- cache_action :action3
5
- cache_action :action4, 0.05
6
-
7
- cache_page :action5
8
- cache_page :action6, 0.05
9
- # or cache_pages :action5, [:action6, 0.05]
10
- cache_page :action7
11
-
12
- cache_action :action8, 0.05, :if => proc {|controller| !controller.params[:id].empty?}
13
- cache_action :action9, 0.05, :unless => proc {|controller| controller.params[:id].empty?}
14
- cache_action :action10, :if => :non_empty_id?
15
- cache_action :action11, :unless => :empty_id?
16
-
17
- cache_actions :cache_actions_1, [:cache_actions_2, 0.05], [:cache_actions_3, { :if => :empty_id? }]
18
-
19
- def action1
20
- render
21
- end
22
-
23
- def action2
24
- render
25
- end
26
-
27
- def action3
28
- "test action3"
29
- end
30
-
31
- def action4
32
- "test action4"
33
- end
34
-
35
- def action5
36
- "test action5"
37
- end
38
-
39
- def action6
40
- Time.now.to_s
41
- end
42
-
43
- def action7
44
- provides :js, :css, :html, :xml, :jpg
45
- case params[:format]
46
- when "css"
47
- "CSS"
48
- when "js"
49
- "JS"
50
- when "html"
51
- "HTML"
52
- when "xml"
53
- "XML"
54
- when "jpg"
55
- "JPG"
56
- else
57
- raise "BAD FORMAT: #{params[:format].inspect}"
58
- end
59
- end
60
-
61
- def action8
62
- "test action8"
63
- end
64
-
65
- def action9
66
- "test action9"
67
- end
68
-
69
- def action10
70
- "test action10"
71
- end
72
-
73
- def action11
74
- "test action11"
75
- end
76
-
77
- def cache_actions_1
78
- 'test cache_actions_1'
79
- end
80
-
81
- def cache_actions_2
82
- 'test cache_actions_2'
83
- end
84
-
85
- def cache_actions_3
86
- 'test cache_actions_3'
87
- end
88
-
89
- def index
90
- "test index"
91
- end
92
-
93
- private
94
- def empty_id?
95
- params[:id].empty?
96
- end
97
-
98
- def non_empty_id?
99
- !empty_id?
100
- end
101
- end