Class: NiceCache

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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

COMMANDS_WITH_TAGS =
[
  :set,
  :setnx,
  :hmset,
  :hsetnx,
  :lset,
  :sadd,
  :zadd,
  # :mset,
  # :msetnx,
  # :zinterstore,
  # :zunionstore
]
VERSION =
"0.2.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ NiceCache

Returns a new instance of NiceCache.



48
49
50
51
# File 'lib/nice_cache.rb', line 48

def initialize(options = {})
  @redis = options[:redis] || Redis.current
  @tag_store = Redis::Namespace.new(:NiceCache, redis: redis)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/nice_cache.rb', line 53

def method_missing(method, *args, &block)
  return super unless redis.respond_to?(method)

  self.class.send(:define_method, method, ->(*arguments) {
    redis.send(method, *arguments, &block)
  })

  self.send(method, *args, &block)
end

Instance Attribute Details

#redisObject

Returns the value of attribute redis.



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

def redis
  @redis
end

#tag_storeObject

Returns the value of attribute tag_store.



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

def tag_store
  @tag_store
end

Instance Method Details

#all_fragmentsObject



79
80
81
# File 'lib/nice_cache.rb', line 79

def all_fragments
  tag_store.smembers Fragment.all
end

#convert_tags(arg) ⇒ Object



24
25
26
# File 'lib/nice_cache.rb', line 24

def convert_tags(arg)
  arg.respond_to?(:key) ? arg[:tags] : arg
end

#del(fragment_name) ⇒ Object



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

def del(fragment_name)
  Fragment.find(self, fragment_name).delete
end

#destroy(key) ⇒ Object



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

def destroy(key)
  redis.del key
end

#flush_index!Object



73
74
75
76
77
# File 'lib/nice_cache.rb', line 73

def flush_index!
  #TODO: Benchmark
  # all_fragments.map { |fragment| fragment.delete }
  tag_store.keys.map { |key| tag_store.del key }
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/nice_cache.rb', line 63

def respond_to_missing?(method_name, include_private = false)
  redis.respond_to?(method_name, include_private) || super
end

#sweep_tags(*tag_names) ⇒ Object



67
68
69
70
71
# File 'lib/nice_cache.rb', line 67

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