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", 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_storeObject (readonly)

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

Returns:

  • (Boolean)


121
122
123
# File 'lib/rails-brotli-cache/store.rb', line 121

def self.supports_cache_versioning?
  true
end

Instance Method Details

#clear(options = nil) ⇒ Object



107
108
109
# File 'lib/rails-brotli-cache/store.rb', line 107

def clear(options = nil)
  @core_store.clear
end

#decrement(*args) ⇒ Object



116
117
118
119
# File 'lib/rails-brotli-cache/store.rb', line 116

def decrement(*args)
  args[0] = cache_key(args[0])
  @core_store.decrement(*args)
end

#delete(name, options = nil) ⇒ Object



103
104
105
# File 'lib/rails-brotli-cache/store.rb', line 103

def delete(name, options = nil)
  @core_store.delete(cache_key(name), options)
end

#exist?(name, options = nil) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/rails-brotli-cache/store.rb', line 99

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)
  options ||= {}

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

  uncompressed(
    @core_store.fetch(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



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

def fetch_multi(*names)
  options = names.extract_options!
  names = names.map { |name| cache_key(name) }

  @core_store.fetch_multi(
    *names, options.merge(compress: false)
  ) do |name|
    compressed(yield(name), options)
  end
end

#increment(*args) ⇒ Object



111
112
113
114
# File 'lib/rails-brotli-cache/store.rb', line 111

def increment(*args)
  args[0] = cache_key(args[0])
  @core_store.increment(*args)
end

#read(name, options = nil) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/rails-brotli-cache/store.rb', line 55

def read(name, options = nil)
  payload = @core_store.read(
    cache_key(name),
    options
  )

  uncompressed(payload, options)
end

#read_multi(*names) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/rails-brotli-cache/store.rb', line 79

def read_multi(*names)
  options = names.extract_options!
  names = names.map { |name| cache_key(name) }

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

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



44
45
46
47
48
49
50
51
52
53
# File 'lib/rails-brotli-cache/store.rb', line 44

def write(name, value, options = nil)
  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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rails-brotli-cache/store.rb', line 64

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

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