Skip to content

Commit

Permalink
chore: Stabler cache service
Browse files Browse the repository at this point in the history
  • Loading branch information
jrgns committed Mar 20, 2024
1 parent 4444060 commit 05614c0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require:
- rubocop-sequel
AllCops:
NewCops: enable
TargetRubyVersion: 2.7.1
TargetRubyVersion: 3.2
Exclude:
- migrate/**/*
Layout/LineLength:
Expand Down
18 changes: 14 additions & 4 deletions lib/ditty/services/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'dalli'
require 'net/telnet'
require 'memcached'

module Ditty
module Services
Expand All @@ -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
Expand All @@ -37,7 +43,7 @@ def clear(key)
end

def delete(key)
store.delete(key)
store&.delete(key)
end

def dump
Expand Down Expand Up @@ -90,6 +96,10 @@ def cache_items
end
items
end

def logger
@logger ||= ::Ditty::Services::Logger
end
end
end
end
Expand Down

0 comments on commit 05614c0

Please sign in to comment.