Skip to content

Commit

Permalink
Use rufo formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
pawurb committed Feb 22, 2024
1 parent f69eaa4 commit 44fb19f
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
source 'https://rubygems.org'
source "https://rubygems.org"

gemspec
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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
12 changes: 6 additions & 6 deletions benchmarks/Gemfile
Original file line number Diff line number Diff line change
@@ -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"
16 changes: 8 additions & 8 deletions benchmarks/main.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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-")

Expand Down
3 changes: 1 addition & 2 deletions lib/rails-brotli-cache.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require 'rails-brotli-cache/store'
require "rails-brotli-cache/store"

module RailsBrotliCache
end

30 changes: 15 additions & 15 deletions lib/rails-brotli-cache/store.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

require 'active_support/cache'
require "active_support/cache"
begin
require 'brotli'
require "brotli"
rescue LoadError
end

Expand All @@ -25,18 +25,18 @@ def self.inflate(payload)
DEFAULT_OPTIONS = {
compress_threshold: COMPRESS_THRESHOLD,
compress: true,
compressor_class: BrotliCompressor
compressor_class: BrotliCompressor,
}

attr_reader :core_store

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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 13 additions & 12 deletions rails-brotli-cache.gemspec
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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" }
Expand Down
1 change: 0 additions & 1 deletion spec/dummy/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,3 @@ group :development do
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"
end

2 changes: 1 addition & 1 deletion spec/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/dummy/config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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
Expand All @@ -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
4 changes: 2 additions & 2 deletions spec/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/config/initializers/filter_parameter_logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
8 changes: 4 additions & 4 deletions spec/rails-brotli-cache/cache_keys_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -60,7 +60,7 @@ def to_param
context "custom namespace string is not duplicated" do
let(:options) do
{
namespace: "myapp"
namespace: "myapp",
}
end

Expand All @@ -77,7 +77,7 @@ def to_param
context "custom namespace proc" do
let(:options) do
{
namespace: -> { "myapp" }
namespace: -> { "myapp" },
}
end

Expand Down
11 changes: 5 additions & 6 deletions spec/rails-brotli-cache/compatibility_spec.rb
Original file line number Diff line number Diff line change
@@ -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|
Expand Down Expand Up @@ -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)
Expand All @@ -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|
Expand Down
4 changes: 2 additions & 2 deletions spec/rails-brotli-cache/rails_store_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/rails-brotli-cache/redis_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down

0 comments on commit 44fb19f

Please sign in to comment.