Class: Shrinker::Backend::Redis

Inherits:
Abstract
  • Object
show all
Defined in:
lib/shrinker/backend/redis.rb

Instance Attribute Summary

Attributes inherited from Abstract

#options

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Redis

Returns a new instance of Redis.



6
7
8
9
10
11
# File 'lib/shrinker/backend/redis.rb', line 6

def initialize(options = {})
  super
  @client_options = options[:client] || {}
  @client         = options[:connection]
  @namespace      = options[:namespace] || '_shrinker'
end

Instance Method Details

#fetch(token) ⇒ Object



23
24
25
26
27
28
# File 'lib/shrinker/backend/redis.rb', line 23

def fetch(token)
  content = client.get(key(token))

  return {} unless content
  Shrinker::Serializer::from_json(content)
end

#store(raw_url, token, attributes = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/shrinker/backend/redis.rb', line 13

def store(raw_url, token, attributes = {})
  content   = Shrinker::Serializer::to_json(raw_url, attributes)
  redis_key = key(token)
  if ttl
    client.setex(redis_key, ttl, content)
  else
    client.set(redis_key, content)
  end
end

#used_token?(token) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/shrinker/backend/redis.rb', line 30

def used_token?(token)
  !!client.get(key(token))
end