Class: HTTParty::Cache::Store::Redis

Inherits:
Abstract
  • Object
show all
Defined in:
lib/httparty/cache/store/redis.rb

Constant Summary collapse

NIL =

A Marshaled ‘nil` value

"\x04\b0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis_url: nil) ⇒ Redis

Returns a new instance of Redis.

Parameters:

  • :redis_url (String)


23
24
25
# File 'lib/httparty/cache/store/redis.rb', line 23

def initialize(redis_url: nil)
  marshal_load(redis_url)
end

Instance Attribute Details

#optionsHash (readonly)

Returns:

  • (Hash)


20
21
22
# File 'lib/httparty/cache/store/redis.rb', line 20

def options
  @options
end

Class Method Details

._load(string) ⇒ Object



45
46
47
# File 'lib/httparty/cache/store/redis.rb', line 45

def self._load(string)
  new(redis_url: string)
end

Instance Method Details

#_dump(level) ⇒ Object

I couldn’t get this to work! It’s never called even if I dump the store directly



41
42
43
# File 'lib/httparty/cache/store/redis.rb', line 41

def _dump(level)
  options[:redis_url]
end

#clearObject



94
95
96
97
# File 'lib/httparty/cache/store/redis.rb', line 94

def clear
  redis('FLUSHALL', 'SYNC')
  nil
end

#delete(key) ⇒ nil

Removes a key from Redis

Parameters:

  • :key (String)

Returns:

  • (nil)


81
82
83
84
# File 'lib/httparty/cache/store/redis.rb', line 81

def delete(key)
  redis('DEL', key)
  nil
end

#get(key) ⇒ Response?

Retrieves a key

Parameters:

  • :key (String)

Returns:



53
54
55
# File 'lib/httparty/cache/store/redis.rb', line 53

def get(key)
  Marshal.load(redis('GET', key) || NIL)
end

#getset(key) ⇒ Response?

Returns a key or set its value to a block result

Parameters:

  • :key (String)

Returns:



71
72
73
74
75
# File 'lib/httparty/cache/store/redis.rb', line 71

def getset(key)
  return get(key) if key?(key)

  set(key, yield)
end

#key?(key) ⇒ Boolean

Discovers if a key exists

Parameters:

  • :key (String)

Returns:

  • (Boolean)


90
91
92
# File 'lib/httparty/cache/store/redis.rb', line 90

def key?(key)
  redis('EXISTS', key).positive?
end

#marshal_dumpObject



27
28
29
# File 'lib/httparty/cache/store/redis.rb', line 27

def marshal_dump
  options[:redis_url]
end

#marshal_load(redis_url) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/httparty/cache/store/redis.rb', line 31

def marshal_load(redis_url)
  @options = {}
  @options[:redis_url] = redis_url || ENV['REDIS_URL']
  @config = RedisClient.config(url: @options[:redis_url])
  @redis = @config.new_pool(size: 2)
  @redis.call('PING')
end

#set(key, response) ⇒ Response

Stores a Marshaled value indexed by a key.

Parameters:

Returns:



62
63
64
65
# File 'lib/httparty/cache/store/redis.rb', line 62

def set(key, response)
  redis('SET', key, Marshal.dump(response))
  response
end