Class: Hoodoo::TransientStore::Redis

Inherits:
Base
  • Object
show all
Defined in:
lib/hoodoo/transient_store/transient_store/redis.rb

Overview

Hoodoo::TransientStore plugin supporting Redis. The redis-rb gem is used for server communication.

Instance Method Summary collapse

Constructor Details

#initialize(storage_host_uri:, namespace:) ⇒ Redis

See Hoodoo::TransientStore::Base::new for details.

Do not instantiate this class directly. Use Hoodoo::TransientStore::new.

The redis-rb gem is used to talk to Redis and requires connection UIRs with a redis protocol, such as redis://localhost:6379.

TCP keep-alive is enabled for the server connection.



36
37
38
39
# File 'lib/hoodoo/transient_store/transient_store/redis.rb', line 36

def initialize( storage_host_uri:, namespace: )
  super # Pass all arguments through -> *not* 'super()'
  @client = connect_to_redis( storage_host_uri )
end

Instance Method Details

#closeObject

See Hoodoo::TransientStore::Base#close for details.



71
72
73
# File 'lib/hoodoo/transient_store/transient_store/redis.rb', line 71

def close
  @client.quit()
end

#delete(key:) ⇒ Object

See Hoodoo::TransientStore::Base#delete for details.



64
65
66
67
# File 'lib/hoodoo/transient_store/transient_store/redis.rb', line 64

def delete( key: )
  @client.del( namespaced_key( key ) )
  true
end

#get(key:) ⇒ Object

See Hoodoo::TransientStore::Base#get for details.



57
58
59
60
# File 'lib/hoodoo/transient_store/transient_store/redis.rb', line 57

def get( key: )
  result = @client.get( namespaced_key( key ) )
  return result.nil? ? nil : ( JSON.parse( result ) rescue nil )
end

#set(key:, payload:, maximum_lifespan:) ⇒ Object

See Hoodoo::TransientStore::Base#set for details.

The payload is encoded into JSON for storage and automatically decoded by #get; so callers don’t need to do marshalling to Strings themselves.



46
47
48
49
50
51
52
53
# File 'lib/hoodoo/transient_store/transient_store/redis.rb', line 46

def set( key:, payload:, maximum_lifespan: )
  nk = namespaced_key( key )

  @client.set( nk, JSON.fast_generate( payload ) )
  @client.expire( nk, maximum_lifespan )

  true
end