class Cacchern::Object

Attributes

id[R]
key[R]
value[R]

Public Class Methods

find(id) click to toggle source
# File lib/cacchern/object.rb, line 12
def find(id)
  key = self.key(id)
  value = Redis.current.get(key)

  new(id, value) if value
end
key(id) click to toggle source
# File lib/cacchern/object.rb, line 8
def key(id)
  "#{name.underscore}:#{id}"
end
new(id, value) click to toggle source
# File lib/cacchern/object.rb, line 24
def initialize(id, value)
  @id = id
  @key = "#{self.class.name.underscore}:#{id}"
  @value = value
end

Public Instance Methods

delete() click to toggle source
# File lib/cacchern/object.rb, line 39
def delete
  Redis.current.del(key)
end
save(options = {}) click to toggle source
# File lib/cacchern/object.rb, line 30
def save(options = {})
  expires_in = options[:expires_in]
  valid? ? create_or_update(expires_in) : false
end
save!(options = {}) click to toggle source
# File lib/cacchern/object.rb, line 35
def save!(options = {})
  save(options) || raise_validation_error
end

Private Instance Methods

create_or_update(expires_in = nil) click to toggle source
# File lib/cacchern/object.rb, line 45
def create_or_update(expires_in = nil)
  if expires_in
    Redis.current.setex(key, expires_in.to_i, value)
  else
    Redis.current.set(key, value) == 'OK'
  end
end