response_bank 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 22412918297172ba47f51811bb250ace6dbb2b3d8eb26405f0f970a18b2479de
4
- data.tar.gz: 94022c0b2e796e9a0a8a0fcc47f7f4ba40f64118458695554683d21452009388
3
+ metadata.gz: 2a5d65c4fd4310708e807cde576efeebdc590c16eb8e16190aef77a715ed48c2
4
+ data.tar.gz: 498da7cd7cfcc2da09b517e6694093551e79288ec4e52e815950448353ba4a0f
5
5
  SHA512:
6
- metadata.gz: eb15b72956d34a954d829f1ac5666568e5b0c1b4eeebd7a127d8f88b5d103efbd856fe0d4466c675826868a2e8fb8fd572004891753a0bf1799ba3bc04a1cc81
7
- data.tar.gz: 86ac266abcbe74dbf7fb72c833def9283fdd5943c0666fc1bf6dbed34008161c3458d85f1d365f7b8d6aacdd4af1a5b8822bc792c33de094668490d6108d4a87
6
+ metadata.gz: 5985c959c55ebf8917d9687dd0e3c3d82cae36684f7466d7c1d1805097da2fd57cadaa6efa9f7d618a757e24949a7e493aefe0e298f5c0254d9b595f7871bba8
7
+ data.tar.gz: 5bc8ac60f2d2ce149120a8f0bc98aca57899511359f5a67a125b6cc3894edf0043dffad8be635ae9fa95a9d1b295220a091b0d2faa118982f9740eac5e3d6786
data/README.md CHANGED
@@ -22,7 +22,32 @@ This gem supports the following versions of Ruby and Rails:
22
22
  gem 'response_bank'
23
23
  ```
24
24
 
25
- 2. use `#response_cache` method to any desired controller's action
25
+ 2. add an initializer file. We need to configure the `acquire_lock` method, set the cache store and the logger
26
+
27
+ ```ruby
28
+ require 'response_bank'
29
+
30
+ module ResponseBank
31
+ LOCK_TTL = 90
32
+
33
+ class << self
34
+ def acquire_lock(cache_key)
35
+ cache_store.write("#{cache_key}:lock", '1', unless_exist: true, expires_in: LOCK_TTL, raw: true)
36
+ end
37
+ end
38
+ end
39
+
40
+ ResponseBank.cache_store = ActiveSupport::Cache.lookup_store(Rails.configuration.cache_store)
41
+ ResponseBank.logger = Rails.logger
42
+
43
+ ```
44
+
45
+ 3. enables caching on your application
46
+ ```ruby
47
+ config.action_controller.perform_caching = true
48
+ ```
49
+
50
+ 4. use `#response_cache` method to any desired controller's action
26
51
 
27
52
  ```ruby
28
53
  class PostsController < ApplicationController
@@ -35,7 +60,17 @@ class PostsController < ApplicationController
35
60
  end
36
61
  ```
37
62
 
38
- 3. **(optional)** override custom cache key data. For default, cache key is defined by URL and query string
63
+ 5. **(optional)** set a custom TTL for the cache by overriding the `write_to_backing_cache_store` method in your initializer file
64
+ ```ruby
65
+ module ResponseBank
66
+ CACHE_TTL = 30.minutes
67
+ def write_to_backing_cache_store(_env, key, payload, expires_in: CACHE_TTL)
68
+ cache_store.write(key, payload, raw: true, expires_in: expires_in)
69
+ end
70
+ end
71
+ ```
72
+
73
+ 6. **(optional)** override custom cache key data. For default, cache key is defined by URL and query string
39
74
 
40
75
  ```ruby
41
76
  class PostsController < ApplicationController
data/lib/response_bank.rb CHANGED
@@ -21,6 +21,14 @@ module ResponseBank
21
21
  yield
22
22
  end
23
23
 
24
+ def write_to_backing_cache_store(_env, key, payload, expires_in: nil)
25
+ cache_store.write(key, payload, raw: true, expires_in: expires_in)
26
+ end
27
+
28
+ def read_from_backing_cache_store(_env, cache_key, backing_cache_store: cache_store)
29
+ backing_cache_store.read(cache_key, raw: true)
30
+ end
31
+
24
32
  def compress(content)
25
33
  io = StringIO.new
26
34
  gz = Zlib::GzipWriter.new(io)
@@ -44,10 +44,15 @@ module ResponseBank
44
44
 
45
45
  ResponseBank.write_to_cache(env['cacheable.key']) do
46
46
  payload = MessagePack.dump(cache_data)
47
- ResponseBank.cache_store.write(env['cacheable.key'], payload, raw: true)
47
+ ResponseBank.write_to_backing_cache_store(
48
+ env,
49
+ env['cacheable.key'],
50
+ payload,
51
+ expires_in: env['cacheable.versioned-cache-expiry'],
52
+ )
48
53
 
49
54
  if env['cacheable.unversioned-key']
50
- ResponseBank.cache_store.write(env['cacheable.unversioned-key'], payload, raw: true)
55
+ ResponseBank.write_to_backing_cache_store(env, env['cacheable.unversioned-key'], payload)
51
56
  end
52
57
  end
53
58
 
@@ -127,7 +127,7 @@ module ResponseBank
127
127
  end
128
128
 
129
129
  def serve_from_cache(cache_key_hash, message, cache_age_tolerance = nil)
130
- raw = @cache_store.read(cache_key_hash)
130
+ raw = ResponseBank.read_from_backing_cache_store(@env, cache_key_hash, backing_cache_store: @cache_store)
131
131
 
132
132
  if raw
133
133
  hit = MessagePack.load(raw)
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module ResponseBank
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: response_bank
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Lütke
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-01-27 00:00:00.000000000 Z
12
+ date: 2021-08-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: useragent
@@ -129,7 +129,8 @@ files:
129
129
  homepage: ''
130
130
  licenses:
131
131
  - MIT
132
- metadata: {}
132
+ metadata:
133
+ allowed_push_host: https://rubygems.org
133
134
  post_install_message:
134
135
  rdoc_options: []
135
136
  require_paths:
@@ -145,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
146
  - !ruby/object:Gem::Version
146
147
  version: '0'
147
148
  requirements: []
148
- rubygems_version: 3.1.2
149
+ rubygems_version: 3.2.20
149
150
  signing_key:
150
151
  specification_version: 4
151
152
  summary: Simple response caching for Ruby applications