From 6b7b27a72eb993985b46aa945cbe575081bf97d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Gald=C3=A1mez?= Date: Wed, 12 Mar 2025 07:49:53 -0300 Subject: [PATCH] Set memoryCache as default It was causing issues with tests when the gem was used --- README.md | 8 ++++-- .../ruber/templates/initializers/ruber.tt | 11 +++++--- lib/ruber/cache.rb | 25 ------------------- lib/ruber/configuration.rb | 6 ++--- lib/ruber/configuration/file_cache.rb | 9 +++---- .../{null_cache.rb => memory_cache.rb} | 2 +- test/ruber/authenticator_test.rb | 1 + test/ruber/configuration/file_cache_test.rb | 13 +++------- test/test_helper.rb | 1 - test/tmp/ruber_cache.yaml | 0 10 files changed, 26 insertions(+), 50 deletions(-) delete mode 100644 lib/ruber/cache.rb rename lib/ruber/configuration/{null_cache.rb => memory_cache.rb} (93%) create mode 100644 test/tmp/ruber_cache.yaml diff --git a/README.md b/README.md index 2743ff1..9d727aa 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,8 @@ end _ℹ️ If you run the `init` generator you should set the attributes in the generated initializer (`config/initializers/ruber`)_ ## Cache -Ruber uses a caching solution to improve efficiency (e.g., for caching tokens). By default, it uses a simple file cache (see below), but you can change the cache method by setting the `Ruber.cache` attribute: +Ruber uses a caching solution to improve efficiency (e.g., for caching tokens). By default, it uses a simple memory cache, +but you can change the cache method by setting the `Ruber.cache` attribute. ```ruby Ruber.cache = Redis.new @@ -52,9 +53,12 @@ Ruber.cache = Rails.cache Ruber.cache = YourCustomCache.new ``` +> Try not to use the memory cache in production, as it will be cleared on every request. + + ### File cache -File cache is the default cache option. It uses a yaml file to store the cached data. +Ruber comes with a simple File cache option. It uses a yaml file to store the cached data. ```yml --- diff --git a/lib/generators/ruber/templates/initializers/ruber.tt b/lib/generators/ruber/templates/initializers/ruber.tt index 0a07563..b8f14c7 100644 --- a/lib/generators/ruber/templates/initializers/ruber.tt +++ b/lib/generators/ruber/templates/initializers/ruber.tt @@ -4,10 +4,15 @@ Ruber.configure do |config| config.client_id = 'uber-client-id' config.client_secret = 'uber-client-secret' + # -------------------------------------------------------------------------- + # Cache + # -------------------------------------------------------------------------- + # Ruber comes with a File cache option. It uses a yaml file to store the cached data. + # Uncomment the following line to use the file cache. Make sure the path is in your + # .gitignore to avoid pushing your token to the repo. + # config.cache = Ruber::FileCache.new('tmp/ruber_cache.yaml') + # If you want to use a different cache option (like redis, memcache, etc). # It might be any object that responds to read/write/delete/clear # config.cache = Rails.cache - - # If you want to use a file cache, you can set the path here - # config.file_cache_path = 'tmp/uber_cache.yaml' end diff --git a/lib/ruber/cache.rb b/lib/ruber/cache.rb deleted file mode 100644 index e420172..0000000 --- a/lib/ruber/cache.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -module Ruber - class NullCache - def read(key) = memory_store[key] - def write(key, value, _options = {}) = memory_store[key] = value - def clear = memory_store.clear - def delete(key) = memory_store.delete(key) - def memory_store = @memory_store ||= {} - end - - class << self - def cache=(store) - unless %i[read write clear delete].all? { |method| store.respond_to?(method) } - raise ArgumentError, "cache_store must respond to read, write, clear, and delete" - end - - @cache = store - end - - def cache - @cache ||= NullCache.new - end - end -end diff --git a/lib/ruber/configuration.rb b/lib/ruber/configuration.rb index c4661a7..8edfa76 100644 --- a/lib/ruber/configuration.rb +++ b/lib/ruber/configuration.rb @@ -1,14 +1,14 @@ # frozen_string_literal: true -require_relative "configuration/null_cache" +require_relative "configuration/memory_cache" require_relative "configuration/file_cache" module Ruber class Configuration - attr_accessor :customer_id, :client_id, :client_secret, :file_cache_path + attr_accessor :customer_id, :client_id, :client_secret def cache - @cache ||= FileCache.new + @cache ||= MemoryCache.new end def cache=(store) diff --git a/lib/ruber/configuration/file_cache.rb b/lib/ruber/configuration/file_cache.rb index 9de0a59..07512e2 100644 --- a/lib/ruber/configuration/file_cache.rb +++ b/lib/ruber/configuration/file_cache.rb @@ -4,17 +4,16 @@ module Ruber class FileCache - def initialize - raise "FileCache requires a file path" unless Ruber.configuration.file_cache_path - - @store = YAML::Store.new(Ruber.configuration.file_cache_path) + def initialize(file_cache_path) + @file_cache_path = file_cache_path + @store = YAML::Store.new(file_cache_path) end def read(key) = @store.transaction { @store[key] } def write(key, value) = @store.transaction { @store[key] = value } def clear - File.delete(Ruber.configuration.file_cache_path) if File.exist?(Ruber.configuration.file_cache_path) + File.delete(@file_cache_path) if File.exist?(@file_cache_path) end def delete(key) = @store.transaction { @store.delete(key) } diff --git a/lib/ruber/configuration/null_cache.rb b/lib/ruber/configuration/memory_cache.rb similarity index 93% rename from lib/ruber/configuration/null_cache.rb rename to lib/ruber/configuration/memory_cache.rb index 90a31c5..585fe70 100644 --- a/lib/ruber/configuration/null_cache.rb +++ b/lib/ruber/configuration/memory_cache.rb @@ -2,7 +2,7 @@ module Ruber class Configuration - class NullCache + class MemoryCache def read(key) = memory_store[key] def write(key, value, _options = {}) = memory_store[key] = value def clear = memory_store.clear diff --git a/test/ruber/authenticator_test.rb b/test/ruber/authenticator_test.rb index e36d6b8..0e26b1e 100644 --- a/test/ruber/authenticator_test.rb +++ b/test/ruber/authenticator_test.rb @@ -8,6 +8,7 @@ class AuthenticatorTest < Minitest::Test include WebMock::API def setup + Ruber.cache = Ruber::FileCache.new("test/tmp/ruber_cache.yaml") Ruber.cache.clear end diff --git a/test/ruber/configuration/file_cache_test.rb b/test/ruber/configuration/file_cache_test.rb index 2c5e0b2..bafeaa0 100644 --- a/test/ruber/configuration/file_cache_test.rb +++ b/test/ruber/configuration/file_cache_test.rb @@ -7,23 +7,16 @@ module Ruber class FileCacheTest < Minitest::Test def setup - File.delete(Ruber.configuration.file_cache_path) if File.exist?(Ruber.configuration.file_cache_path) + file_cache_path = "ruber_cache.yaml" + File.delete(file_cache_path) if File.exist?(file_cache_path) - @cache = FileCache.new + @cache = FileCache.new(file_cache_path) end def teardown @cache.clear end - def test_initialize_raises_error_if_no_file_path - Ruber.configuration.stub :file_cache_path, nil do - assert_raises(RuntimeError, "FileCache requires a file path") do - FileCache.new - end - end - end - def test_write_and_read @cache.write("foo", "bar") assert_equal "bar", @cache.read("foo") diff --git a/test/test_helper.rb b/test/test_helper.rb index f8d30a1..330cfde 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -16,7 +16,6 @@ class Minitest::Test # rubocop:disable Style/ClassAndModuleChildren def before_setup super - Ruber.configuration.file_cache_path = "file_cache_test.yaml" Ruber.configuration.customer_id = "a_customer_id" Ruber.configuration.client_secret = "a_client_secret" end diff --git a/test/tmp/ruber_cache.yaml b/test/tmp/ruber_cache.yaml new file mode 100644 index 0000000..e69de29