diff --git a/Gemfile b/Gemfile index fa75df1..b4e2a20 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,3 @@ -source 'https://rubygems.org' +source "https://rubygems.org" gemspec diff --git a/Rakefile b/Rakefile index d10599f..acf4811 100644 --- a/Rakefile +++ b/Rakefile @@ -1,11 +1,11 @@ require "bundler/gem_tasks" -require 'rspec/core/rake_task' +require "rspec/core/rake_task" RSpec::Core::RakeTask.new(:spec) task default: :spec -desc 'Test all cache_stores' +desc "Test all cache_stores" task :test_all do system("TEST_RAILS_CACHE_STORE=redis_cache_store bundle exec rspec spec && TEST_RAILS_CACHE_STORE=brotli_cache_store bundle exec rspec spec && TEST_RAILS_CACHE_STORE=mem_cache_store bundle exec rspec spec && bundle exec rspec spec") end diff --git a/benchmarks/Gemfile b/benchmarks/Gemfile index c82b8ba..500c826 100644 --- a/benchmarks/Gemfile +++ b/benchmarks/Gemfile @@ -1,7 +1,7 @@ -source 'https://rubygems.org' +source "https://rubygems.org" -gem 'brotli' -gem 'rails-brotli-cache', path: "../" -gem 'redis' -gem 'dalli' -gem 'zstd-ruby' +gem "brotli" +gem "rails-brotli-cache", path: "../" +gem "redis" +gem "dalli" +gem "zstd-ruby" diff --git a/benchmarks/main.rb b/benchmarks/main.rb index ac2053e..cb1ba08 100644 --- a/benchmarks/main.rb +++ b/benchmarks/main.rb @@ -1,10 +1,10 @@ -require 'active_support' -require 'active_support/core_ext/hash' -require 'net/http' -require 'brotli' -require 'rails-brotli-cache' -require 'benchmark' -require 'zstd-ruby' +require "active_support" +require "active_support/core_ext/hash" +require "net/http" +require "brotli" +require "rails-brotli-cache" +require "benchmark" +require "zstd-ruby" class ZSTDCompressor def self.deflate(payload) @@ -28,7 +28,7 @@ def self.inflate(payload) brotli_memcached_cache = RailsBrotliCache::Store.new(memcached_cache) zstd_memcached_cache = RailsBrotliCache::Store.new(memcached_cache, compressor_class: ZSTDCompressor, prefix: "zs-") -file_cache = ActiveSupport::Cache::FileStore.new('/tmp') +file_cache = ActiveSupport::Cache::FileStore.new("/tmp") brotli_file_cache = RailsBrotliCache::Store.new(file_cache) zstd_file_cache = RailsBrotliCache::Store.new(file_cache, compressor_class: ZSTDCompressor, prefix: "zs-") diff --git a/lib/rails-brotli-cache.rb b/lib/rails-brotli-cache.rb index a64a68a..cd20cc5 100644 --- a/lib/rails-brotli-cache.rb +++ b/lib/rails-brotli-cache.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true -require 'rails-brotli-cache/store' +require "rails-brotli-cache/store" module RailsBrotliCache end - diff --git a/lib/rails-brotli-cache/store.rb b/lib/rails-brotli-cache/store.rb index 5cd2f3d..81c576b 100644 --- a/lib/rails-brotli-cache/store.rb +++ b/lib/rails-brotli-cache/store.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require 'active_support/cache' +require "active_support/cache" begin - require 'brotli' + require "brotli" rescue LoadError end @@ -25,7 +25,7 @@ def self.inflate(payload) DEFAULT_OPTIONS = { compress_threshold: COMPRESS_THRESHOLD, compress: true, - compressor_class: BrotliCompressor + compressor_class: BrotliCompressor, } attr_reader :core_store @@ -33,10 +33,10 @@ def self.inflate(payload) def initialize(core_store, options = {}) @core_store = core_store @prefix = if options.key?(:prefix) - options.fetch(:prefix) - else - "br-" - end + options.fetch(:prefix) + else + "br-" + end @init_options = options.reverse_merge(DEFAULT_OPTIONS) end @@ -87,7 +87,7 @@ def write_multi(hash, options = nil) new_hash = hash.map do |key, val| [ expanded_cache_key(key), - compressed(val, options) + compressed(val, options), ] end @@ -112,12 +112,12 @@ def fetch_multi(*names) expanded_names = names.map { |name| expanded_cache_key(name) } options = options.reverse_merge(@init_options) - reads = core_store.send(:read_multi_entries, expanded_names, **options) + reads = core_store.send(:read_multi_entries, expanded_names, **options) reads.map do |key, val| [source_cache_key(key), uncompressed(val, options)] end.to_h - writes = {} + writes = {} ordered = names.index_with do |name| reads.fetch(name) { writes[name] = yield(name) } end @@ -175,11 +175,11 @@ def uncompressed(payload, options) return payload if payload.is_a?(Integer) serialized = if payload.start_with?(MARK_BR_COMPRESSED) - compressor = options.fetch(:compressor_class) - compressor.inflate(payload.byteslice(1..-1)) - else - payload - end + compressor = options.fetch(:compressor_class) + compressor.inflate(payload.byteslice(1..-1)) + else + payload + end Marshal.load(serialized) end diff --git a/rails-brotli-cache.gemspec b/rails-brotli-cache.gemspec index 94c9171..7c25191 100644 --- a/rails-brotli-cache.gemspec +++ b/rails-brotli-cache.gemspec @@ -1,20 +1,20 @@ # -*- encoding: utf-8 -*- -lib = File.expand_path('../lib', __FILE__) +lib = File.expand_path("../lib", __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'rails-brotli-cache/version' +require "rails-brotli-cache/version" Gem::Specification.new do |gem| - gem.name = "rails-brotli-cache" - gem.version = RailsBrotliCache::VERSION - gem.authors = ["pawurb"] - gem.email = ["contact@pawelurbanek.com"] - gem.summary = %q{ Drop-in enhancement for Rails cache, offering better performance and compression with Brotli algorithm. } - gem.description = %q{ This gem reduces storage needed for Rails cache by using Brotli compression, which can produce outputs smaller by ~20% and offers better performance than Gzip. } - gem.homepage = "https://github.com/pawurb/rails-brotli-cache" - gem.files = `git ls-files`.split("\n") - gem.test_files = gem.files.grep(%r{^(spec)/}) + gem.name = "rails-brotli-cache" + gem.version = RailsBrotliCache::VERSION + gem.authors = ["pawurb"] + gem.email = ["contact@pawelurbanek.com"] + gem.summary = %q{ Drop-in enhancement for Rails cache, offering better performance and compression with Brotli algorithm. } + gem.description = %q{ This gem reduces storage needed for Rails cache by using Brotli compression, which can produce outputs smaller by ~20% and offers better performance than Gzip. } + gem.homepage = "https://github.com/pawurb/rails-brotli-cache" + gem.files = `git ls-files`.split("\n") + gem.test_files = gem.files.grep(%r{^(spec)/}) gem.require_paths = ["lib"] - gem.license = "MIT" + gem.license = "MIT" gem.add_dependency "activesupport" gem.add_development_dependency "brotli" gem.add_development_dependency "rspec" @@ -24,6 +24,7 @@ Gem::Specification.new do |gem| gem.add_development_dependency "redis" gem.add_development_dependency "dalli" gem.add_development_dependency "byebug" + gem.add_development_dependency "rufo" if gem.respond_to?(:metadata=) gem.metadata = { "rubygems_mfa_required" => "true" } diff --git a/spec/dummy/Gemfile b/spec/dummy/Gemfile index 6a2a2de..21ac5ef 100644 --- a/spec/dummy/Gemfile +++ b/spec/dummy/Gemfile @@ -42,4 +42,3 @@ group :development do # Speed up commands on slow machines / big apps [https://github.com/rails/spring] # gem "spring" end - diff --git a/spec/dummy/config/application.rb b/spec/dummy/config/application.rb index f95ce30..4ece80a 100644 --- a/spec/dummy/config/application.rb +++ b/spec/dummy/config/application.rb @@ -22,7 +22,7 @@ module Dummy class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. - if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7') + if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.7") config.load_defaults 7.0 else config.load_defaults 6.0 diff --git a/spec/dummy/config/environments/production.rb b/spec/dummy/config/environments/production.rb index 1ab34ad..9e6f7d7 100644 --- a/spec/dummy/config/environments/production.rb +++ b/spec/dummy/config/environments/production.rb @@ -13,7 +13,7 @@ config.eager_load = true # Full error reports are disabled and caching is turned on. - config.consider_all_requests_local = false + config.consider_all_requests_local = false config.action_controller.perform_caching = true # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] @@ -45,7 +45,7 @@ config.log_level = :info # Prepend all log lines with the following tags. - config.log_tags = [ :request_id ] + config.log_tags = [:request_id] # Use a different cache store in production. # config.cache_store = :mem_cache_store @@ -65,8 +65,8 @@ # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new "app-name") if ENV["RAILS_LOG_TO_STDOUT"].present? - logger = ActiveSupport::Logger.new(STDOUT) + logger = ActiveSupport::Logger.new(STDOUT) logger.formatter = config.log_formatter - config.logger = ActiveSupport::TaggedLogging.new(logger) + config.logger = ActiveSupport::TaggedLogging.new(logger) end end diff --git a/spec/dummy/config/environments/test.rb b/spec/dummy/config/environments/test.rb index eb2f171..7779fc6 100644 --- a/spec/dummy/config/environments/test.rb +++ b/spec/dummy/config/environments/test.rb @@ -19,11 +19,11 @@ # Configure public file server for tests with Cache-Control for performance. config.public_file_server.enabled = true config.public_file_server.headers = { - "Cache-Control" => "public, max-age=#{1.hour.to_i}" + "Cache-Control" => "public, max-age=#{1.hour.to_i}", } # Show full error reports and disable caching. - config.consider_all_requests_local = true + config.consider_all_requests_local = true config.action_controller.perform_caching = false config.cache_store = :null_store diff --git a/spec/dummy/config/initializers/filter_parameter_logging.rb b/spec/dummy/config/initializers/filter_parameter_logging.rb index adc6568..ec95807 100644 --- a/spec/dummy/config/initializers/filter_parameter_logging.rb +++ b/spec/dummy/config/initializers/filter_parameter_logging.rb @@ -4,5 +4,5 @@ # sensitive information. See the ActiveSupport::ParameterFilter documentation for supported # notations and behaviors. Rails.application.config.filter_parameters += [ - :passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn + :passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn, ] diff --git a/spec/rails-brotli-cache/cache_keys_spec.rb b/spec/rails-brotli-cache/cache_keys_spec.rb index 6e3f3fc..0fc7b69 100644 --- a/spec/rails-brotli-cache/cache_keys_spec.rb +++ b/spec/rails-brotli-cache/cache_keys_spec.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require 'spec_helper' +require "spec_helper" -return unless ENV['TEST_RAILS_CACHE_STORE'] == 'redis_cache_store' +return unless ENV["TEST_RAILS_CACHE_STORE"] == "redis_cache_store" describe RailsBrotliCache do class Post @@ -60,7 +60,7 @@ def to_param context "custom namespace string is not duplicated" do let(:options) do { - namespace: "myapp" + namespace: "myapp", } end @@ -77,7 +77,7 @@ def to_param context "custom namespace proc" do let(:options) do { - namespace: -> { "myapp" } + namespace: -> { "myapp" }, } end diff --git a/spec/rails-brotli-cache/compatibility_spec.rb b/spec/rails-brotli-cache/compatibility_spec.rb index cc19617..db4e94d 100644 --- a/spec/rails-brotli-cache/compatibility_spec.rb +++ b/spec/rails-brotli-cache/compatibility_spec.rb @@ -1,15 +1,14 @@ # frozen_string_literal: true -require 'spec_helper' +require "spec_helper" describe RailsBrotliCache do - CACHE_STORE_TYPES = [ [ActiveSupport::Cache::MemoryStore.new, ActiveSupport::Cache::MemoryStore.new], [ActiveSupport::Cache::RedisCacheStore.new, ActiveSupport::Cache::RedisCacheStore.new], [ActiveSupport::Cache::MemCacheStore.new, ActiveSupport::Cache::MemCacheStore.new], - [ActiveSupport::Cache::FileStore.new('./tmp'), ActiveSupport::Cache::FileStore.new('./tmp')], - [ActiveSupport::Cache::NullStore.new, ActiveSupport::Cache::NullStore.new] + [ActiveSupport::Cache::FileStore.new("./tmp"), ActiveSupport::Cache::FileStore.new("./tmp")], + [ActiveSupport::Cache::NullStore.new, ActiveSupport::Cache::NullStore.new], ] CACHE_STORE_TYPES.each do |cache_store_types| @@ -72,7 +71,7 @@ it "for #write_multi and #read_multi" do values = { "key_1" => big_enough_to_compress_value, - "key_2" => "val_2" + "key_2" => "val_2", } brotli_store.write_multi(values) @@ -87,7 +86,7 @@ it "for #fetch_multi" do values = { "key_1" => big_enough_to_compress_value, - "key_2" => "val_2" + "key_2" => "val_2", } brotli_store.fetch_multi("key_1", "key_2") do |key| diff --git a/spec/rails-brotli-cache/rails_store_spec.rb b/spec/rails-brotli-cache/rails_store_spec.rb index 0ca602d..ac5c590 100644 --- a/spec/rails-brotli-cache/rails_store_spec.rb +++ b/spec/rails-brotli-cache/rails_store_spec.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require 'spec_helper' +require "spec_helper" -return unless ENV['TEST_RAILS_CACHE_STORE'] == 'brotli_cache_store' +return unless ENV["TEST_RAILS_CACHE_STORE"] == "brotli_cache_store" describe RailsBrotliCache do subject(:cache_store) do diff --git a/spec/rails-brotli-cache/redis_spec.rb b/spec/rails-brotli-cache/redis_spec.rb index e695591..71ad3c4 100644 --- a/spec/rails-brotli-cache/redis_spec.rb +++ b/spec/rails-brotli-cache/redis_spec.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -require 'spec_helper' +require "spec_helper" -return unless ENV['TEST_RAILS_CACHE_STORE'] == 'redis_cache_store' +return unless ENV["TEST_RAILS_CACHE_STORE"] == "redis_cache_store" describe RailsBrotliCache do let(:options) do @@ -24,7 +24,7 @@ end let(:json) do - File.read('spec/fixtures/sample.json') + File.read("spec/fixtures/sample.json") end it "applies more efficient brotli compression" do diff --git a/spec/rails-brotli-cache/store_spec.rb b/spec/rails-brotli-cache/store_spec.rb index f6d2583..f043314 100644 --- a/spec/rails-brotli-cache/store_spec.rb +++ b/spec/rails-brotli-cache/store_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'spec_helper' +require "spec_helper" describe RailsBrotliCache do subject(:cache_store) do @@ -89,7 +89,7 @@ def to_param it "works" do values = { "key_1" => big_enough_to_compress_value, - "key_2" => 123 + "key_2" => 123, } cache_store.write_multi(values, expires_in: 5.seconds) @@ -107,8 +107,8 @@ def to_param let(:keys) { %w[key_1 key_2] } let(:response) do { - 'key_1' => big_enough_to_compress_value + 'key_1', - 'key_2' => big_enough_to_compress_value + 'key_2' + "key_1" => big_enough_to_compress_value + "key_1", + "key_2" => big_enough_to_compress_value + "key_2", } end @@ -120,7 +120,7 @@ def to_param end).to eq response end - context 'with a complex object that responds to #cache_key' do + context "with a complex object that responds to #cache_key" do subject do cache_store.fetch_multi(*keys) do |key| big_enough_to_compress_value + key.id @@ -129,14 +129,14 @@ def to_param let(:keys) do [ - OpenStruct.new(cache_key: 'key_1', id: '12345'), - OpenStruct.new(cache_key: 'key_2', id: '54321') + OpenStruct.new(cache_key: "key_1", id: "12345"), + OpenStruct.new(cache_key: "key_2", id: "54321"), ] end let(:response) do { - keys[0] => big_enough_to_compress_value + '12345', - keys[1] => big_enough_to_compress_value + '54321' + keys[0] => big_enough_to_compress_value + "12345", + keys[1] => big_enough_to_compress_value + "54321", } end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bba5522..337835a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,28 +1,27 @@ -require 'rubygems' -require 'bundler/setup' +require "rubygems" +require "bundler/setup" require "active_support" require "active_support/core_ext/hash" -require 'redis' +require "redis" -require_relative '../lib/rails-brotli-cache' +require_relative "../lib/rails-brotli-cache" $redis = Redis.new -$test_rails_cache_store = if ENV['TEST_RAILS_CACHE_STORE'] == 'redis_cache_store' - ActiveSupport::Cache::RedisCacheStore.new(redis: $redis) -elsif ENV['TEST_RAILS_CACHE_STORE'] == 'brotli_cache_store' - RailsBrotliCache::Store.new(ActiveSupport::Cache::MemoryStore.new) -elsif ENV['TEST_RAILS_CACHE_STORE'] == 'memcache_cache_store' - ActiveSupport::Cache::ActiveSupport::Cache::MemCacheStore.new -else - ActiveSupport::Cache::MemoryStore.new -end +$test_rails_cache_store = if ENV["TEST_RAILS_CACHE_STORE"] == "redis_cache_store" + ActiveSupport::Cache::RedisCacheStore.new(redis: $redis) + elsif ENV["TEST_RAILS_CACHE_STORE"] == "brotli_cache_store" + RailsBrotliCache::Store.new(ActiveSupport::Cache::MemoryStore.new) + elsif ENV["TEST_RAILS_CACHE_STORE"] == "memcache_cache_store" + ActiveSupport::Cache::ActiveSupport::Cache::MemCacheStore.new + else + ActiveSupport::Cache::MemoryStore.new + end -require_relative '../spec/dummy/config/environment' -ENV['RAILS_ROOT'] ||= "#{File.dirname(__FILE__)}../../../spec/dummy" +require_relative "../spec/dummy/config/environment" +ENV["RAILS_ROOT"] ||= "#{File.dirname(__FILE__)}../../../spec/dummy" RSpec.configure do |config| config.before(:each) do Rails.cache.clear end end -