Class: Phronomy::StateStore::Redis

Inherits:
Base
  • Object
show all
Defined in:
lib/phronomy/state_store/redis.rb

Overview

Redis-backed state store. Persists graph state as a JSON string under the key "phronomy:state:" in Redis.

The Redis client must be compatible with the redis-rb gem interface: client.set(key, value) client.get(key) client.del(key)

Examples:

require "redis"
redis = Redis.new(url: ENV["REDIS_URL"])
Phronomy.configure do |c|
  c.default_state_store = Phronomy::StateStore::Redis.new(client: redis)
end

with TTL

Phronomy::StateStore::Redis.new(client: redis, ttl: 3600)

Instance Method Summary collapse

Constructor Details

#initialize(client:, ttl: nil) ⇒ Redis

Returns a new instance of Redis.

Parameters:

  • client (#set, #get, #del)

    Redis-compatible client

  • ttl (Integer, nil) (defaults to: nil)

    optional key expiry in seconds



31
32
33
34
# File 'lib/phronomy/state_store/redis.rb', line 31

def initialize(client:, ttl: nil)
  @client = client
  @ttl = ttl
end

Instance Method Details

#clear(thread_id) ⇒ self

Returns:

  • (self)


58
59
60
61
# File 'lib/phronomy/state_store/redis.rb', line 58

def clear(thread_id)
  @client.del(key(thread_id))
  self
end

#load(thread_id) ⇒ Object?

Returns state instance or nil.

Parameters:

  • thread_id (String)

Returns:

  • (Object, nil)

    state instance or nil



50
51
52
53
54
55
# File 'lib/phronomy/state_store/redis.rb', line 50

def load(thread_id)
  raw = @client.get(key(thread_id))
  return nil unless raw

  deserialize_state(raw)
end

#save(state) ⇒ self

Parameters:

  • state (Object)

    includes Phronomy::Graph::State

Returns:

  • (self)


38
39
40
41
42
43
44
45
46
# File 'lib/phronomy/state_store/redis.rb', line 38

def save(state)
  serialized = serialize_state(state)
  if @ttl
    @client.set(key(state.thread_id), serialized, ex: @ttl)
  else
    @client.set(key(state.thread_id), serialized)
  end
  self
end