class Restruct::Cache

Attributes

ttl[R]

Public Class Methods

new(options={}) click to toggle source
Calls superclass method
# File lib/restruct/cache.rb, line 8
def initialize(options={})
  super options
  @ttl = options[:ttl]
end

Public Instance Methods

[](key) click to toggle source
# File lib/restruct/cache.rb, line 25
def [](key)
  deserialize connection.call('GET', id[key])
end
[]=(key, value) click to toggle source
# File lib/restruct/cache.rb, line 29
def []=(key, value)
  connection.lazy 'SET', id[key], serialize(value)
  connection.lazy 'EXPIRE', id[key], ttl if ttl
end
clear()
Alias for: destroy
count()
Alias for: size
delete(key) click to toggle source
# File lib/restruct/cache.rb, line 34
def delete(key)
  value = self[key]
  connection.lazy 'DEL', id[key]
  value
end
destroy() click to toggle source
# File lib/restruct/cache.rb, line 77
def destroy
  keys.each { |k| connection.lazy 'DEL', id[k] }
  self
end
Also aliased as: clear
dump()
Alias for: to_h
each() { |key, self| ... } click to toggle source
# File lib/restruct/cache.rb, line 51
def each
  keys.each { |key| yield key, self[key] }
end
empty?() click to toggle source
# File lib/restruct/cache.rb, line 61
def empty?
  size == 0
end
fetch(key, &block) click to toggle source
# File lib/restruct/cache.rb, line 40
def fetch(key, &block)
  if key? key
    connection.lazy 'EXPIRE', id[key], ttl if ttl
    self[key]
  else
    value = block.call
    self[key] = value
    value
  end
end
has_key?(key)
Alias for: key?
key?(key) click to toggle source
# File lib/restruct/cache.rb, line 13
def key?(key)
  connection.call('EXISTS', id[key]) == 1
end
Also aliased as: has_key?
keys() click to toggle source
# File lib/restruct/cache.rb, line 18
def keys
  sections = id.sections.count + 1
  connection.call('KEYS', id['*']).map do |k| 
    Id.new(k).sections.take(sections).last
  end.uniq.sort
end
length()
Alias for: size
restore(dump) click to toggle source
# File lib/restruct/cache.rb, line 73
def restore(dump)
  dump.each { |k,v| self[k] = v }
end
size() click to toggle source
# File lib/restruct/cache.rb, line 55
def size
  keys.count
end
Also aliased as: count, length
to_h() click to toggle source
# File lib/restruct/cache.rb, line 65
def to_h
  keys.each_with_object({}) do |key, hash|
    hash[key] = self[key]
  end
end
Also aliased as: to_primitive, dump
to_primitive()
Alias for: to_h

Private Instance Methods

deserialize(string) click to toggle source
# File lib/restruct/cache.rb, line 89
def deserialize(string)
  string
end
serialize(string) click to toggle source
# File lib/restruct/cache.rb, line 85
def serialize(string)
  string
end