Class: RailsBrotliCache::Store

Inherits:
ActiveSupport::Cache::Store
  • Object
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", 6).to_i
MARK_BR_COMPRESSED =
"\x02".b
DEFAULT_OPTIONS =
{
  compress_threshold: COMPRESS_THRESHOLD,
  compress: true,
  compressor_class: BrotliCompressor
}

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.



33
34
35
36
37
38
39
40
41
42
# File 'lib/rails-brotli-cache/store.rb', line 33

def initialize(core_store, options = {})
  @core_store = core_store
  @prefix = if options.key?(:prefix)
    options.fetch(:prefix)
  else
    "br-"
  end

  @init_options = options.reverse_merge(DEFAULT_OPTIONS)
end

Instance Attribute Details

#core_storeObject (readonly)

Returns the value of attribute core_store.



31
32
33
# File 'lib/rails-brotli-cache/store.rb', line 31

def core_store
  @core_store
end

Class Method Details

.supports_cache_versioning?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/rails-brotli-cache/store.rb', line 144

def self.supports_cache_versioning?
  true
end

Instance Method Details

#clear(options = {}) ⇒ Object



132
133
134
# File 'lib/rails-brotli-cache/store.rb', line 132

def clear(options = {})
  @core_store.clear(**options)
end

#decrement(name, amount = 1, **options) ⇒ Object



140
141
142
# File 'lib/rails-brotli-cache/store.rb', line 140

def decrement(name, amount = 1, **options)
  @core_store.decrement(expanded_cache_key(name), amount, **options)
end

#delete(name, options = {}) ⇒ Object



128
129
130
# File 'lib/rails-brotli-cache/store.rb', line 128

def delete(name, options = {})
  @core_store.delete(expanded_cache_key(name), options)
end

#exist?(name, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/rails-brotli-cache/store.rb', line 124

def exist?(name, options = {})
  @core_store.exist?(expanded_cache_key(name), options)
end

#fetch(name, options = nil, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rails-brotli-cache/store.rb', line 44

def fetch(name, options = nil, &block)
  options = (options || {}).reverse_merge(@init_options)

  if !block_given? && options[:force]
    raise ArgumentError, "Missing block: Calling `Cache#fetch` with `force: true` requires a block."
  end

  uncompressed(
    @core_store.fetch(expanded_cache_key(name), options.merge(compress: false)) do
      if block_given?
        compressed(block.call, options)
      else
        nil
      end
    end,
    options
  )
end

#fetch_multi(*names) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rails-brotli-cache/store.rb', line 110

def fetch_multi(*names)
  options = names.extract_options!
  names = names.map { |name| expanded_cache_key(name) }
  options = options.reverse_merge(@init_options)

  @core_store.fetch_multi(
    *names, options.merge(compress: false)
  ) do |name|
    compressed(yield(source_cache_key(name)), options)
  end.map do |key, val|
    [source_cache_key(key), uncompressed(val, options)]
  end.to_h
end

#increment(name, amount = 1, **options) ⇒ Object



136
137
138
# File 'lib/rails-brotli-cache/store.rb', line 136

def increment(name, amount = 1, **options)
  @core_store.increment(expanded_cache_key(name), amount, **options)
end

#read(name, options = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/rails-brotli-cache/store.rb', line 74

def read(name, options = nil)
  options = (options || {}).reverse_merge(@init_options)

  payload = @core_store.read(
    expanded_cache_key(name),
    options
  )

  uncompressed(payload, options)
end

#read_multi(*names) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/rails-brotli-cache/store.rb', line 100

def read_multi(*names)
  options = names.extract_options!
  names = names.map { |name| expanded_cache_key(name) }
  options = options.reverse_merge(@init_options)

  core_store.read_multi(*names, options).map do |key, val|
    [source_cache_key(key), uncompressed(val, options)]
  end.to_h
end

#write(name, value, options = nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/rails-brotli-cache/store.rb', line 63

def write(name, value, options = nil)
  options = (options || {}).reverse_merge(@init_options)
  payload = compressed(value, options)

  @core_store.write(
    expanded_cache_key(name),
    payload,
    options.merge(compress: false)
  )
end

#write_multi(hash, options = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rails-brotli-cache/store.rb', line 85

def write_multi(hash, options = nil)
  options = (options || {}).reverse_merge(@init_options)
  new_hash = hash.map do |key, val|
    [
      expanded_cache_key(key),
      compressed(val, options)
    ]
  end

  @core_store.write_multi(
    new_hash,
    options.merge(compress: false)
  )
end