Module: NiceCache

Defined in:
lib/nice_cache.rb,
lib/nice_cache/tag.rb,
lib/nice_cache/version.rb,
lib/nice_cache/fragment.rb

Defined Under Namespace

Classes: Fragment, Tag

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.all_fragments_keyObject



37
38
39
# File 'lib/nice_cache.rb', line 37

def self.all_fragments_key
  "AllFragments"
end

.cache(data, tags) ⇒ Object



19
20
21
22
23
# File 'lib/nice_cache.rb', line 19

def self.cache(data, tags)
  tag_names = tags.is_a?(Hash) ? tags[:tags] : Array(tags)
  Fragment.new(data, tag_names)
  self
end

.cleanupObject



41
42
43
# File 'lib/nice_cache.rb', line 41

def self.cleanup
  fragments.map(&:destroy)
end

.config(&block) ⇒ Object



7
8
9
# File 'lib/nice_cache.rb', line 7

def self.config(&block)
  block.call(self)
end

.fragmentsObject



45
46
47
48
# File 'lib/nice_cache.rb', line 45

def self.fragments
  # O(N), N is number of fragment keys in NiceCache
  redis.smembers(all_fragments_key).map { |name| Fragment.find(name) }
end

.redisObject



11
12
13
# File 'lib/nice_cache.rb', line 11

def self.redis
  @redis ||= namespaced_redis
end

.redis=(custom_redis) ⇒ Object



15
16
17
# File 'lib/nice_cache.rb', line 15

def self.redis=(custom_redis)
  @redis = namespaced_redis(custom_redis)
end

.sweep_fragments(*fragment_names) ⇒ Object



31
32
33
34
35
# File 'lib/nice_cache.rb', line 31

def self.sweep_fragments(*fragment_names)
  fragment_names.map do |fragment_name|
    Fragment.find(fragment_name).destroy
  end
end

.sweep_tags(*tag_names) ⇒ Object



25
26
27
28
29
# File 'lib/nice_cache.rb', line 25

def self.sweep_tags(*tag_names)
  Tag.find(tag_names).map do |tag|
    tag.sweep
  end
end