class Hoodoo::TransientStore::Memcached

Hoodoo::TransientStore plugin for Memcached. The Dalli gem is used for server communication.

Attributes

client[RW]

Overrideable access to this object’s Dalli::Client instance. It is inadvisable to couple calling code directly into this interface as it is a Hoodoo::TransientStore::Memcached specialisation, but if deemed absolutely necessary the Dalli client can be override with a customised version.

Public Class Methods

new( storage_host_uri:, namespace: ) click to toggle source

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

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

The Dalli gem is used to talk to Memcached and accepts connection UIRs of simple, terse forms such as 'localhost:11211'. Connections are configured with JSON serialisation, compression off and a forced namespace of nz_co_loyalty_hoodoo_transient_store_ to avoid collision of data stored with this object and other data that may be in the Memcached instance identified by storage_host_uri.

Calls superclass method Hoodoo::TransientStore::Base::new
# File lib/hoodoo/transient_store/transient_store/memcached.rb, line 45
def initialize( storage_host_uri:, namespace: )
  super # Pass all arguments through -> *not* 'super()'
  @client = connect_to_memcached( storage_host_uri, namespace )
end

Public Instance Methods

close() click to toggle source

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

# File lib/hoodoo/transient_store/transient_store/memcached.rb, line 72
def close
  @client.close()
end
delete( key: ) click to toggle source

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

# File lib/hoodoo/transient_store/transient_store/memcached.rb, line 65
def delete( key: )
  @client.delete( key )
  true
end
get( key: ) click to toggle source

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

# File lib/hoodoo/transient_store/transient_store/memcached.rb, line 59
def get( key: )
  @client.get( key )
end
set( key:, payload:, maximum_lifespan: ) click to toggle source

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

# File lib/hoodoo/transient_store/transient_store/memcached.rb, line 52
def set( key:, payload:, maximum_lifespan: )
  @client.set( key, payload, maximum_lifespan )
  true
end

Private Instance Methods

connect_to_memcached( host, namespace ) click to toggle source

Connect to Memcached if possible and return the connected Dalli client instance, else raise an exception.

host

Connection URI, e.g. localhost:11211.

namespace

Key namespace (prefix), as a String or Symbol; e.g. "nz_co_loyalty_hoodoo_transient_store_".

# File lib/hoodoo/transient_store/transient_store/memcached.rb, line 85
def connect_to_memcached( host, namespace )
  exception = nil
  stats     = nil
  client    = nil

  if Hoodoo::Services::Middleware.environment.test? &&
     ( host.nil? || host.empty? )
     return Hoodoo::TransientStore::Mocks::DalliClient.new
  end

  begin
    client = ::Dalli::Client.new(
      host,
      {
        :compress   => false,
        :serializer => JSON,
        :namespace  => namespace
      }
    )

    stats = client.stats()

  rescue => e
    exception = e

  end

  if stats.nil?
    if exception.nil?
      raise "Hoodoo::TransientStore::Memcached: Did not get back meaningful data from Memcached at '#{ host }'"
    else
      raise "Hoodoo::TransientStore::Memcached: Cannot connect to Memcached at '#{ host }': #{ exception.to_s }"
    end
  else
    return client
  end
end