Skip to content

Using Dalli with SSL TLS

Ian Young edited this page Oct 21, 2024 · 2 revisions

Dalli supports SSL/TLS connections to memcached servers.

If your memcached servers' certificates are not signed by a CA in the certificate chain provided by your OS, you will need a PEM file containing the certificate chain for the certificate authority (CA) that signed the certificates.

An example of how to configure a Dalli client using SSL/TLS given the above is below:

require 'dalli'
require 'openssl'

server_configuration = 'localhost:11212'

ssl_context = OpenSSL::SSL::SSLContext.new

ssl_context.set_params(
  min_version: :TLS1_2, # Can set max_version as well if desired
  verify_hostname: true, # Skip this line if using JRuby
  verify_mode: OpenSSL::SSL::VERIFY_PEER,
  ca_file: <path to PEM file> # Omit this line to use the default CA files
)

dc = Dalli::Client.new(server_configuration, ssl_context: ssl_context)

# Now test it out
dc.set('abc', 123)
from_cache = dc.get('abc')