Class: Sir::Backends::RedisCache

Inherits:
Base
  • Object
show all
Defined in:
lib/sir/backends/redis_cache.rb

Constant Summary collapse

TYPEERROR =
"Redis does not need or support this function"
META =
{
    name:   "Redis Cache",
    author: "Lyjia"
}
DEFAULTS =
{
    redis_obj:  nil,
    namespace:  'SirCachealot',
    serializer: :marshal
}
@@config =
DEFAULTS
@@redis =
@@config[:redis_obj]

Constants inherited from Base

Base::EXPORTS

Class Method Summary collapse

Methods inherited from Base

arity, configure

Class Method Details

.able?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/sir/backends/redis_cache.rb', line 87

def self.able?
  return TYPEERROR == @@redis.echo(TYPEERROR)
end

.dumpObject

Raises:

  • (TypeError)


102
103
104
# File 'lib/sir/backends/redis_cache.rb', line 102

def self.dump
  raise TypeError, TYPEERROR
end

.flushObject



117
118
119
# File 'lib/sir/backends/redis_cache.rb', line 117

def self.flush
  return true if @@redis.bgsave
end

.get(key) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sir/backends/redis_cache.rb', line 22

def self.get(key)
  invalid = self.valid?({ key: key })
  raise ArgumentError, invalid if invalid

  nskey = self::nsed_key(key)
  got = @@redis.get(nskey)

  unless got.nil?

    case @@config[:serializer]
      when :marshal
        return Marshal.load(got)[0]
      when :json
        return JSON.parse(got)[0]
      else
        raise TypeError, "Invalid serializer: #{@@config[:serializer]}. You probably want to look at your Sir.configure() statement"
    end

  else
    super
  end
end

.keys(mask = "*") ⇒ Object



127
128
129
# File 'lib/sir/backends/redis_cache.rb', line 127

def self.keys(mask = "*")
  return @@redis.keys(mask).map {|x| x.gsub(/^#{self.nsed_key("")}/, '').intern}
end

.kill(key) ⇒ Object

Raises:

  • (ArgumentError)


92
93
94
95
96
97
98
99
# File 'lib/sir/backends/redis_cache.rb', line 92

def self.kill(key)
  invalid = self.valid?({ key: key })
  raise ArgumentError, invalid if invalid

  if @@redis.del(self::nsed_key(key))
    return true
  end
end

.lengthObject



122
123
124
# File 'lib/sir/backends/redis_cache.rb', line 122

def self.length
  return @@redis.dbsize
end

.nukeObject



107
108
109
# File 'lib/sir/backends/redis_cache.rb', line 107

def self.nuke
  return true if @@redis.flushdb
end

.post_configureObject



132
133
134
# File 'lib/sir/backends/redis_cache.rb', line 132

def self.post_configure
  @@redis = @@config[:redis_obj]
end

.put(key, value, expiry = Sir.config(:default_expiry)) ⇒ Object

Note:

We wrap ‘value` in `[ ]` to protect nil values and such from being misinterpreted JSON in particular: JSON.parse(nil.to_json) #=> JSON::ParserError

but JSON.parse([nil].to_json) #=> [nil]

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sir/backends/redis_cache.rb', line 49

def self.put(key, value, expiry = Sir.config(:default_expiry))
  invalid = self.valid?({ key: key, value: value, expiry: expiry })
  raise ArgumentError, invalid if invalid

  key = self::nsed_key(key)
  ser = nil

  case @@config[:serializer]
    when :marshal
      ser = Marshal.dump([value]).to_s
    when :json
      ser = [value].to_json
    else
      raise TypeError, "Invalid serializer: #{@@config[:serializer]}. You probably want to look at your Sir.configure() statement"
  end

  @@redis.set(key, ser)

  #### This code snippet needs to be DRYed
  expiry = expiry.to_i unless expiry.nil?

  # normalize relative/absolute times to absolute time, skip if expiry = nil
  if !expiry.nil? && Time.now.to_i > expiry
    expiry += Time.now.to_i
  end
  ####

  if expiry == nil
    @@redis.persist(key)
  else
    @@redis.expireat(key, expiry.to_i)
  end

  return value

end

.sweep(include_nil_expiry = nil) ⇒ Object

Raises:

  • (TypeError)


112
113
114
# File 'lib/sir/backends/redis_cache.rb', line 112

def self.sweep(include_nil_expiry = nil)
  raise TypeError, TYPEERROR
end