From 05614c07c66c6b11e918444323587bc326c55b64 Mon Sep 17 00:00:00 2001 From: Jurgens du Toit Date: Wed, 20 Mar 2024 06:26:26 +0200 Subject: [PATCH] chore: Stabler cache service --- .rubocop.yml | 2 +- lib/ditty/services/cache.rb | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 222d6d4..aa642bb 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -5,7 +5,7 @@ require: - rubocop-sequel AllCops: NewCops: enable - TargetRubyVersion: 2.7.1 + TargetRubyVersion: 3.2 Exclude: - migrate/**/* Layout/LineLength: diff --git a/lib/ditty/services/cache.rb b/lib/ditty/services/cache.rb index d1ac2a0..7bd5035 100644 --- a/lib/ditty/services/cache.rb +++ b/lib/ditty/services/cache.rb @@ -2,6 +2,7 @@ require 'dalli' require 'net/telnet' +require 'memcached' module Ditty module Services @@ -17,16 +18,21 @@ def initialize(store: nil, ttl: 3_600, namespace: 'Ditty') end def store - @store ||= Dalli::Client.new(ENV.fetch('CACHE_URL'), namespace: namespace) + @store ||= begin + Dalli::Client.new(ENV.fetch('CACHE_URL'), namespace: namespace) + rescue KeyError + logger.error 'Could not initialize Cache - No URL' + nil + end end def set(key, obj) - store.set(key, obj, ttl) + store&.set(key, obj, ttl) obj end def get(key) - store.get(key) + store&.get(key) rescue Memcached::ServerIsMarkedDead => e logger.warn "Could not retrieve cache key #{key}: #{e.message}" nil @@ -37,7 +43,7 @@ def clear(key) end def delete(key) - store.delete(key) + store&.delete(key) end def dump @@ -90,6 +96,10 @@ def cache_items end items end + + def logger + @logger ||= ::Ditty::Services::Logger + end end end end