Class: Cachetastic::Adapters::Redis

Inherits:
Base
  • Object
show all
Defined in:
lib/cachetastic/adapters/redis.rb

Instance Attribute Summary

Attributes inherited from Base

#klass

Instance Method Summary collapse

Methods inherited from Base

#debug?, #marshal, #unmarshal

Constructor Details

#initialize(klass) ⇒ Redis

Returns a new instance of Redis.


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cachetastic/adapters/redis.rb', line 5

def initialize(klass)
  define_accessor(:redis_host)
  define_accessor(:redis_options)
  define_accessor(:delete_delay)
  super
  self.redis_host ||= "redis://localhost:6379/"
  parsed_url = URI.parse(self.redis_host)
  self.redis_options = ::Redis::Client::DEFAULTS.merge({
    db: "cachetastic",
    url: self.redis_host,
    scheme: parsed_url.scheme,
    host: parsed_url.host,
    port: parsed_url.port,
    password: parsed_url.password
  })
  self.marshal_method = :yaml if self.marshal_method == :none
  connection
end

Instance Method Details

#delete(key) ⇒ Object

set


33
34
35
# File 'lib/cachetastic/adapters/redis.rb', line 33

def delete(key) # :nodoc:
  connection.del(transform_key(key))
end

#expire_allObject

delete


37
38
39
40
# File 'lib/cachetastic/adapters/redis.rb', line 37

def expire_all # :nodoc:
  connection.flushdb
  return nil
end

#get(key) ⇒ Object

:nodoc:


24
25
26
# File 'lib/cachetastic/adapters/redis.rb', line 24

def get(key) # :nodoc:
  connection.get(transform_key(key))
end

#set(key, value, expiry_time = configatron.cachetastic.defaults.default_expiry) ⇒ Object

get


28
29
30
31
# File 'lib/cachetastic/adapters/redis.rb', line 28

def set(key, value, expiry_time = configatron.cachetastic.defaults.default_expiry) # :nodoc:
  connection.setex(transform_key(key), expiry_time, value)
  return value
end

#transform_key(key) ⇒ Object

expire_all


42
43
44
# File 'lib/cachetastic/adapters/redis.rb', line 42

def transform_key(key) # :nodoc:
  key.to_s.hexdigest
end

#valid?Boolean

Returns:

  • (Boolean)

46
47
48
# File 'lib/cachetastic/adapters/redis.rb', line 46

def valid?
  !connection.nil?
end