Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into lud/strict-types
Browse files Browse the repository at this point in the history
  • Loading branch information
swiknaba committed Jul 18, 2023
2 parents 0624f51 + 5ca9c78 commit 37f2d41
Show file tree
Hide file tree
Showing 8 changed files with 11,488 additions and 14,493 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Style/HashSyntax:
Style/FrozenStringLiteralComment:
Enabled: false


Gemspec/RequiredRubyVersion:
Enabled: false

Naming/BlockForwarding:
Enabled: false

Expand Down
41 changes: 41 additions & 0 deletions lib/money/distributed/read_write_lock.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# typed: strict
# frozen_string_literal: true

class Money
module Distributed
class ReadWriteLock
extend T::Sig

sig do
params(
lock: Concurrent::ReentrantReadWriteLock,
_: T.proc.returns(T.untyped),
).returns(T.untyped)
end
def self.read(lock, &_)
lock.acquire_read_lock
begin
yield
ensure
lock.release_read_lock
nil
end
end

sig do
params(
lock: Concurrent::ReentrantReadWriteLock,
_: T.proc.void,
).void
end
def self.write(lock, &_)
lock.acquire_write_lock
begin
yield
ensure
lock.release_write_lock
end
end
end
end
end
18 changes: 11 additions & 7 deletions lib/money/distributed/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def initialize(redis, cache_ttl = nil)
@cache_ttl = cache_ttl
@cache_updated_at = T.let(nil, T.nilable(Time))

@mutex = Mutex.new
@lock = Concurrent::ReentrantReadWriteLock.new
end

sig do
Expand Down Expand Up @@ -81,8 +81,8 @@ def marshal_dump
[iso_from, iso_to].join(INDEX_KEY_SEPARATOR).upcase
end

private def cached_rates
@mutex.synchronize do
def cached_rates
Money::Distributed::ReadWriteLock.read(@lock) do
retrieve_rates if @cache.empty? || cache_outdated?
@cache
end
Expand All @@ -97,21 +97,25 @@ def marshal_dump
end

sig { void }
private def clear_cache
@mutex.synchronize do
def clear_cache
Money::Distributed::ReadWriteLock.write(@lock) do
@cache.clear
end
end

sig { returns(T.untyped) } # @TODO
sig { void }
private def retrieve_rates
updated_cache = {}

@redis.exec do |r|
r.hgetall(REDIS_KEY).each_with_object(@cache) do |(key, val), h|
r.hgetall(REDIS_KEY).each_with_object(updated_cache) do |(key, val), h|
next if val.nil? || val == ''

h[key] = BigDecimal(val)
end
end

@cache = updated_cache
@cache_updated_at = Time.now
end
end
Expand Down
1 change: 1 addition & 0 deletions money-distributed.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Gem::Specification.new do |spec|
'README.md',
]

spec.add_dependency 'concurrent-ruby'
spec.add_dependency 'connection_pool'
spec.add_dependency 'money', '>= 6.6.0'
spec.add_dependency 'redis'
Expand Down
Loading

0 comments on commit 37f2d41

Please sign in to comment.