Skip to content

Commit

Permalink
Merge pull request #42 from Shopify/rm-rename-gem
Browse files Browse the repository at this point in the history
Rename cacheable to response_bank
  • Loading branch information
rafaelfranca authored Jan 27, 2020
2 parents 57c8241 + 7159740 commit fe582f1
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 106 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
source "https://rubygems.org"

# Specify your gem's dependencies in cacheable.gemspec
# Specify your gem's dependencies in response_bank.gemspec
gemspec

gem 'rails', '~> 6.0.0'
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
cacheable (2.0.0)
response_bank (1.0.0)
msgpack
useragent

Expand Down Expand Up @@ -156,11 +156,11 @@ PLATFORMS
ruby

DEPENDENCIES
cacheable!
minitest (>= 5.13.0)
mocha (>= 1.10.0)
rails (~> 6.0.0)
rake
response_bank!
rubocop (= 0.78.0)
tzinfo-data (>= 1.2019.3)

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Cacheable [![Build Status](https://secure.travis-ci.org/Shopify/cacheable.png)](http://travis-ci.org/Shopify/cacheable)
# ResponseBank [![Build Status](https://secure.travis-ci.org/Shopify/response_bank.png)](http://travis-ci.org/Shopify/response_bank)

### Features

Expand All @@ -19,7 +19,7 @@ This gem supports the following versions of Ruby and Rails:
1. include the gem in your Gemfile

```ruby
gem 'cacheable'
gem 'response_bank'
```

2. use `#response_cache` method to any desired controller's action
Expand Down Expand Up @@ -83,9 +83,9 @@ class PostsController < ApplicationController
def set_shop
# @shop = ...
end
end
end
```

### License

Cacheable is released under the [MIT License](LICENSE.txt).
ResponseBank is released under the [MIT License](LICENSE.txt).
4 changes: 0 additions & 4 deletions lib/cacheable/version.rb

This file was deleted.

12 changes: 6 additions & 6 deletions lib/cacheable.rb → lib/response_bank.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# frozen_string_literal: true
require 'cacheable/middleware'
require 'cacheable/railtie' if defined?(Rails)
require 'cacheable/response_cache_handler'
require 'response_bank/middleware'
require 'response_bank/railtie' if defined?(Rails)
require 'response_bank/response_cache_handler'
require 'msgpack'

module Cacheable
module ResponseBank
class << self
attr_accessor :cache_store
attr_writer :logger

def log(message)
@logger.info("[Cacheable] #{message}")
@logger.info("[ResponseBank] #{message}")
end

def acquire_lock(_cache_key)
raise NotImplementedError, "Override Cacheable.acquire_lock in an initializer."
raise NotImplementedError, "Override ResponseBank.acquire_lock in an initializer."
end

def write_to_cache(_key)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true
module Cacheable
module ResponseBank
module Controller
private

# Only get? and head? requests should be cacheable
# Only get? and head? requests should be cached.
def cacheable_request?
(request.get? || request.head?) && (request.params[:cache] != 'false')
end
Expand Down Expand Up @@ -35,13 +35,13 @@ def response_cache(key_data = nil, version_data = nil, &block)
cacheable_req = cacheable_request?

unless cache_configured? && cacheable_req
Cacheable.log("Uncacheable request. cache_configured='#{!!cache_configured?}'" \
ResponseBank.log("Uncacheable request. cache_configured='#{!!cache_configured?}'" \
" cacheable_request='#{cacheable_req}' params_cache='#{request.params[:cache] != 'false'}'")
response.headers['Cache-Control'] = 'no-cache, no-store' unless cacheable_req
return yield
end

handler = Cacheable::ResponseCacheHandler.new(
handler = ResponseBank::ResponseCacheHandler.new(
key_data: key_data || cache_key_data,
version_data: version_data || cache_version_data,
env: request.env,
Expand Down
10 changes: 5 additions & 5 deletions lib/cacheable/middleware.rb → lib/response_bank/middleware.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
require 'useragent'

module Cacheable
module ResponseBank
class Middleware
REQUESTED_WITH = "HTTP_X_REQUESTED_WITH"
ACCEPT = "HTTP_ACCEPT"
Expand Down Expand Up @@ -36,18 +36,18 @@ def call(env)
body.each { |part| body_string << part }
end

body_gz = Cacheable.compress(body_string)
body_gz = ResponseBank.compress(body_string)

# Store result
cache_data = [status, headers['Content-Type'], body_gz, timestamp]
cache_data << headers['Location'] if status == 301

Cacheable.write_to_cache(env['cacheable.key']) do
ResponseBank.write_to_cache(env['cacheable.key']) do
payload = MessagePack.dump(cache_data)
Cacheable.cache_store.write(env['cacheable.key'], payload, raw: true)
ResponseBank.cache_store.write(env['cacheable.key'], payload, raw: true)

if env['cacheable.unversioned-key']
Cacheable.cache_store.write(env['cacheable.unversioned-key'], payload, raw: true)
ResponseBank.cache_store.write(env['cacheable.unversioned-key'], payload, raw: true)
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true
module Cacheable
module ResponseBank
module ModelExtensions
def self.included(base)
super
Expand Down
12 changes: 6 additions & 6 deletions lib/cacheable/railtie.rb → lib/response_bank/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# frozen_string_literal: true
require 'rails'
require 'cacheable/controller'
require 'cacheable/model_extensions'
require 'response_bank/controller'
require 'response_bank/model_extensions'

module Cacheable
module ResponseBank
class Railtie < ::Rails::Railtie
initializer "cachable.configure_active_record" do |config|
config.middleware.insert_after(Rack::Head, Cacheable::Middleware)
config.middleware.insert_after(Rack::Head, ResponseBank::Middleware)

ActiveSupport.on_load(:action_controller) do
include Cacheable::Controller
include ResponseBank::Controller
end

ActiveSupport.on_load(:active_record) do
include Cacheable::ModelExtensions
include ResponseBank::ModelExtensions
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
require 'digest/md5'

module Cacheable
module ResponseBank
class ResponseCacheHandler
def initialize(
key_data:,
Expand All @@ -11,7 +11,7 @@ def initialize(
serve_unversioned:,
headers:,
force_refill_cache: false,
cache_store: Cacheable.cache_store,
cache_store: ResponseBank.cache_store,
&block
)
@cache_miss_block = block
Expand All @@ -32,7 +32,7 @@ def run!
@env['cacheable.key'] = versioned_key_hash
@env['cacheable.unversioned-key'] = unversioned_key_hash

Cacheable.log(cacheable_info_dump)
ResponseBank.log(cacheable_info_dump)

if @force_refill_cache
refill_cache
Expand All @@ -56,11 +56,11 @@ def key_hash(key)
end

def versioned_key
@versioned_key ||= Cacheable.cache_key_for(key: @key_data, version: @version_data)
@versioned_key ||= ResponseBank.cache_key_for(key: @key_data, version: @version_data)
end

def unversioned_key
@unversioned_key ||= Cacheable.cache_key_for(key: @key_data)
@unversioned_key ||= ResponseBank.cache_key_for(key: @key_data)
end

def cacheable_info_dump
Expand Down Expand Up @@ -93,7 +93,7 @@ def try_to_serve_from_cache

@env['cacheable.locked'] ||= false

if @env['cacheable.locked'] || Cacheable.acquire_lock(versioned_key_hash)
if @env['cacheable.locked'] || ResponseBank.acquire_lock(versioned_key_hash)
# execute if we can get the lock
@env['cacheable.locked'] = true
elsif serving_from_noncurrent_but_recent_version_acceptable?
Expand All @@ -120,7 +120,7 @@ def serve_from_browser_cache(cache_key_hash)
@headers.delete('Content-Type')
@headers.delete('Content-Length')

Cacheable.log("Cache hit: client")
ResponseBank.log("Cache hit: client")

[304, @headers, []]
end
Expand All @@ -138,7 +138,7 @@ def serve_from_cache(cache_key_hash, message, cache_age_tolerance = nil)
status, content_type, body, timestamp, location = hit

if cache_age_tolerance && page_too_old?(timestamp, cache_age_tolerance)
Cacheable.log("Found an unversioned cache entry, but it was too old (#{timestamp})")
ResponseBank.log("Found an unversioned cache entry, but it was too old (#{timestamp})")

nil
else
Expand All @@ -150,11 +150,11 @@ def serve_from_cache(cache_key_hash, message, cache_age_tolerance = nil)
@headers['Content-Encoding'] = "gzip"
else
# we have to uncompress because the client doesn't support gzip
Cacheable.log("uncompressing for client without gzip")
body = Cacheable.decompress(body)
ResponseBank.log("uncompressing for client without gzip")
body = ResponseBank.decompress(body)
end

Cacheable.log(message)
ResponseBank.log(message)

[status, @headers, [body]]
end
Expand All @@ -168,7 +168,7 @@ def page_too_old?(timestamp, cache_age_tolerance)
def refill_cache
@env['cacheable.miss'] = true

Cacheable.log("Refilling cache")
ResponseBank.log("Refilling cache")

@cache_miss_block.call
end
Expand Down
4 changes: 4 additions & 0 deletions lib/response_bank/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true
module ResponseBank
VERSION = "1.0.0"
end
9 changes: 4 additions & 5 deletions cacheable.gemspec → response_bank.gemspec
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
# -*- encoding: utf-8 -*-
# frozen_string_literal: true
$LOAD_PATH.push(File.expand_path("../lib", __FILE__))
require "cacheable/version"
require "response_bank/version"

Gem::Specification.new do |s|
s.name = "cacheable"
s.version = Cacheable::VERSION
s.name = "response_bank"
s.version = ResponseBank::VERSION
s.license = "MIT"
s.authors = ["Tobias Lütke", "Burke Libbey"]
s.email = ["[email protected]", "[email protected]"]
s.homepage = ""
s.summary = 'Simple rails request caching'
s.description = 'Simple rails request caching'
s.summary = 'Simple response caching for Ruby applications'

s.files = Dir["lib/**/*.rb", "README.md", "LICENSE.txt"]
s.require_paths = ["lib"]
Expand Down
12 changes: 6 additions & 6 deletions test/controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
require File.dirname(__FILE__) + "/test_helper"

class CacheableControllerTest < Minitest::Test
class ResponseBankControllerTest < Minitest::Test
class MockRequest
def get?
true
Expand All @@ -23,7 +23,7 @@ def headers
end

class MockController
include Cacheable::Controller
include ResponseBank::Controller

def cache_configured?
true
Expand All @@ -44,8 +44,8 @@ def response

def setup
@cache_store = stub.tap { |s| s.stubs(read: nil) }
Cacheable.cache_store = @cache_store
Cacheable.stubs(:acquire_lock).returns(true)
ResponseBank.cache_store = @cache_store
ResponseBank.stubs(:acquire_lock).returns(true)
end

def test_cache_control_no_store_set_for_uncacheable_requests
Expand All @@ -64,7 +64,7 @@ def test_server_cache_hit

def test_client_cache_hit
controller.request.env['HTTP_IF_NONE_MATCH'] = 'deadbeef'
Cacheable::ResponseCacheHandler.any_instance.expects(:versioned_key_hash).returns('deadbeef').at_least_once
ResponseBank::ResponseCacheHandler.any_instance.expects(:versioned_key_hash).returns('deadbeef').at_least_once
controller.expects(:head).with(:not_modified)

controller.send(:response_cache) {}
Expand All @@ -77,6 +77,6 @@ def controller
end

def page_serialized
MessagePack.dump([200, "text/html", Cacheable.compress("<body>hi.</body>"), 1331765506])
MessagePack.dump([200, "text/html", ResponseBank.compress("<body>hi.</body>"), 1331765506])
end
end
Loading

0 comments on commit fe582f1

Please sign in to comment.