Skip to content

Commit

Permalink
Implement configurable storage backend (#242)
Browse files Browse the repository at this point in the history
* Implement configurable storage backend

* Refactor `Configuration` class and its usage

* Update tests

* Fix flake test
  • Loading branch information
viralpraxis authored Oct 16, 2023
1 parent fce56c6 commit 95f538f
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/rails-settings-cached.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require_relative "rails-settings/fields/string"

require_relative "rails-settings/base"
require_relative "rails-settings/configuration"
require_relative "rails-settings/request_cache"
require_relative "rails-settings/middleware"
require_relative "rails-settings/railtie"
Expand Down
8 changes: 6 additions & 2 deletions lib/rails-settings/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def clear_cache
class << self
def clear_cache
RequestCache.reset
Rails.cache.delete(cache_key)
cache_storage.delete(cache_key)
end

def field(key, **opts)
Expand Down Expand Up @@ -127,13 +127,17 @@ def rails_initialized?
end

def _all_settings
RequestCache.all_settings ||= Rails.cache.fetch(cache_key, expires_in: 1.week) do
RequestCache.all_settings ||= cache_storage.fetch(cache_key, expires_in: 1.week) do
vars = unscoped.select("var, value")
result = {}
vars.each { |record| result[record.var] = record.value }
result.with_indifferent_access
end
end

def cache_storage
RailsSettings.config.cache_storage
end
end
end
end
23 changes: 23 additions & 0 deletions lib/rails-settings/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module RailsSettings
class Configuration
# Caching storage backend.
# Default: `Rails.cache`
attr_accessor :cache_storage
end

class << self
def config
return @config if defined?(@config)

@config = Configuration.new
@config.cache_storage = Rails.cache
@config
end

def configure(&block)
config.instance_exec(&block)
end
end
end
23 changes: 23 additions & 0 deletions test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,27 @@ class NewSetting < RailsSettings::Base
assert_equal %(SELECT "settings".* FROM "settings" WHERE (var like 'readonly_%')), Setting.by_prefix("readonly_").to_sql
assert_equal "foo", Setting.by_prefix("readonly_").foo
end

class CustomCacheStorageTest < ActiveSupport::TestCase
setup do
RailsSettings.configure do
self.cache_storage = ActiveSupport::Cache.lookup_store(:dummy_store)
end
end

teardown do
RailsSettings.configure do
self.cache_storage = Rails.cache
end
end

test "uses expected cached storage" do
Setting.user_limits = 42

assert_equal Setting.user_limits, 42
assert RailsSettings.config.cache_storage.data.keys.any? do |key|
key.start_with?("rails-settings-cached")
end
end
end
end
13 changes: 13 additions & 0 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require "test_helper"

class ConfigurationTest < ActiveSupport::TestCase
test "configuration" do
RailsSettings.configure do
self.cache_storage = ActiveSupport::Cache.lookup_store(:dummy_store)
end

assert RailsSettings.config.cache_storage.instance_of? ActiveSupport::Cache::DummyStore
end
end
27 changes: 27 additions & 0 deletions test/dummy/lib/active_support/cache/dummy_store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module ActiveSupport
module Cache
class DummyStore < Store
attr_reader :data

def initialize(options = {})
super(options)

@data = {}
end

def read_entry(key, **options)
data[key]
end

def write_entry(key, value, **options)
data[key] = value
end

def delete_entry(key, **options)
data.delete(key)
end
end
end
end

0 comments on commit 95f538f

Please sign in to comment.