Module: ResponseBank

Defined in:
lib/response_bank.rb,
lib/response_bank/railtie.rb,
lib/response_bank/version.rb,
lib/response_bank/controller.rb,
lib/response_bank/middleware.rb,
lib/response_bank/model_extensions.rb,
lib/response_bank/response_cache_handler.rb

Defined Under Namespace

Modules: Controller, ModelExtensions Classes: Middleware, Railtie, ResponseCacheHandler

Constant Summary collapse

VERSION =
"1.3.5"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cache_storeObject

Returns the value of attribute cache_store.



10
11
12
# File 'lib/response_bank.rb', line 10

def cache_store
  @cache_store
end

.logger=(value) ⇒ Object (writeonly)

Sets the attribute logger

Parameters:

  • value

    the value to set the attribute logger to.



11
12
13
# File 'lib/response_bank.rb', line 11

def logger=(value)
  @logger = value
end

Class Method Details

.acquire_lock(_cache_key) ⇒ Object

Raises:

  • (NotImplementedError)


17
18
19
# File 'lib/response_bank.rb', line 17

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

.cache_key_for(data) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/response_bank.rb', line 68

def cache_key_for(data)
  case data
  when Hash
    return data.inspect unless data.key?(:key)

    key = hash_value_str(data[:key])

    key = %{#{data[:key_schema_version]}:#{key}} if data[:key_schema_version]

    key = %{#{key}:#{hash_value_str(data[:version])}} if data[:version]

    # add the encoding to only the cache key but don't expose this detail in the entity_tag
    key = %{#{key}:#{hash_value_str(data[:encoding])}} if data[:encoding]

    key
  when Array
    data.inspect
  when Time, DateTime
    data.to_i
  when Date
    data.to_s # Date#to_i does not support timezones, using iso8601 instead
  when true, false, Integer, Symbol, String
    data.inspect
  else
    data.to_s.inspect
  end
end

.check_encoding(env, default_encoding = 'br') ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/response_bank.rb', line 96

def check_encoding(env, default_encoding = 'br')
  if env['HTTP_ACCEPT_ENCODING'].to_s.include?('br')
    'br'
  elsif env['HTTP_ACCEPT_ENCODING'].to_s.include?('gzip')
    'gzip'
  else
    # No encoding requested from client, but we still need to cache the page in server cache
    default_encoding
  end
end

.compress(content, encoding = "br") ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/response_bank.rb', line 33

def compress(content, encoding = "br")
  case encoding
  when 'gzip'
    attempts = 0

    begin
      Zlib.gzip(content, level: Zlib::BEST_COMPRESSION)
    rescue Zlib::BufError
      # We get sporadic Zlib::BufError, so we retry once (https://github.com/ruby/zlib/issues/49)
      attempts += 1

      if attempts <= 1
        retry
      else
        raise
      end
    end
  when 'br'
    Brotli.deflate(content, mode: :text, quality: 7)
  else
    raise ArgumentError, "Unsupported encoding: #{encoding}"
  end
end

.decompress(content, encoding = "br") ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/response_bank.rb', line 57

def decompress(content, encoding = "br")
  case encoding
  when 'gzip'
    Zlib.gunzip(content)
  when 'br'
    Brotli.inflate(content)
  else
    raise ArgumentError, "Unsupported encoding: #{encoding}"
  end
end

.log(message) ⇒ Object



13
14
15
# File 'lib/response_bank.rb', line 13

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

.read_from_backing_cache_store(_env, cache_key, backing_cache_store: cache_store) ⇒ Object



29
30
31
# File 'lib/response_bank.rb', line 29

def read_from_backing_cache_store(_env, cache_key, backing_cache_store: cache_store)
  backing_cache_store.read(cache_key, raw: true)
end

.write_to_backing_cache_store(_env, key, payload, expires_in: nil) ⇒ Object



25
26
27
# File 'lib/response_bank.rb', line 25

def write_to_backing_cache_store(_env, key, payload, expires_in: nil)
  cache_store.write(key, payload, raw: true, expires_in: expires_in)
end

.write_to_cache(_key) ⇒ Object



21
22
23
# File 'lib/response_bank.rb', line 21

def write_to_cache(_key)
  yield
end