diff --git a/CHANGELOG.md b/CHANGELOG.md index cf6a245..b32f0e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ This project adheres to [Semantic Versioning]. Changes: - Drop support for Rails < 7.0. +Feature: +- Add `cache_enabled` to config to be able to enable/disable cache. + Fix: - Make `RateStore` marshalable so a bank using it can be marshaled. diff --git a/README.md b/README.md index 092ae2c..a1d6316 100644 --- a/README.md +++ b/README.md @@ -33,15 +33,15 @@ ActiveCurrency::AddRates.call(currencies: %w[EUR USD]) You can then exchange money by using the Money gem helpers: ```rb -10.to_money('EUR').exchange_to('USD').cents +10.to_money("EUR").exchange_to("USD").cents ``` If you need to look up the previous currency rates: ```rb -ActiveCurrency::Rate.value_for('EUR', 'USD', 1.month.ago) +ActiveCurrency::Rate.value_for("EUR", "USD", 1.month.ago) # => 1.151 -ActiveCurrency::Rate.where(from: 'EUR', to: 'USD').pluck(:value) +ActiveCurrency::Rate.where(from: "EUR", to: "USD").pluck(:value) # => [1.162, 1.162, 1.161, 1.161, 1.163, …] ``` @@ -88,7 +88,7 @@ add the following to your application’s initializers: ```rb ActiveCurrency.configure do |config| config.remote_bank = :open_exchange_rates - config.open_exchange_rates_app_id = '…' + config.open_exchange_rates_app_id = "…" end ``` @@ -101,16 +101,21 @@ You can provide any Money-compatible bank when calling ActiveCurrency::AddRates.call(…, bank: …) ``` -## Apply a multiplier - -If you want to increase or decrease the currency rates by a given multiplier, -you can do so by setting the `multiplier` option: +## Configuration ```rb ActiveCurrency.configure do |config| - config.multiplier = { - ["EUR", "USD"] => 1.01, - } + # Use the money-open-exchange-rates bank instead. + # config.remote_bank = :open_exchange_rates + # config.open_exchange_rates_app_id = "…" + + # Disable `Rails.cache`. + # config.cache_enabled = false + + # Increase or decrease the currency rates by a given multiplier. + # config.multiplier = { + # ["EUR", "USD"] => 1.01, + # } end ``` @@ -124,8 +129,8 @@ For that, you can use a fake rate store in your `rails_helper.rb`: ```rb MoneyRails.configure do |config| rate_store = Money::RatesStore::Memory.new.tap do |store| - store.add_rate('USD', 'EUR', 1.5) - store.add_rate('EUR', 'USD', 0.67) + store.add_rate("USD", "EUR", 1.5) + store.add_rate("EUR", "USD", 0.67) end config.default_bank = Money::Bank::VariableExchange.new(rate_store) end diff --git a/lib/active_currency/cacheable_store.rb b/lib/active_currency/cacheable_store.rb index 5a6695e..459b5b2 100644 --- a/lib/active_currency/cacheable_store.rb +++ b/lib/active_currency/cacheable_store.rb @@ -7,6 +7,7 @@ module CacheableStore include AfterCommitEverywhere def get_rate(from, to, date = nil) + return super unless ActiveCurrency.configuration.cache_enabled? return super if date Rails.cache.fetch(cache_key(from, to), expires_in: 1.hour) do @@ -17,6 +18,8 @@ def get_rate(from, to, date = nil) def add_rate(from, to, rate, date = nil) super + return unless ActiveCurrency.configuration.cache_enabled? + after_commit do Rails.cache.delete(cache_key(from, to)) end diff --git a/lib/active_currency/configuration.rb b/lib/active_currency/configuration.rb index 3bc8847..6d9c905 100644 --- a/lib/active_currency/configuration.rb +++ b/lib/active_currency/configuration.rb @@ -6,10 +6,16 @@ def initialize @remote_bank = :eu_central_bank @open_exchange_rates_app_id = nil @multiplier = {} + @cache_enabled = true end attr_accessor :remote_bank, :open_exchange_rates_app_id, - :multiplier + :multiplier, + :cache_enabled + + def cache_enabled? + !!@cache_enabled + end end end