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
116
117
118
|
# File 'lib/rails-brotli-cache/store.rb', line 116
def self.supports_cache_versioning?
true
end
|
Instance Method Details
#clear(options = nil) ⇒ Object
102
103
104
|
# File 'lib/rails-brotli-cache/store.rb', line 102
def clear(options = nil)
@core_store.clear
end
|
#decrement(*args) ⇒ Object
111
112
113
114
|
# File 'lib/rails-brotli-cache/store.rb', line 111
def decrement(*args)
args[0] = cache_key(args[0])
@core_store.decrement(*args)
end
|
#delete(name, options = nil) ⇒ Object
98
99
100
|
# File 'lib/rails-brotli-cache/store.rb', line 98
def delete(name, options = nil)
@core_store.delete(cache_key(name), options)
end
|
#exist?(name, options = nil) ⇒ Boolean
94
95
96
|
# File 'lib/rails-brotli-cache/store.rb', line 94
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
|
#increment(*args) ⇒ Object
106
107
108
109
|
# File 'lib/rails-brotli-cache/store.rb', line 106
def increment(*args)
args[0] = cache_key(args[0])
@core_store.increment(*args)
end
|
#read(name, options = nil) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/rails-brotli-cache/store.rb', line 74
def read(name, options = nil)
payload = @core_store.read(
cache_key(name),
options
)
return nil unless payload.present?
return payload if payload.is_a?(Integer)
serialized = if payload.start_with?(MARK_BR_COMPRESSED)
compressor = compressor_class(options, default: @compressor_class)
compressor.inflate(payload.byteslice(1..-1))
else
payload
end
Marshal.load(serialized)
end
|
#write(name, value, options = nil) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# 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
serialized = Marshal.dump(value)
options = (options || {}).reverse_merge(compress: true)
payload = if serialized.bytesize >= COMPRESS_THRESHOLD && !options.fetch(:compress) == false
compressor = compressor_class(options, default: @compressor_class)
compressed_payload = compressor.deflate(serialized)
if compressed_payload.bytesize < serialized.bytesize
MARK_BR_COMPRESSED + compressed_payload
else
serialized
end
else
serialized
end
@core_store.write(
cache_key(name),
payload,
options.merge(compress: false)
)
end
|