Skip to content

Instantly share code, notes, and snippets.

@CGA1123
Created October 9, 2020 15:46
Show Gist options
  • Save CGA1123/1f6eea5f3320766f8c6a3c303e380d8b to your computer and use it in GitHub Desktop.
Save CGA1123/1f6eea5f3320766f8c6a3c303e380d8b to your computer and use it in GitHub Desktop.
require 'concurrent'
module ActiveSupport
module Cache
class AsyncWriteStore < SimpleDelegator
def self.supports_cache_versioning?
true
end
def initialize(klass_or_sym, *args)
@store = build_store(klass_or_sym, args)
@pool = Concurrent::ThreadPoolExecutor.new(
min_threads: 5,
max_threads: 5,
max_queue: 100,
fallback_policy: :discard
)
ensure_store_supports_cache_versioning!
super(@store)
end
def ensure_store_supports_cache_versioning!
return if @store.class.supports_cache_versioning?
raise "#{@store.class.name} does not support cache versioning!"
end
def write(*args)
@pool << proc { @store.write(*args) }
end
private
def build_store(klass_or_sym, args)
if klass_or_sym.is_a?(Symbol)
ActiveSupport::Cache.lookup_store(klass_or_sym, *args)
else
klass_or_sym.new(*args)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment