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.4.1"

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


131
132
133
134
135
136
137
# File 'lib/cashier.rb', line 131

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


147
148
149
# File 'lib/cashier.rb', line 147

def adapter=(cache_adapter)
  @@adapter = cache_adapter
end

.clearObject

Public: clears the tags.

Examples

Cashier.clear


91
92
93
94
95
# File 'lib/cashier.rb', line 91

def clear
  ActiveSupport::Notifications.instrument("clear.cashier") do
    adapter.clear
  end
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')


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cashier.rb', line 51

def expire(*tags)
  return unless perform_caching?

  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

.keysObject

Public: get all the keys names as an array.

Examples

Cachier.keys
# => ['key1', 'key2', 'key3']


105
106
107
# File 'lib/cashier.rb', line 105

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']


117
118
119
# File 'lib/cashier.rb', line 117

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


14
15
16
# File 'lib/cashier.rb', line 14

def perform_caching?
  ::ApplicationController.perform_caching
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')


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cashier.rb', line 27

def store_fragment(fragment, *tags)
  return unless perform_caching?

  tags = tags.flatten

  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']


80
81
82
# File 'lib/cashier.rb', line 80

def tags
  adapter.tags
end