Module: Cashier
- Defined in:
- lib/cashier.rb,
lib/cashier/railtie.rb,
lib/cashier/version.rb,
lib/cashier/matchers.rb,
lib/cashier/adapters/cache_store.rb,
lib/cashier/adapters/redis_store.rb
Defined Under Namespace
Modules: Adapters, Matchers Classes: Railtie
Constant Summary collapse
- CACHE_KEY =
'cashier-tags'
- VERSION =
"0.5.0"
Class Method Summary collapse
-
.adapter ⇒ Object
Public: adapter which is used by cashier.
-
.adapter=(cache_adapter) ⇒ Object
Public: set the adapter the Cashier module will use to store the keys.
-
.canonize_tags(tags) ⇒ Object
Public: canonize tags: convert ActiveRecord objects to string (inc. id).
-
.clear ⇒ Object
Public: clears the tags.
- .container_cache_key(tag) ⇒ Object
-
.expire(*tags) ⇒ Object
Public: expire tags.
-
.get_containers(tags) ⇒ Object
Public: get the tags of containers for the given fragment tags.
-
.keys ⇒ Object
Public: get all the keys names as an array.
-
.keys_for(tag) ⇒ Object
Public: get all the keys for a specific tag as an array.
-
.perform_caching? ⇒ Boolean
Public: whether the module will perform caching or not.
-
.pop_container ⇒ Object
Public: remove tags of a container fragment from the current container stack.
-
.push_container(*tags) ⇒ Object
Public: add tags of a container fragment into the current container stack (used internally by ActiveSupport::Notifications).
-
.store_fragment(fragment, *tags) ⇒ Object
Public: store a fragment with an array of tags for this fragment.
-
.tags ⇒ Object
Public: returns the array of tags stored in the tags store.
Class Method Details
.adapter ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/cashier.rb', line 142 def adapter if @@adapter == :cache_store Cashier::Adapters::CacheStore else Cashier::Adapters::RedisStore end end |
.adapter=(cache_adapter) ⇒ Object
158 159 160 |
# File 'lib/cashier.rb', line 158 def adapter=(cache_adapter) @@adapter = cache_adapter end |
.canonize_tags(tags) ⇒ Object
Public: canonize tags: convert ActiveRecord objects to string (inc. id)
cache_adapter - :cache_store / :redis_store
Examples
Cashier.([1, :a, Article.find(123)])
# => [1, :a, "Article-123"]
217 218 219 220 221 222 223 224 225 226 |
# File 'lib/cashier.rb', line 217 def () = [ || []].flatten .map do |tag| if tag.is_a?(ActiveRecord::Base) "#{tag.class.name}-#{tag.to_param}" else tag end end end |
.clear ⇒ Object
101 102 103 104 105 |
# File 'lib/cashier.rb', line 101 def clear ActiveSupport::Notifications.instrument("clear.cashier") do adapter.clear end end |
.container_cache_key(tag) ⇒ Object
5 6 7 |
# File 'lib/cashier.rb', line 5 def self.container_cache_key(tag) "cashier-tag-containers:#{tag}" end |
.expire(*tags) ⇒ Object
Public: expire tags. expiring the keys ‘assigned’ to the tags you expire and removes the tags from the tags list
tags - array of tags to expire.
Examples
Cashier.expire('tag1', 'tag2')
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cashier.rb', line 56 def expire(*) return unless perform_caching? # add tags of container fragments to expired tags list = () containers = adapter.() || [] = ( + containers).compact.uniq ActiveSupport::Notifications.instrument("expire.cashier", :data => ) do # delete them from the cache .each do |tag| fragment_keys = adapter.get_fragments_for_tag(tag) fragment_keys.each do |fragment_key| Rails.cache.delete(fragment_key) end adapter.delete_tag(tag) end # now remove them from the list # of stored tags adapter.() end end |
.get_containers(tags) ⇒ Object
Public: get the tags of containers for the given fragment tags
cache_adapter - :cache_store / :redis_store
Examples
Cashier.get_containers(['article1'])
# => ['section2', 'section3']
202 203 204 205 |
# File 'lib/cashier.rb', line 202 def get_containers() = () adapter.() end |
.keys ⇒ Object
Public: get all the keys names as an array.
Examples
Cachier.keys
# => ['key1', 'key2', 'key3']
115 116 117 |
# File 'lib/cashier.rb', line 115 def keys adapter.keys end |
.keys_for(tag) ⇒ Object
Public: get all the keys for a specific tag as an array.
Examples
Cashier.('tag1')
# => ['key1', 'key2', 'key3']
127 128 129 130 |
# File 'lib/cashier.rb', line 127 def keys_for(tag) tag = (tag) adapter.get_fragments_for_tag(tag) end |
.perform_caching? ⇒ Boolean
Public: whether the module will perform caching or not. this is being set in the application layer .perform_caching configuration
Examples
Cashier.perform_caching?
# => true
18 19 20 |
# File 'lib/cashier.rb', line 18 def perform_caching? ::ApplicationController.perform_caching end |
.pop_container ⇒ Object
Public: remove tags of a container fragment from the current container stack
cache_adapter - :cache_store / :redis_store
Examples
Cashier.pop_container()
186 187 188 189 190 191 |
# File 'lib/cashier.rb', line 186 def pop_container() return unless perform_caching? @@container_stack ||= [] container = @@container_stack.pop container end |
.push_container(*tags) ⇒ Object
Public: add tags of a container fragment into the current container stack (used internally by ActiveSupport::Notifications)
cache_adapter - :cache_store / :redis_store
Examples
Cashier.push_container(['section2'])
170 171 172 173 174 175 176 |
# File 'lib/cashier.rb', line 170 def push_container(*) return unless perform_caching? @@container_stack ||= [] = () adapter.(, @@container_stack) @@container_stack.push end |
.store_fragment(fragment, *tags) ⇒ Object
Public: store a fragment with an array of tags for this fragment.
fragment - cached fragment. tags - array of tags you want to assign this fragments.
Examples
Cachier.store_fragment('foo', 'tag1', 'tag2', 'tag3')
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cashier.rb', line 31 def store_fragment(fragment, *) return unless perform_caching? = .flatten = () ActiveSupport::Notifications.instrument("store_fragment.cashier", :data => [fragment, ]) do .each do |tag| # store the fragment adapter.store_fragment_in_tag(fragment, tag) end # now store the tag for book keeping adapter.() end end |
.tags ⇒ Object
90 91 92 |
# File 'lib/cashier.rb', line 90 def adapter. end |