Class: RailsBrotliCache::Store
- Inherits:
-
ActiveSupport::Cache::Store
- Object
- ActiveSupport::Cache::Store
- RailsBrotliCache::Store
show all
- Defined in:
- lib/rails-brotli-cache/store.rb
Defined Under Namespace
Classes: BrotliCompressor
Constant Summary
collapse
- COMPRESS_THRESHOLD =
ENV.fetch("BR_CACHE_COMPRESS_THRESHOLD", 1).to_f * 1024.0
- BR_COMPRESS_QUALITY =
ENV.fetch("BR_CACHE_COMPRESS_QUALITY", 5).to_i
- MARK_BR_COMPRESSED =
"\x02".b
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(core_store, options = {}) ⇒ Store
Returns a new instance of Store.
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/rails-brotli-cache/store.rb', line 14
def initialize(core_store, options = {})
@core_store = core_store
@prefix = if options.key?(:prefix)
options.fetch(:prefix)
else
"br-"
end
@compressor_class = compressor_class(options, default: BrotliCompressor)
end
|
Instance Attribute Details
#core_store ⇒ Object
Returns the value of attribute core_store.
12
13
14
|
# File 'lib/rails-brotli-cache/store.rb', line 12
def core_store
@core_store
end
|
Class Method Details
.supports_cache_versioning? ⇒ Boolean
122
123
124
|
# File 'lib/rails-brotli-cache/store.rb', line 122
def self.supports_cache_versioning?
true
end
|
Instance Method Details
#clear(options = nil) ⇒ Object
108
109
110
|
# File 'lib/rails-brotli-cache/store.rb', line 108
def clear(options = nil)
@core_store.clear
end
|
#decrement(*args) ⇒ Object
117
118
119
120
|
# File 'lib/rails-brotli-cache/store.rb', line 117
def decrement(*args)
args[0] = cache_key(args[0])
@core_store.decrement(*args)
end
|
#delete(name, options = nil) ⇒ Object
104
105
106
|
# File 'lib/rails-brotli-cache/store.rb', line 104
def delete(name, options = nil)
@core_store.delete(cache_key(name), options)
end
|
#exist?(name, options = nil) ⇒ Boolean
100
101
102
|
# File 'lib/rails-brotli-cache/store.rb', line 100
def exist?(name, options = nil)
@core_store.exist?(cache_key(name), options)
end
|
#fetch(name, options = nil, &block) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/rails-brotli-cache/store.rb', line 25
def fetch(name, options = nil, &block)
value = read(name, options)
if value.present? && !options&.fetch(:force, false) == true
return value
end
if block_given?
value = block.call
write(name, value, options)
value
elsif options && options[:force]
raise ArgumentError, "Missing block: Calling `Cache#fetch` with `force: true` requires a block."
else
read(name, options)
end
end
|
#fetch_multi(*names) ⇒ Object
91
92
93
94
95
96
97
98
|
# File 'lib/rails-brotli-cache/store.rb', line 91
def fetch_multi(*names)
options = names.
names = names.map { |name| cache_key(name) }
@core_store.fetch_multi(*names, options) do |name|
compressed(yield(name), options)
end
end
|
#increment(*args) ⇒ Object
112
113
114
115
|
# File 'lib/rails-brotli-cache/store.rb', line 112
def increment(*args)
args[0] = cache_key(args[0])
@core_store.increment(*args)
end
|
#read(name, options = nil) ⇒ Object
62
63
64
65
66
67
68
69
|
# File 'lib/rails-brotli-cache/store.rb', line 62
def read(name, options = nil)
payload = @core_store.read(
cache_key(name),
options
)
uncompressed(payload)
end
|
#read_multi(*names) ⇒ Object
82
83
84
85
86
87
88
89
|
# File 'lib/rails-brotli-cache/store.rb', line 82
def read_multi(*names)
options = names.
names = names.map { |name| cache_key(name) }
Hash[core_store.read_multi(*names, options).map do |key, val|
[source_cache_key(key), uncompressed(val)]
end]
end
|
#write(name, value, options = nil) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/rails-brotli-cache/store.rb', line 44
def write(name, value, options = nil)
if value.is_a?(Integer)
return @core_store.write(
cache_key(name),
value
)
end
options = (options || {}).reverse_merge(compress: true)
payload = compressed(value, options)
@core_store.write(
cache_key(name),
payload,
options.merge(compress: false)
)
end
|
#write_multi(hash, options = nil) ⇒ Object
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/rails-brotli-cache/store.rb', line 71
def write_multi(hash, options = nil)
new_hash = hash.map do |key, val|
[
cache_key(key),
compressed(val, options)
]
end
@core_store.write_multi(new_hash, options)
end
|