Class: PatientHttp::PayloadStore::RedisStore

Inherits:
Base
  • Object
show all
Defined in:
lib/patient_http/payload_store/redis_store.rb

Overview

Redis-based payload store for production deployments.

Stores payloads as JSON strings in Redis. This store is recommended for production environments where multiple processes need to share payload data.

Thread-safe: Redis clients handle their own thread safety.

Examples:

Configuration with direct Redis client

redis = RedisClient.new(url: ENV["REDIS_URL"])
config.register_payload_store(:redis, adapter: :redis, redis: redis, ttl: 86400)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

create, #generate_key, lookup, register, registered_adapters, #store

Constructor Details

#initialize(redis:, ttl: nil, key_prefix: nil) ⇒ RedisStore

Initialize a new Redis store.

Parameters:

  • redis (Object)

    Redis client instance. Required.

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

    Time-to-live in seconds for stored payloads. Supports fractional seconds (e.g., 0.5 for 500ms). If nil, payloads do not expire.

  • key_prefix (String) (defaults to: nil)

    Prefix for all Redis keys. Defaults to “patient_payloads:”

Raises:

  • (ArgumentError)

    If redis client is not provided



33
34
35
36
37
38
39
# File 'lib/patient_http/payload_store/redis_store.rb', line 33

def initialize(redis:, ttl: nil, key_prefix: nil)
  raise ArgumentError, "redis client is required" unless redis

  @redis = redis
  @ttl = ttl
  @key_prefix = key_prefix || "patient_http:payloads:"
end

Instance Attribute Details

#key_prefixString (readonly)

Returns The key prefix used for all stored payloads.

Returns:

  • (String)

    The key prefix used for all stored payloads



20
21
22
# File 'lib/patient_http/payload_store/redis_store.rb', line 20

def key_prefix
  @key_prefix
end

#ttlFloat? (readonly)

Returns TTL in seconds for stored payloads.

Returns:

  • (Float, nil)

    TTL in seconds for stored payloads



23
24
25
# File 'lib/patient_http/payload_store/redis_store.rb', line 23

def ttl
  @ttl
end

Instance Method Details

#delete(key) ⇒ Boolean

Delete a payload from Redis.

Idempotent - does not raise if key doesn’t exist.

Parameters:

  • key (String)

    The key to delete

Returns:

  • (Boolean)

    true



76
77
78
79
80
# File 'lib/patient_http/payload_store/redis_store.rb', line 76

def delete(key)
  full_key = key_with_prefix(key)
  @redis.del(full_key)
  true
end

#exists?(key) ⇒ Boolean

Check if a payload exists.

Parameters:

  • key (String)

    The key to check

Returns:

  • (Boolean)

    true if the payload exists



86
87
88
89
# File 'lib/patient_http/payload_store/redis_store.rb', line 86

def exists?(key)
  full_key = key_with_prefix(key)
  @redis.exists(full_key) > 0
end

#fetch(key) ⇒ Hash?

Fetch data from Redis.

Parameters:

  • key (String)

    The key to fetch

Returns:

  • (Hash, nil)

    The stored data or nil if not found



62
63
64
65
66
67
68
# File 'lib/patient_http/payload_store/redis_store.rb', line 62

def fetch(key)
  full_key = key_with_prefix(key)
  json = @redis.get(full_key)
  return nil if json.nil?

  JSON.parse(json)
end

#store_json(key, json) ⇒ String

Store pre-serialized JSON string directly in Redis.

Parameters:

  • key (String)

    Unique key (appended to key_prefix)

  • json (String)

    Pre-serialized JSON string

Returns:

  • (String)

    The key



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

def store_json(key, json)
  full_key = key_with_prefix(key)

  if @ttl
    ttl_ms = (@ttl * 1000).round
    @redis.set(full_key, json, px: ttl_ms)
  else
    @redis.set(full_key, json)
  end
  key
end