Class: Hyrax::RedisEventStore

Inherits:
Object
  • Object
show all
Defined in:
lib/hyrax/redis_event_store.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ RedisEventStore

Returns a new instance of RedisEventStore.



34
35
36
# File 'lib/hyrax/redis_event_store.rb', line 34

def initialize(key)
  @key = key
end

Class Method Details

.create(action, timestamp) ⇒ Fixnum

Returns the id of the event.

Returns:

  • (Fixnum)

    the id of the event



9
10
11
12
13
14
15
16
# File 'lib/hyrax/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) ⇒ Object



4
5
6
# File 'lib/hyrax/redis_event_store.rb', line 4

def for(key)
  new(key)
end

.instanceObject



20
21
22
23
24
25
26
27
# File 'lib/hyrax/redis_event_store.rb', line 20

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

.namespaceObject



29
30
31
# File 'lib/hyrax/redis_event_store.rb', line 29

def namespace
  Hyrax.config.redis_namespace
end

Instance Method Details

#fetch(size) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hyrax/redis_event_store.rb', line 38

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) ⇒ Object

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



51
52
53
54
55
56
# File 'lib/hyrax/redis_event_store.rb', line 51

def push(value)
  RedisEventStore.instance.lpush(@key, value)
rescue Redis::CommandError, Redis::CannotConnectError
  RedisEventStore.logger.error("unable to push event: #{@key}")
  nil
end