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.

Examples:

Create a new cache instance

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

Create a compressed cache instance

Readthis::Cache.new('redis://localhost:6379/0', compress: true, compression_threshold: 2048)

Parameters:

  • A (String)

    redis compliant url with necessary connection details

  • [Boolean] (Hash)

    a customizable set of options

  • [Number] (Hash)

    a customizable set of options

  • [Module] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options



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

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

  @entity = Readthis::Entity.new(
    marshal:   options.fetch(:marshal, Marshal),
    compress:  options.fetch(:compress, false),
    threshold: options.fetch(:compression_threshold, 1024)
  )

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

Instance Attribute Details

#entityObject (readonly)

Returns the value of attribute entity.



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

def entity
  @entity
end

#expires_inObject (readonly)

Returns the value of attribute expires_in.



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

def expires_in
  @expires_in
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

#poolObject (readonly)

Returns the value of attribute pool.



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

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.



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

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

Instance Method Details

#clear(options = {}) ⇒ Object

Clear the entire cache. This flushes the current database, no globbing is applied.

Examples:


cache.clear #=> 'OK'

Parameters:

  • Options, (Hash)

    only present for compatibility.



241
242
243
# File 'lib/readthis/cache.rb', line 241

def clear(options = {})
  invoke(:clear, '*', &:flushdb)
end

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

Decrement a key in the store.

If the key doesn’t exist it will be initialized at 0. If the key exists but it isn’t a Fixnum it will be initialized at 0.

Examples:


cache.write('counter', 20) # => 20
cache.decrement('counter') # => 19
cache.decrement('counter', 2) # => 17

Parameters:

  • Key (String)

    for lookup

  • Value (Fixnum)

    to decrement by

  • Optional (Hash)

    overrides



163
164
165
166
167
# File 'lib/readthis/cache.rb', line 163

def decrement(key, amount = 1, options = {})
  invoke(:decrement, key) do |store|
    alter(key, amount * -1, options)
  end
end

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

Delete the value stored at the specified key. Returns ‘true` if anything was deleted, `false` otherwise.

Examples:


cache.delete('existing-key') # => true
cache.delete('random-key')   # => false


108
109
110
111
112
113
114
# File 'lib/readthis/cache.rb', line 108

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

  invoke(:delete, key) do |store|
    store.del(namespaced) > 0
  end
end

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

Returns ‘true` if the cache contains an entry for the given key.

Examples:


cache.exist?('some-key') # => false
cache.exist?('some-key', namespace: 'cache') # => true

Parameters:

  • Key (String)

    for lookup

  • Optional (Hash)

    overrides

Returns:

  • (Boolean)


227
228
229
230
231
# File 'lib/readthis/cache.rb', line 227

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

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



116
117
118
119
120
121
122
123
124
125
# File 'lib/readthis/cache.rb', line 116

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.

Examples:


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

cache.fetch_multi('a', 'b', expires_in: 60) do |key|
  key * 2
end

Return all values fro the given keys, applying the block to the key when a value is missing.

Parameters:

  • One (String)

    or more keys to fetch



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/readthis/cache.rb', line 198

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_entity(key, value, store, options)
          results[key] = value
        end
      end
    end

    results
  end
end

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

Increment a key in the store.

If the key doesn’t exist it will be initialized at 0. If the key exists but it isn’t a Fixnum it will be initialized at 0.

Examples:


cache.increment('counter') # => 0
cache.increment('counter') # => 1
cache.increment('counter', 2) # => 3

Parameters:

  • Key (String)

    for lookup

  • Value (Fixnum)

    to increment by

  • Optional (Hash)

    overrides



142
143
144
145
146
# File 'lib/readthis/cache.rb', line 142

def increment(key, amount = 1, options = {})
  invoke(:incremenet, key) do |store|
    alter(key, amount, options)
  end
end

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

Fetches data from the cache, using the given key. If there is data in the cache with the given key, then that data is returned. Otherwise, nil is returned.

Examples:


cache.read('missing') # => nil
cache.read('matched') # => 'some value'

Parameters:

  • Key (String)

    for lookup

  • Optional (Hash)

    overrides



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

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

    entity.load(value)
  end
end

#read_multi(*keys) ⇒ Object



169
170
171
172
173
174
175
176
177
178
# File 'lib/readthis/cache.rb', line 169

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

  invoke(:read_multi, keys) do |store|
    values = store.mget(mapping).map { |value| entity.load(value) }

    keys.zip(values).to_h
  end
end

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

Writes data to the cache using the given key. Will overwrite whatever value is already stored at that key.

Examples:


cache.write('some-key', 'a bunch of text')                     # => 'OK'
cache.write('some-key', 'short lived', expires_in: 60)         # => 'OK'
cache.write('some-key', 'lives elsehwere', namespace: 'cache') # => 'OK'

Parameters:

  • Key (String)

    for lookup

  • Optional (Hash)

    overrides



90
91
92
93
94
95
96
# File 'lib/readthis/cache.rb', line 90

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

  invoke(:write, key) do |store|
    write_entity(key, value, store, options)
  end
end