Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement configurable storage backend #242

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 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)
RailsSettings.configuration.storage.delete(cache_key)
end

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

def _all_settings
RequestCache.all_settings ||= Rails.cache.fetch(cache_key, expires_in: 1.week) do
RequestCache.all_settings ||= RailsSettings.configuration.storage.fetch(cache_key, expires_in: 1.week) do
viralpraxis marked this conversation as resolved.
Show resolved Hide resolved
vars = unscoped.select("var, value")
result = {}
vars.each { |record| result[record.var] = record.value }
Expand Down
33 changes: 33 additions & 0 deletions lib/rails-settings/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module RailsSettings
InvalidConfigurationError = Class.new(StandardError)

class Configuration
attr_reader :storage

def initialize
@storage = Rails.cache
end

def storage=(value)
unless value.is_a?(ActiveSupport::Cache::Store)
raise InvalidConfigurationError, <<~TXT
Option `storage` must be an instance of `ActiveSupport::Cache::Store` class.
TXT
end

@storage = value
end
end

class << self
def configuration
viralpraxis marked this conversation as resolved.
Show resolved Hide resolved
@configuration ||= Configuration.new
end

def configure
yield configuration
end
end
end