Class: Readthis::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/readthis/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Cache

Creates a new Readthis::Cache object with the given redis URL. The URL is parsed by the redis client directly.



10
11
12
13
14
15
16
17
# File 'lib/readthis/cache.rb', line 10

def initialize(url, options = {})
  @expires_in = options.fetch(:expires_in, nil)
  @namespace  = options.fetch(:namespace, nil)

  @pool = ConnectionPool.new(pool_options(options)) do
    Redis.new(url: url)
  end
end

Instance Attribute Details

#expires_inObject (readonly)

Returns the value of attribute expires_in.



6
7
8
# File 'lib/readthis/cache.rb', line 6

def expires_in
  @expires_in
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



6
7
8
# File 'lib/readthis/cache.rb', line 6

def namespace
  @namespace
end

#poolObject (readonly)

Returns the value of attribute pool.



6
7
8
# File 'lib/readthis/cache.rb', line 6

def pool
  @pool
end

Instance Method Details

#clearObject



107
108
109
110
111
# File 'lib/readthis/cache.rb', line 107

def clear
  with do |store|
    store.flushdb
  end
end

#decrement(key, options = {}) ⇒ Object



61
62
63
64
65
# File 'lib/readthis/cache.rb', line 61

def decrement(key, options = {})
  with do |store|
    store.decr(namespaced_key(key, merged_options(options)))
  end
end

#delete(key, options = {}) ⇒ Object



38
39
40
41
42
# File 'lib/readthis/cache.rb', line 38

def delete(key, options = {})
  with do |store|
    store.del(namespaced_key(key, merged_options(options)))
  end
end

#exist?(key, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
105
# File 'lib/readthis/cache.rb', line 101

def exist?(key, options = {})
  with do |store|
    store.exists(namespaced_key(key, merged_options(options)))
  end
end

#fetch(key, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/readthis/cache.rb', line 44

def fetch(key, options = {})
  value = read(key, options) unless options[:force]

  if value.nil? && block_given?
    value = yield(key)
    write(key, value, options)
  end

  value
end

#fetch_multi(*keys) ⇒ Object

This must be done in two separate blocks. Redis pipelines return futures, which can not be resolved until the pipeline has exited.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/readthis/cache.rb', line 82

def fetch_multi(*keys)
  results = read_multi(*keys)
  options = merged_options(extract_options!(keys))

  with do |store|
    store.pipelined do
      results.each do |key, value|
        if value.nil?
          value = yield key
          write(key, value, options)
          results[key] = value
        end
      end
    end
  end

  results
end

#increment(key, options = {}) ⇒ Object



55
56
57
58
59
# File 'lib/readthis/cache.rb', line 55

def increment(key, options = {})
  with do |store|
    store.incr(namespaced_key(key, merged_options(options)))
  end
end

#read(key, options = {}) ⇒ Object



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

def read(key, options = {})
  with do |store|
    store.get(namespaced_key(key, merged_options(options)))
  end
end

#read_multi(*keys) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/readthis/cache.rb', line 67

def read_multi(*keys)
  options = merged_options(extract_options!(keys))
  results = []

  with do |store|
    results = store.pipelined do
      keys.each { |key| store.get(namespaced_key(key, options)) }
    end
  end

  keys.zip(results).to_h
end

#write(key, value, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/readthis/cache.rb', line 25

def write(key, value, options = {})
  options    = merged_options(options)
  namespaced = namespaced_key(key, options)

  with do |store|
    if expiration = options[:expires_in]
      store.setex(namespaced, expiration, value)
    else
      store.set(namespaced, value)
    end
  end
end