Class: Hyrax::RedisEventStore

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

Overview

TODO:

stop swallowing all errors! let clients determine how to handle failure cases.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EventStore

for, #initialize, logger

Constructor Details

This class inherits a constructor from Hyrax::EventStore

Class Method Details

.create(action, timestamp) ⇒ Fixnum?

Returns the id of the event, or `nil` on failure(?!).

Returns:

  • (Fixnum, nil)

    the id of the event, or `nil` on failure(?!)


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

.instanceRedis

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

this is NOT a singleton-ilke `.instance` method, it returns a `Redis` client.

Returns:

  • (Redis)

25
26
27
28
29
30
31
32
# File 'lib/hyrax/redis_event_store.rb', line 25

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

.namespaceString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)

37
38
39
# File 'lib/hyrax/redis_event_store.rb', line 37

def namespace
  Hyrax.config.redis_namespace
end

Instance Method Details

#fetch(size) ⇒ Enumerable<Hash<Symbol, String>>

Parameters:

  • size (Integer)

Returns:

  • (Enumerable<Hash<Symbol, String>>)

46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hyrax/redis_event_store.rb', line 46

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) ⇒ Integer?

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

Parameters:

  • value (Integer)

Returns:

  • (Integer, nil)

    the value successfully pushed; or `nil` on failure(!?)


64
65
66
67
68
69
# File 'lib/hyrax/redis_event_store.rb', line 64

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