composite_cache_store 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -5
- data/lib/composite_cache_store/version.rb +1 -1
- data/lib/composite_cache_store.rb +30 -20
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 689876c5fc7b2bd00343817eac73b522a0687482aaaeb1058a471c7fcac275e7
|
4
|
+
data.tar.gz: dba46b9f25d1302377dd0fb64c9765a8c6016545f78fdb57444282e6b4d94cd8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a04ad705343eea18cdd44d399cbd2264b11fa7eb91a6be8c0d98960b8937510abd77a780a95c816217bf2752cf7fde885dbf423b04aaee11024280876e19f994
|
7
|
+
data.tar.gz: 72bcee27ef6647b444582c481dfa011fedbb587d540537d193a3f1042965bf16e7cb740ed059f6483b7926afdd99dd2b84fe80715665ea8908dcf2c3efc1ca0f
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# CompositeCacheStore
|
2
2
|
|
3
|
-
### A composite cache store comprised of
|
3
|
+
### A composite cache store comprised of layered ActiveSupport::Cache::Store instances
|
4
4
|
|
5
5
|
<!-- Tocer[start]: Auto-generated, don't remove. -->
|
6
6
|
|
@@ -27,10 +27,10 @@ While these services are robust and performant, they can also be a source of lat
|
|
27
27
|
__A composite (or layered) cache can mitigate these risks__
|
28
28
|
by reducing traffic and backpressure on the persistence service.
|
29
29
|
|
30
|
-
Consider a composite cache that wraps a remote Redis-backed "
|
31
|
-
When both caches are warm, a read hit on the local in-memory cache returns instantly and avoids the overhead of
|
30
|
+
Consider a composite cache that wraps a remote Redis-backed "layer 2 cache" with a local in-memory "layer 1 cache".
|
31
|
+
When both caches are warm, a read hit on the local in-memory "layer 1 cache" returns instantly and avoids the overhead of
|
32
32
|
inter-process communication (IPC) and/or network traffic _(with its attendant data marshaling and socket/wire noise)_
|
33
|
-
associated with accessing the remote Redis-backed cache.
|
33
|
+
associated with accessing the remote Redis-backed "layer 2 cache".
|
34
34
|
|
35
35
|
To summarize: __Reads prioritize the inner cache and fall back to the outer cache.__
|
36
36
|
|
@@ -79,7 +79,7 @@ def Rails.composite_cache
|
|
79
79
|
),
|
80
80
|
|
81
81
|
# Layer 2 cache (outer)
|
82
|
-
Rails.cache, # use whatever makes sense for your app
|
82
|
+
Rails.cache, # use whatever makes sense for your app
|
83
83
|
|
84
84
|
# additional layers are optional
|
85
85
|
)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "active_support/
|
3
|
+
require "active_support/all"
|
4
4
|
require_relative "composite_cache_store/version"
|
5
5
|
|
6
6
|
class CompositeCacheStore
|
@@ -100,40 +100,50 @@ class CompositeCacheStore
|
|
100
100
|
end
|
101
101
|
|
102
102
|
def read_multi(...)
|
103
|
+
missed_layers = []
|
103
104
|
layers.each do |store|
|
104
|
-
|
105
|
-
|
105
|
+
hash = store.read_multi(...)
|
106
|
+
if hash.present?
|
107
|
+
missed_layers.each { |s| s.write_multi(hash) }
|
108
|
+
return hash
|
109
|
+
end
|
110
|
+
missed_layers << store
|
106
111
|
end
|
107
|
-
|
112
|
+
{}
|
108
113
|
end
|
109
114
|
|
110
115
|
def silence!
|
111
116
|
layers.each { |store| store.silence! }
|
112
117
|
end
|
113
118
|
|
114
|
-
# Only applies expiration options to the outermost cache
|
115
|
-
# Inner caches use their global expiration options
|
116
119
|
def write(name, value, options = nil)
|
117
|
-
options ||= {}
|
118
120
|
layers.each do |store|
|
119
|
-
|
120
|
-
store.write(name, value, options)
|
121
|
-
else
|
122
|
-
store.write(name, value, options.except(:expires_in, :expires_at))
|
123
|
-
end
|
121
|
+
store.write name, value, permitted_options(store, options)
|
124
122
|
end
|
125
123
|
end
|
126
124
|
|
127
|
-
# Only applies expiration options to the outermost cache
|
128
|
-
# Inner caches use their global expiration options
|
129
125
|
def write_multi(hash, options = nil)
|
130
|
-
options ||= {}
|
131
126
|
layers.each do |store|
|
132
|
-
|
133
|
-
store.write_multi(hash, options)
|
134
|
-
else
|
135
|
-
store.write_multi(hash, options.except(:expires_in, :expires_at))
|
136
|
-
end
|
127
|
+
store.write_multi hash, permitted_options(store, options)
|
137
128
|
end
|
138
129
|
end
|
130
|
+
|
131
|
+
private
|
132
|
+
|
133
|
+
def permitted_options(store, options = {})
|
134
|
+
return options if options.blank?
|
135
|
+
return options if keep_expiration?(store, options)
|
136
|
+
options.except(:expires_in, :expires_at)
|
137
|
+
end
|
138
|
+
|
139
|
+
def keep_expiration?(store, options = {})
|
140
|
+
return true if store == layers.last
|
141
|
+
return true unless store.options[:expires_in]
|
142
|
+
|
143
|
+
expires_in = options[:expires_in]
|
144
|
+
expires_in ||= Time.current - options[:expires_at] if options[:expires_at]
|
145
|
+
return false unless expires_in
|
146
|
+
|
147
|
+
expires_in < store.options[:expires_in]
|
148
|
+
end
|
139
149
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: composite_cache_store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nate Hopkins (hopsoft)
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-03-
|
11
|
+
date: 2023-03-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -82,7 +82,7 @@ dependencies:
|
|
82
82
|
version: '0'
|
83
83
|
description: |
|
84
84
|
Enhanced application performance with faster reads, data redundancy,
|
85
|
-
and reduced backpressure on the
|
85
|
+
and reduced backpressure on the outer cache store.
|
86
86
|
email:
|
87
87
|
- natehop@gmail.com
|
88
88
|
executables: []
|
@@ -118,5 +118,6 @@ requirements: []
|
|
118
118
|
rubygems_version: 3.4.6
|
119
119
|
signing_key:
|
120
120
|
specification_version: 4
|
121
|
-
summary: A composite cache store comprised of
|
121
|
+
summary: A composite cache store comprised of layered ActiveSupport::Cache::Store
|
122
|
+
instances
|
122
123
|
test_files: []
|