-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Cache api_keys Currently have request to an internal service requires at least one DB hit. For some services this is noise, for others it's the only reason they have a databse. In a few cases, API key look up is what the database is spending most of its time on. Since this data is effectively immutable it's a great candidate for caching. All apps would benefit from this. This caches API keys in memory using a bounded LRU cache. * Run Cache injector at define time * Allow configuration via ENV * Add new configuration values for api key cache * migrate cache injector to a cache wrapper * Use new cache key wrapper for api client wrapper * Refactor api client access for configurable cache * Update generators to include the new config values * Test cache and db paths * update docs * Speling * Disable clear when cache is disabled * An RC * More docs * remove deprecated classes and upgrade mechanism (#94) Co-authored-by: David Copeland <[email protected]>
- Loading branch information
1 parent
7542917
commit f07a1af
Showing
16 changed files
with
247 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'lru_redux' | ||
|
||
module Stitches::ApiClientAccessWrapper | ||
|
||
def self.fetch_for_key(key) | ||
if cache_enabled | ||
fetch_for_key_from_cache(key) | ||
else | ||
fetch_for_key_from_db(key) | ||
end | ||
end | ||
|
||
def self.fetch_for_key_from_cache(key) | ||
api_key_cache.getset(key) do | ||
fetch_for_key_from_db(key) | ||
end | ||
end | ||
|
||
def self.fetch_for_key_from_db(key) | ||
if ::ApiClient.column_names.include?("enabled") | ||
::ApiClient.find_by(key: key, enabled: true) | ||
else | ||
ActiveSupport::Deprecation.warn('api_keys is missing "enabled" column. Run "rails g stitches:add_enabled_to_api_clients"') | ||
::ApiClient.find_by(key: key) | ||
end | ||
end | ||
|
||
def self.clear_api_cache | ||
api_key_cache.clear if cache_enabled | ||
end | ||
|
||
def self.api_key_cache | ||
@api_key_cache ||= LruRedux::TTL::ThreadSafeCache.new( | ||
Stitches.configuration.max_cache_size, | ||
Stitches.configuration.max_cache_ttl, | ||
) | ||
end | ||
|
||
def self.cache_enabled | ||
Stitches.configuration.max_cache_ttl.positive? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
require 'stitches/api_key' | ||
require 'stitches/valid_mime_type' | ||
require 'stitches/api_client_access_wrapper' | ||
|
||
module Stitches | ||
class Railtie < Rails::Railtie | ||
config.app_middleware.use Stitches::ApiKey | ||
config.app_middleware.use Stitches::ValidMimeType | ||
|
||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module Stitches | ||
VERSION = "3.8.3" | ||
VERSION = '4.0.0' | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'spec_helper.rb' | ||
|
||
module MyApp | ||
class Application | ||
end | ||
end | ||
|
||
unless defined? ApiClient | ||
class ApiClient | ||
def self.column_names | ||
["enabled"] | ||
end | ||
end | ||
end | ||
|
||
describe Stitches::ApiClientAccessWrapper do | ||
let(:api_client) { | ||
double(ApiClient, id: 42) | ||
} | ||
before do | ||
Stitches.configuration.reset_to_defaults! | ||
end | ||
describe '#fetch_by_key' do | ||
context "cache is disabled" do | ||
before do | ||
expect(ApiClient).to receive(:find_by).and_return(api_client).twice | ||
end | ||
|
||
it "fetchs object from db twice" do | ||
expect(described_class.fetch_for_key("123").id).to eq(42) | ||
expect(described_class.fetch_for_key("123").id).to eq(42) | ||
end | ||
end | ||
|
||
context "cache is configured" do | ||
before do | ||
Stitches.configure do |config| | ||
config.max_cache_ttl = 5 | ||
config.max_cache_size = 10 | ||
end | ||
|
||
expect(ApiClient).to receive(:find_by).and_return(api_client).once | ||
end | ||
|
||
it "fetchs object from cache" do | ||
expect(described_class.fetch_for_key("123").id).to eq(42) | ||
# This should hit the cache | ||
expect(described_class.fetch_for_key("123").id).to eq(42) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.