class Sufia::RedisEventStore

Public Class Methods

create(action, timestamp) click to toggle source

@return [Fixnum] the id of the event

# File lib/sufia/redis_event_store.rb, line 9
def create(action, timestamp)
  event_id = instance.incr("events:latest_id")
  instance.hmset("events:#{event_id}", "action", action, "timestamp", timestamp)
  event_id
rescue Redis::CommandError => e
  logger.error("unable to create event: #{e}")
  nil
end
for(key) click to toggle source
# File lib/sufia/redis_event_store.rb, line 4
def for(key)
  new(key)
end
instance() click to toggle source
# File lib/sufia/redis_event_store.rb, line 22
def instance
  if Redis.current.is_a? Redis::Namespace
    Redis.current.namespace = namespace
  else
    Redis.current = Redis::Namespace.new(namespace, redis: Redis.current)
  end
  Redis.current
end
logger() click to toggle source
# File lib/sufia/redis_event_store.rb, line 18
def logger
  Rails.logger || CurationConcerns::NullLogger.new
end
namespace() click to toggle source
# File lib/sufia/redis_event_store.rb, line 31
def namespace
  Sufia.config.redis_namespace
end
new(key) click to toggle source
# File lib/sufia/redis_event_store.rb, line 36
def initialize(key)
  @key = key
end

Public Instance Methods

fetch(size) click to toggle source
# File lib/sufia/redis_event_store.rb, line 40
def fetch(size)
  RedisEventStore.instance.lrange(@key, 0, size).map do |event_id|
    {
      action: RedisEventStore.instance.hget("events:#{event_id}", "action"),
      timestamp: RedisEventStore.instance.hget("events:#{event_id}", "timestamp")
    }
  end
rescue Redis::CommandError, Redis::CannotConnectError
  RedisEventStore.logger.error("unable to fetch event: #{@key}")
  []
end
push(value) click to toggle source

Adds a value to the end of a list identified by key

# File lib/sufia/redis_event_store.rb, line 53
def push(value)
  RedisEventStore.instance.lpush(@key, value)
rescue Redis::CommandError, Redis::CannotConnectError
  RedisEventStore.logger.error("unable to push event: #{@key}")
  nil
end