From 2d24c666f0006a95cff1c819b22345baf266d21c Mon Sep 17 00:00:00 2001 From: Michael Grosser Date: Mon, 12 Feb 2024 14:58:06 -0800 Subject: [PATCH] fix memcached on ruby 3 --- config/application.rb | 1 + config/initializers/sockify.rb | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 config/initializers/sockify.rb diff --git a/config/application.rb b/config/application.rb index 468460d14..2ede208df 100644 --- a/config/application.rb +++ b/config/application.rb @@ -66,6 +66,7 @@ def self.deprecated_url(var) when "memory" config.cache_store = :memory_store # to debug cache keys, bundle open activesupport -> active_support/cache.rb#log when "memcached" + require_relative "initializers/sockify" options = { value_max_bytes: 3000000, compress: true, diff --git a/config/initializers/sockify.rb b/config/initializers/sockify.rb new file mode 100644 index 000000000..0e911137d --- /dev/null +++ b/config/initializers/sockify.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true +# +# TODO: remove one `rails runner 'Rails.cache.fetch 1'` works with memcached +# dalli has this code inside an `if RUBY_VERSION >= '3.0'` but only old version works +require 'dalli' + +class Dalli::Socket::TCP + def self.open(host, port, options = {}) + Timeout.timeout(options[:socket_timeout]) do + sock = new(host, port) + sock.options = {host: host, port: port}.merge(options) + init_socket_options(sock, options) + + options[:ssl_context] ? wrapping_ssl_socket(sock, host, options[:ssl_context]) : sock + end + end +end