Created
October 9, 2020 15:46
-
-
Save CGA1123/1f6eea5f3320766f8c6a3c303e380d8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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