-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from 0exp/feature/cache
[utilities/cache] Cache utlitiy
- Loading branch information
Showing
61 changed files
with
2,133 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ | |
.ruby-version | ||
gemfiles/*.lock | ||
*.gem | ||
/spec/support/artifacts/* | ||
TODOS.md |
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 |
---|---|---|
|
@@ -13,3 +13,6 @@ AllCops: | |
- Gemfile | ||
- Rakefile | ||
- blast_wave.gemspec | ||
|
||
Layout/MultilineOperationIndentation: | ||
Enabled: false |
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 |
---|---|---|
|
@@ -3,6 +3,6 @@ | |
|
||
require 'bundler/setup' | ||
require 'blast_wave' | ||
|
||
require 'pry' | ||
|
||
Pry.start |
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,119 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require 'pathname' | ||
require 'optparse' | ||
|
||
module BlastWaveSpecRunner | ||
extend self | ||
|
||
def expand_gemfile_path(gemfile_name) | ||
File.expand_path( | ||
File.join('..', '..', 'gemfiles', gemfile_name), | ||
Pathname.new(__FILE__).realpath | ||
) | ||
end | ||
|
||
GEMFILES = { | ||
redis: expand_gemfile_path('redis.gemfile'), | ||
redis_store: expand_gemfile_path('redis_store.gemfile'), | ||
dalli: expand_gemfile_path('dalli.gemfile'), | ||
active_support_with_redis: expand_gemfile_path('active_support_with_redis.gemfile'), | ||
active_support: expand_gemfile_path('active_support.gemfile') | ||
}.freeze | ||
|
||
# rubocop:disable Metrics/MethodLength, Metrics/BlockLength | ||
def run! | ||
OptionParser.new do |opts| | ||
opts.banner = 'Usage: bin/rspec [options]' | ||
|
||
opts.on( | ||
'--test-redis-cache', | ||
'Run Redis cache specs' | ||
) { run_redis_cache_specs! } | ||
|
||
opts.on( | ||
'--test-redis-store-cache', | ||
'Run Redis::Store cache specs' | ||
) { run_redis_store_cache_specs! } | ||
|
||
opts.on( | ||
'--test-dalli-cache', | ||
'Run Dalli cache specs' | ||
) { run_dalli_cache_specs! } | ||
|
||
opts.on( | ||
'--test-as-redis-cache-store-cache', | ||
'Run ActiveSupport::Cache::RedisCacheStore cache specs' | ||
) { run_as_redis_cache_store_cache_specs! } | ||
|
||
opts.on( | ||
'--test-as-file-store-cache', | ||
'Run ActiveSupport::Cache::FileStore cache specs' | ||
) { run_as_file_store_cache_specs! } | ||
|
||
opts.on( | ||
'--test-as-memory-store-cache', | ||
'Run ActiveSupport::Cache::MemoryStore cache specs' | ||
) { run_as_memory_store_cache_specs! } | ||
|
||
opts.on( | ||
'-h', '--help', | ||
'Show this message' | ||
) { puts opts } | ||
end.parse! | ||
end | ||
# rubocop:enable Metrics/MethodLength, Metrics/BlockLength | ||
|
||
private | ||
|
||
def run_redis_cache_specs! | ||
ENV['TEST_REDIS_CACHE'] = 'true' | ||
ENV['BUNDLE_GEMFILE'] = GEMFILES[:redis] | ||
|
||
run_tests! | ||
end | ||
|
||
def run_redis_store_cache_specs! | ||
ENV['TEST_REDIS_STORE_CACHE'] = 'true' | ||
ENV['BUNDLE_GEMFILE'] = GEMFILES[:redis_store] | ||
|
||
run_tests! | ||
end | ||
|
||
def run_dalli_cache_specs! | ||
ENV['TEST_DALLI_CACHE'] = 'true' | ||
ENV['BUNDLE_GEMFILE'] = GEMFILES[:dalli] | ||
|
||
run_tests! | ||
end | ||
|
||
def run_as_redis_cache_store_cache_specs! | ||
ENV['TEST_AS_REDIS_CACHE_STORE_CACHE'] = 'true' | ||
ENV['BUNDLE_GEMFILE'] = GEMFILES[:active_support_with_redis] | ||
|
||
run_tests! | ||
end | ||
|
||
def run_as_file_store_cache_specs! | ||
ENV['TEST_AS_FILE_STORE_CACHE'] = 'true' | ||
ENV['BUNDLE_GEMFILE'] = GEMFILES[:active_support] | ||
|
||
run_tests! | ||
end | ||
|
||
def run_as_memory_store_cache_specs! | ||
ENV['TEST_AS_MEMORY_STORE_CACHE'] = 'true' | ||
ENV['BUNDLE_GEMFILE'] = GEMFILES[:active_support] | ||
|
||
run_tests! | ||
end | ||
|
||
def run_tests! | ||
require 'rubygems' | ||
require 'bundler/setup' | ||
load Gem.bin_path('rspec-core', 'rspec') | ||
end | ||
end | ||
|
||
BlastWaveSpecRunner.run! |
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,7 @@ | ||
# frozen_string_literal: true | ||
|
||
source 'https://rubygems.org' | ||
|
||
gem 'activesupport', '~> 5.2' | ||
|
||
gemspec path: '..' |
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,8 @@ | ||
# frozen_string_literal: true | ||
|
||
source 'https://rubygems.org' | ||
|
||
gem 'redis', '~> 4.0' | ||
gem 'activesupport', '~> 5.2' | ||
|
||
gemspec path: '..' |
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,7 @@ | ||
# frozen_string_literal: true | ||
|
||
source 'https://rubygems.org' | ||
|
||
gem 'dalli', '~> 2.7' | ||
|
||
gemspec path: '..' |
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,7 @@ | ||
# frozen_string_literal: true | ||
|
||
source 'https://rubygems.org' | ||
|
||
gem 'redis', '~> 4.0' | ||
|
||
gemspec path: '..' |
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,7 @@ | ||
# frozen_string_literal: true | ||
|
||
source 'https://rubygems.org' | ||
|
||
gem 'redis-store', '~> 1.5' | ||
|
||
gemspec path: '..' |
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,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack::BlastWave | ||
# @since 0.1.0 | ||
Error = Class.new(StandardError) | ||
|
||
# @since 0.1.0 | ||
CacheUtilityError = Class.new(Error) | ||
# @since 0.1.0 | ||
UnsupportedCacheDriverError = Class.new(CacheUtilityError) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# @api private | ||
# @since 0.1.0 | ||
module BlastWave::Utilities | ||
require_relative 'utilities/cache' | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# @api private | ||
# @since 0.1.0 | ||
class BlastWave::Utilities::Cache | ||
require_relative 'cache/adapters' | ||
|
||
# @since 0.1.0 | ||
extend Forwardable | ||
|
||
class << self | ||
# @param driver [Object] | ||
# @return [BlastWave::Utilities::Cache] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def build(driver) | ||
new(Adapters.build(driver)) | ||
end | ||
end | ||
|
||
# @since 0.1.0 | ||
def_delegators :adapter, :read, :write, :delete, :increment, :decrement, :re_expire, :clear | ||
|
||
# @param adapter [BlastWave::Utilities::Cache::Adapters::Basic] | ||
# @return [void] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def initialize(adapter) | ||
@adapter = adapter | ||
end | ||
|
||
private | ||
|
||
# @return [BlastWave::Utilities::Cache::Adapters::Basic] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
attr_reader :adapter | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
# @api private | ||
# @since 0.1.0 | ||
module BlastWave::Utilities::Cache::Adapters | ||
require_relative 'adapters/basic' | ||
require_relative 'adapters/delegator' | ||
require_relative 'adapters/dalli' | ||
require_relative 'adapters/redis' | ||
require_relative 'adapters/redis_store' | ||
require_relative 'adapters/active_support_naive_store' | ||
require_relative 'adapters/active_support_file_store' | ||
require_relative 'adapters/active_support_redis_cache_store' | ||
require_relative 'adapters/active_support_memory_store' | ||
|
||
class << self | ||
# @param driver [Object] | ||
# @return [BlastWave::Utilities::Cache::Adapters::Basic] | ||
# | ||
# @raise [BlastWave::UnsupportedCacheDriverError] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
# rubocop:disable Metrics/LineLength | ||
def build(driver) | ||
case | ||
when RedisStore.supported_driver?(driver) then RedisStore.new(driver) | ||
when Redis.supported_driver?(driver) then Redis.new(driver) | ||
when Dalli.supported_driver?(driver) then Dalli.new(driver) | ||
when ActiveSupportRedisCacheStore.supported_driver?(driver) then ActiveSupportRedisCacheStore.new(driver) | ||
when ActiveSupportMemoryStore.supported_driver?(driver) then ActiveSupportMemoryStore.new(driver) | ||
when ActiveSupportFileStore.supported_driver?(driver) then ActiveSupportFileStore.new(driver) | ||
when Delegator.supported_driver?(driver) then Delegator.new(driver) | ||
else | ||
raise BlastWave::UnsupportedCacheDriverError | ||
end | ||
end | ||
# rubocop:enable Metrics/LineLength | ||
end | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
lib/blast_wave/utilities/cache/adapters/active_support_file_store.rb
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,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
module BlastWave::Utilities::Cache::Adapters | ||
# @api private | ||
# @since 0.1.0 | ||
class ActiveSupportFileStore < ActiveSupportNaiveStore | ||
require_relative 'active_support_file_store/fetching' | ||
require_relative 'active_support_file_store/operation' | ||
require_relative 'active_support_file_store/increment' | ||
require_relative 'active_support_file_store/decrement' | ||
require_relative 'active_support_file_store/re_expire' | ||
|
||
class << self | ||
# @param driver [Object] | ||
# @return [Boolean] | ||
# | ||
# @api private | ||
# @since 0.1.0 | ||
def supported_driver?(driver) | ||
defined?(::ActiveSupport::Cache::FileStore) && | ||
driver.is_a?(::ActiveSupport::Cache::FileStore) | ||
end | ||
end | ||
end | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
lib/blast_wave/utilities/cache/adapters/active_support_file_store/decrement.rb
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,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
module BlastWave::Utilities::Cache::Adapters | ||
# @api private | ||
# @since 0.1.0 | ||
class ActiveSupportFileStore::Decrement < ActiveSupportNaiveStore::Decrement | ||
# @since 0.1.0 | ||
include ActiveSupportFileStore::Fetching | ||
end | ||
end | ||
end |
Oops, something went wrong.