Class: Readthis::Cache

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

Instance Attribute Summary collapse

Class Method 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.

Readthis::Cache.new('redis://localhost:6379/0', namespace: 'cache')


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

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.



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

def expires_in
  @expires_in
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

#poolObject (readonly)

Returns the value of attribute pool.



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

def pool
  @pool
end

Class Method Details

.notificationsObject

Provide a class level lookup of the proper notifications module. Instrumention is expected to occur within applications that have ActiveSupport::Notifications available, but needs to work even when it isn’t.



14
15
16
17
18
19
20
# File 'lib/readthis/cache.rb', line 14

def self.notifications
  if Object.const_defined?('ActiveSupport::Notifications')
    ActiveSupport::Notifications
  else
    Readthis::Notifications
  end
end

Instance Method Details

#clearObject



124
125
126
# File 'lib/readthis/cache.rb', line 124

def clear
  invoke(:clear, '*', &:flushdb)
end

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



77
78
79
80
81
# File 'lib/readthis/cache.rb', line 77

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

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



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

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

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

Returns:

  • (Boolean)


118
119
120
121
122
# File 'lib/readthis/cache.rb', line 118

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

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



60
61
62
63
64
65
66
67
68
69
# File 'lib/readthis/cache.rb', line 60

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

Fetches multiple keys from the cache using a single call to the server and filling in any cache misses. All read and write operations are executed atomically.

cache.fetch_multi('alpha', 'beta') do |key|
  "#{key}-was-missing"
end


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/readthis/cache.rb', line 99

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

  invoke(:fetch_multi, keys) 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

    results
  end
end

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



71
72
73
74
75
# File 'lib/readthis/cache.rb', line 71

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

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



35
36
37
38
39
# File 'lib/readthis/cache.rb', line 35

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

#read_multi(*keys) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/readthis/cache.rb', line 83

def read_multi(*keys)
  options = merged_options(extract_options!(keys))
  mapping = keys.map { |key| namespaced_key(key, options) }

  invoke(:read_multi, keys) do |store|
    keys.zip(store.mget(mapping)).to_h
  end
end

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



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

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

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