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

Class Method Details

.adapterObject

Public: adapter which is used by cashier.

Examples

Cashier.adapter
# => Cashier::Adapters::CacheStore

Cashier.adapter
# => Cashier::Adapters::RedisStore


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

Public: set the adapter the Cashier module will use to store the keys

cache_adapter - :cache_store / :redis_store

Examples

Cashier.adapter = :redis_store


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.canonize_tags([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 canonize_tags(tags)
  tags = [tags || []].flatten
  tags.map do |tag| 
    if tag.is_a?(ActiveRecord::Base) 
      "#{tag.class.name}-#{tag.to_param}"
    else
      tag 
    end
  end
end

.clearObject

Public: clears the tags.

Examples

Cashier.clear


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(*tags)
  return unless perform_caching?

  # add tags of container fragments to expired tags list
  tags = canonize_tags(tags)
  containers = adapter.get_tags_containers(tags) || []
  tags = (tags + containers).compact.uniq

  ActiveSupport::Notifications.instrument("expire.cashier", :data => tags) do
    # delete them from the cache
    tags.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.remove_tags(tags)
  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(tags)
  tags = canonize_tags(tags)
  adapter.get_tags_containers(tags)
end

.keysObject

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.tags_for('tag1')
# => ['key1', 'key2', 'key3']


127
128
129
130
# File 'lib/cashier.rb', line 127

def keys_for(tag)
  tag = canonize_tags(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

Returns:

  • (Boolean)


18
19
20
# File 'lib/cashier.rb', line 18

def perform_caching?
  ::ApplicationController.perform_caching
end

.pop_containerObject

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(*tags)
  return unless perform_caching?
  @@container_stack ||= []
  tags = canonize_tags(tags)
  adapter.add_tags_containers(tags, @@container_stack)
  @@container_stack.push tags
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, *tags)
  return unless perform_caching?

  tags = tags.flatten
  tags = canonize_tags(tags)

  ActiveSupport::Notifications.instrument("store_fragment.cashier", :data => [fragment, tags]) do
    tags.each do |tag|
      # store the fragment
      adapter.store_fragment_in_tag(fragment, tag)
    end

     # now store the tag for book keeping
    adapter.store_tags(tags)
  end
end

.tagsObject

Public: returns the array of tags stored in the tags store.

Examples

Cashier.tags
# => ['tag1', 'tag2']


90
91
92
# File 'lib/cashier.rb', line 90

def tags
  adapter.tags
end