class Moderation::Storage::Redis

Attributes

collection[R]
limit[RW]
server[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/moderation/storage/redis.rb, line 9
def initialize(options = {}) 
  @limit = options.fetch(:limit, Moderation::DEFAULT_LIMIT)
  @collection = options.fetch(:collection)
  @server = options.fetch(:server, nil)
end

Public Instance Methods

all(options = {}) click to toggle source
# File lib/moderation/storage/redis.rb, line 22
def all(options = {})
  fetch_limit = options.fetch(:limit, limit).to_i - 1
  redis.lrange(collection, 0, fetch_limit) || []
end
insert(item) click to toggle source
# File lib/moderation/storage/redis.rb, line 15
def insert(item)
  transaction do
    redis.lpush(collection, item)
    redis.ltrim(collection, 0, (limit - 1))
  end
end

Private Instance Methods

redis() click to toggle source
# File lib/moderation/storage/redis.rb, line 29
def redis
  @redis ||= begin
    # graciously borrowed from https://github.com/defunkt/resque
    case server
    when String
      if server['redis://']
        redis_connection = ::Redis.connect(:url => server, :thread_safe => true)
      else
        url, namespace = server.split('/', 2)
        host, port, db = server.split(':')
        redis_connection = ::Redis.new(:host => host, :port => port,
          :thread_safe => true, :db => db)
      end
      namespace ||= :moderation

      ::Redis::Namespace.new(namespace, :redis => redis_connection)
    when ::Redis::Namespace
      server
    else
      ::Redis::Namespace.new(:moderation, :redis => server)
    end
  end
end
redis_version() click to toggle source
# File lib/moderation/storage/redis.rb, line 67
def redis_version
  @redis_version ||= redis.info["redis_version"]
end
transaction() { || ... } click to toggle source
# File lib/moderation/storage/redis.rb, line 53
def transaction(&block)
  if transactions_supported?
    redis.multi { yield }
  else
    yield
  end
end
transactions_supported?() click to toggle source
# File lib/moderation/storage/redis.rb, line 61
def transactions_supported?
  @transactions_supported ||= begin
    redis_version.split('.').first.to_i >= 2 && redis.respond_to?(:multi)
  end
end