Skip to content

Commit

Permalink
Ensure that concurrent_write does in fact cause a require
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Brown committed Jul 9, 2024
1 parent 7c8d50e commit db65a30
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/cached_resource/cached_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def cached_resource(options={})
# and establishing the necessary methods.
def setup_cached_resource!(options)
@cached_resource = CachedResource::Configuration.new(options)
# puts "OK WE ARE SET UP!"
if @cached_resource.concurrent_write
@cached_resource.require_concurrent_ruby
# begin
Expand Down
23 changes: 14 additions & 9 deletions lib/cached_resource/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Configuration < OpenStruct
# prefix for log messages
LOGGER_PREFIX = "[cached_resource]"

attr_reader :concurrent_write

# Initialize a Configuration with the given options, overriding any
# defaults. The following options exist for cached resource:
# :enabled, default: true
Expand All @@ -26,7 +28,7 @@ class Configuration < OpenStruct
# :cache_collections, default: true
# :concurrent_write, default: false
def initialize(options={})
super({
data = {
:cache => defined?(Rails.cache) && Rails.cache || CACHE,
:cache_collections => true,
:cache_key_prefix => nil,
Expand All @@ -39,7 +41,12 @@ def initialize(options={})
:ttl => 604800,
:ttl_randomization => false,
:ttl_randomization_scale => 1..2
}.merge(options))
}.merge(options)

# Set our concurrent_write. Can't override OpenStruct setters
# in a straightforward way.
@concurrent_write = data.delete :concurrent_write
super(data)
end

# Determine the time until a cache entry should expire. If ttl_randomization
Expand All @@ -60,17 +67,15 @@ def off!
end

def concurrent_write=(value)
super(value)

if value
self.require_concurrent_ruby
end
require_concurrent_ruby if value
@concurrent_write = value
puts "WE REQUIRED ID!\n", value
end

# require concurrent/promise, throwing an exception if necessary
def require_concurrent_ruby
begin
# send :require, 'concurrent/promise'
require 'concurrent/promise'
send :require, 'concurrent/promise'
rescue LoadError
@cached_resource.logger.error(
"`concurrent_write` option is enabled, but `concurrent-ruby` is not an installed dependency"
Expand Down

0 comments on commit db65a30

Please sign in to comment.