class Anemone::Storage::Redis

Constants

MARSHAL_FIELDS

Public Class Methods

new(opts = {}) click to toggle source
# File lib/anemone/storage/redis.rb, line 9
def initialize(opts = {})
  @redis = ::Redis.new(opts)
  @key_prefix = opts[:key_prefix] || 'anemone'
  keys.each { |key| delete(key) }
end

Public Instance Methods

[](key) click to toggle source
# File lib/anemone/storage/redis.rb, line 15
def [](key)
  rkey = "#{@key_prefix}:pages:#{key.to_s}"
  rget(rkey)
end
[]=(key, value) click to toggle source
# File lib/anemone/storage/redis.rb, line 20
def []=(key, value)
  rkey = "#{@key_prefix}:pages:#{key.to_s}"
  hash = value.to_hash
  MARSHAL_FIELDS.each do |field|
    hash[field] = Marshal.dump(hash[field])
  end
  hash.each do |field, value|
    @redis.hset(rkey, field, value)
  end
end
close() click to toggle source
# File lib/anemone/storage/redis.rb, line 66
def close
  @redis.quit
end
delete(key) click to toggle source
# File lib/anemone/storage/redis.rb, line 31
def delete(key)
  rkey = "#{@key_prefix}:pages:#{key.to_s}"
  page = self[key]
  @redis.del(rkey)
  page
end
each() { |url.to_s, page| ... } click to toggle source
# File lib/anemone/storage/redis.rb, line 38
def each
  rkeys = @redis.keys("#{@key_prefix}:pages:*")
  rkeys.each do |rkey|
    page = rget(rkey)
    yield page.url.to_s, page
  end
end
has_key?(key) click to toggle source
# File lib/anemone/storage/redis.rb, line 61
def has_key?(key)
  rkey = "#{@key_prefix}:pages:#{key.to_s}"
  @redis.exists(rkey)
end
keys() click to toggle source
# File lib/anemone/storage/redis.rb, line 55
def keys
  keys = []
  self.each { |k, v| keys << k.to_s }
  keys
end
merge!(hash) click to toggle source
# File lib/anemone/storage/redis.rb, line 46
def merge!(hash)
  hash.each { |key, value| self[key] = value }
  self
end
size() click to toggle source
# File lib/anemone/storage/redis.rb, line 51
def size
  @redis.keys("#{@key_prefix}:pages:*").size
end

Private Instance Methods

load_value(hash) click to toggle source
# File lib/anemone/storage/redis.rb, line 72
def load_value(hash)
  MARSHAL_FIELDS.each do |field|
    unless hash[field].nil? || hash[field] == ''
      hash[field] = Marshal.load(hash[field]) 
    end
  end
  Page.from_hash(hash)
end
rget(rkey) click to toggle source
# File lib/anemone/storage/redis.rb, line 81
def rget(rkey)
  hash = @redis.hgetall(rkey)
  if !!hash
    load_value(hash)
  end
end