stockpile_cache 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e0cf5948f44622314a97af71ab6ce39aa3f59931092b7ac6d80f424e052be90d
4
- data.tar.gz: bec173c3c11807350c3b4d4cb05f638fc4a90c71837882f7b64931db1dce8416
3
+ metadata.gz: '055681de81947f8bb1934929e2a0467a332c2c7bfaee62e3fe0c0fdac7c51033'
4
+ data.tar.gz: 7a38b8765377b2b3df4b7919c0148b543cc5f8465a559b1e08ddc9fb4cf4a1f6
5
5
  SHA512:
6
- metadata.gz: '08218ce4ab3946ae6b418dd0d72c354170415eff8b041d0e02bbc261ffca83fe4afd7718d9bb7907d2b80093afaeeaa8f83dc57e7b9a7aa4d91c1384ccf965b7'
7
- data.tar.gz: fddd745d31dae1f5509cd3988e216d2fdfe5e520a971e58ef3ede3ed864c5b2e34191abef7b67bfab0115134fa4cdceec43fd404d8d8430c53df931b4f01bde1
6
+ metadata.gz: b917312a5c31ac63518097609ee538a2699f8c629926386cff33c7c3ab9cf1ed6161a1486ac0b708cd9055d21d424c00c4e4945a315d3237db6c33440b9d65e2
7
+ data.tar.gz: 19f55f2f2b072d4a11f3af6c2fd6596068c6c31a5745eff056548c196ef3d2cda58af8dcaa00cf93a07355cf8b9e8f1fad43e9e8cf5ce1e4db6eaaefbe0e46db
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.0
4
+ - Allowing optional compression of cached content
5
+
3
6
  ## 1.2.0
4
7
  - Adding support for multiple Redis databases/server
5
8
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stockpile_cache (1.2.0)
4
+ stockpile_cache (1.3.0)
5
5
  connection_pool
6
6
  oj
7
7
  rake
@@ -14,13 +14,13 @@ GEM
14
14
  connection_pool (2.2.2)
15
15
  diff-lcs (1.3)
16
16
  jaro_winkler (1.5.3)
17
- oj (3.9.2)
17
+ oj (3.10.6)
18
18
  parallel (1.17.0)
19
19
  parser (2.6.4.1)
20
20
  ast (~> 2.4.0)
21
21
  rainbow (3.0.0)
22
22
  rake (13.0.1)
23
- redis (4.1.3)
23
+ redis (4.1.4)
24
24
  rspec (3.8.0)
25
25
  rspec-core (~> 3.8.0)
26
26
  rspec-expectations (~> 3.8.0)
@@ -53,4 +53,4 @@ DEPENDENCIES
53
53
  stockpile_cache!
54
54
 
55
55
  BUNDLED WITH
56
- 2.0.2
56
+ 2.1.4
data/README.md CHANGED
@@ -149,6 +149,44 @@ Using `configuration_file` setting will make Stockpile ignore all other
149
149
  Redis connection related settings and it will read configuration from `.yml`
150
150
  file instead.
151
151
 
152
+ ### Compression of Cached Content
153
+ Stockpile optionally supports compression of cached content; you will not see
154
+ much benefit from compressing small strings but once you start caching bigger
155
+ payloads like fragments of HTML you could see some improvements by using
156
+ compression. To use compression you will have to use configuration file set by
157
+ `STOCKPILE_CONFIGURATION_FILE`.
158
+
159
+ To enable compression you have to do two things. First you have to set
160
+ `configuration_file` setting to point at `.yml` containing your configuration.
161
+ You can do so by either setting a `STOCKPILE_CONFIGURATION_FILE` environment
162
+ variable or by executing a configuration block during runtime (for Rails create
163
+ `config/initializers/stockpile.rb` with following content):
164
+
165
+ ```
166
+ Stockpile.configure do |configuration|
167
+ configuration.configuration_file = <PATH/TO/FILE>
168
+ end
169
+ ```
170
+
171
+ Second thing to do is to create a `.yml` configuration file. It has to have at
172
+ least one database definition. Providing `sentinels` and `compression` is
173
+ optional. Everything else is mandatory:
174
+
175
+
176
+ ```
177
+ ---
178
+ master:
179
+ url: 'redis://redis-1-host:6379/1'
180
+ sentinels: '8.8.8.8:42,8.8.4.4:42'
181
+ compression: true
182
+ pool_options:
183
+ size: 5
184
+ timeout: 5
185
+ ```
186
+
187
+ From that point everything that will be cached in `master` database will be
188
+ compressed.
189
+
152
190
  ## Caveats
153
191
  There is no timeout or rescue set for code you will be running through the cache. If
154
192
  you need to do either you have to handle it outside of Stockpile.
@@ -22,21 +22,32 @@ module Stockpile
22
22
  module Cache
23
23
  module_function
24
24
 
25
- def get(db: :default, key:)
25
+ def get(db: :default, key:, compress: false)
26
26
  value_from_cache = Stockpile.redis(db: db) { |r| r.get(key) }
27
- Oj.load(value_from_cache) if value_from_cache
27
+
28
+ return unless value_from_cache
29
+
30
+ if compress && value_from_cache
31
+ Oj.load(Zlib::Inflate.inflate(Base64.decode64(value_from_cache)))
32
+ else
33
+ Oj.load(value_from_cache)
34
+ end
28
35
  end
29
36
 
30
- def get_deferred(db: :default, key:)
37
+ def get_deferred(db: :default, key:, compress: false)
31
38
  sleep(Stockpile::SLUMBER_COOLDOWN) until Stockpile.redis(db: db) { |r| r.exists(key) }
32
- value_from_cache = Stockpile.redis(db: db) { |r| r.get(key) }
33
- Oj.load(value_from_cache)
39
+
40
+ get(db: db, key: key, compress: compress)
34
41
  end
35
42
 
36
- def set(db: :default, key:, payload:, ttl:)
37
- payload = Oj.dump(payload)
38
- Stockpile.redis(db: db) { |r| r.set(key, payload) }
39
- Stockpile.redis(db: db) { |r| r.expire(key, ttl) }
43
+ def set(db: :default, key:, payload:, ttl:, compress: false)
44
+ payload = if compress
45
+ Base64.encode64(Zlib::Deflate.deflate(Oj.dump(payload)))
46
+ else
47
+ Oj.dump(payload)
48
+ end
49
+
50
+ Stockpile.redis(db: db) { |r| r.setex(key, ttl, payload) }
40
51
  end
41
52
  end
42
53
  end
@@ -23,5 +23,5 @@ module Stockpile
23
23
  DEFAULT_TTL = 60 * 5
24
24
  LOCK_PREFIX = 'stockpile_lock::'
25
25
  SLUMBER_COOLDOWN = 0.05
26
- VERSION = '1.2.0'
26
+ VERSION = '1.3.0'
27
27
  end
@@ -44,6 +44,10 @@ module Stockpile
44
44
 
45
45
  private
46
46
 
47
+ def compress?
48
+ RedisConnections.compression?(db: db)
49
+ end
50
+
47
51
  def execution
48
52
  @execution ||= Stockpile::Lock.perform_locked(db: db, lock_key: lock_key) do
49
53
  yield
@@ -55,7 +59,8 @@ module Stockpile
55
59
  db: db,
56
60
  key: key,
57
61
  payload: execution.result,
58
- ttl: ttl
62
+ ttl: ttl,
63
+ compress: compress?
59
64
  )
60
65
 
61
66
  execution.release_lock
@@ -68,7 +73,7 @@ module Stockpile
68
73
 
69
74
  def wait_for_cache_or_yield
70
75
  Timeout.timeout(Stockpile.configuration.slumber) do
71
- Stockpile::Cache.get_deferred(db: db, key: key)
76
+ Stockpile::Cache.get_deferred(db: db, key: key, compress: compress?)
72
77
  end
73
78
  rescue Timeout::Error
74
79
  yield
@@ -22,6 +22,10 @@ module Stockpile
22
22
  module RedisConnections
23
23
  module_function
24
24
 
25
+ def compression?(db:)
26
+ instance_variable_get("@#{db}_compression".to_sym)
27
+ end
28
+
25
29
  def with(db:)
26
30
  instance_variable_get("@#{db}".to_sym).with do |connection|
27
31
  yield connection
@@ -29,10 +29,9 @@ module Stockpile
29
29
  Redis.new(database[:redis_configuration])
30
30
  end
31
31
 
32
- RedisConnections.instance_variable_set(
33
- "@#{database[:db]}".to_sym,
34
- pool
35
- )
32
+ RedisConnections.instance_variable_set("@#{database[:db]}".to_sym, pool)
33
+
34
+ RedisConnections.instance_variable_set("@#{database[:db]}_compression".to_sym, database[:compression])
36
35
  end
37
36
 
38
37
  RedisConnections
@@ -27,11 +27,18 @@ module Stockpile
27
27
  {
28
28
  db: database,
29
29
  pool_configuration: extract_pool(settings: settings),
30
- redis_configuration: extract_redis(settings: settings)
30
+ redis_configuration: extract_redis(settings: settings),
31
+ compression: extract_compression(settings: settings)
31
32
  }
32
33
  end
33
34
  end
34
35
 
36
+ def extract_compression(settings:)
37
+ return true if settings['compression'].eql?(true)
38
+
39
+ false
40
+ end
41
+
35
42
  def extract_redis(settings:)
36
43
  sentinels = Stockpile::RedisConnectionsFactory.process_sentinels(
37
44
  sentinels: settings['sentinels'] || ''
data/lib/stockpile.rb CHANGED
@@ -14,11 +14,13 @@
14
14
  # See the License for the specific language governing permissions and
15
15
  # limitations under the License.
16
16
 
17
+ require 'base64'
17
18
  require 'connection_pool'
18
19
  require 'oj'
19
20
  require 'redis'
20
21
  require 'timeout'
21
22
  require 'yaml'
23
+ require 'zlib'
22
24
 
23
25
  require 'stockpile/constants'
24
26
  require 'stockpile/configuration'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stockpile_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ConvertKit, LLC
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-28 00:00:00.000000000 Z
11
+ date: 2020-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: connection_pool