Class: ActiveSupport::Cache::H2ocubeRailsCache

Inherits:
Store
  • Object
show all
Defined in:
lib/active_support/cache/h2ocube_rails_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ H2ocubeRailsCache

Returns a new instance of H2ocubeRailsCache.



8
9
10
11
12
13
14
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 8

def initialize(options = {})
  options ||= {}
  @config = options
  @namespace = options[:namespace]
  @data = Redis.new(options)
  super(options)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 6

def config
  @config
end

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 6

def data
  @data
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



6
7
8
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 6

def namespace
  @namespace
end

Instance Method Details

#clearObject



127
128
129
130
131
132
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 127

def clear
  instrument :clear, nil, nil do
    keys.each_slice(1000) { |key_slice| @data.del(*key_slice) }
    true
  end
end

#decrement(key, amount = 1, options = {}) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 160

def decrement(key, amount = 1, options = {})
  options.reverse_merge! config
  key = normalize_key key

  instrument :decrement, key, amount: amount do
    if amount == 1
      @data.decr key
    else
      @data.decrby key, amount
    end
  end
end

#delete(key, options = {}) ⇒ Object Also known as: delete_entry



112
113
114
115
116
117
118
119
120
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 112

def delete(key, options = {})
  options.reverse_merge! config
  key = normalize_key key

  instrument :delete, key, options do
    @data.keys(key).each { |k| @data.del k }
    true
  end
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
125
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 122

def exist?(key)
  key = normalize_key key
  @data.exists key
end

#expire(key, expires_in) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 138

def expire(key, expires_in)
  options.reverse_merge! config
  key = normalize_key key

  instrument :expire, key, expires_in: expires_in.to_i do
    @data.expire key, expires_in.to_i
  end
end

#fetch(key, options = {}, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 21

def fetch(key, options = {}, &block)
  key = normalize_key key

  if @data.exists(key)
    if options.key?(:force)
      force = options[:force].is_a?(Proc) ? options[:force].call(key, options) : options[:force]
      if force
        write key, yield, options
      else
        fetch_raw key, options do
          yield
        end
      end
    else
      fetch_raw(key, options) do
        yield
      end
    end
  else
    write key, yield, options
  end
end

#fetch_raw(key, options = {}, &block) ⇒ Object



44
45
46
47
48
49
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 44

def fetch_raw(key, options = {}, &block)
  key = normalize_key key
  instrument :fetch, key, options do
    exist?(key) ? read(key) : write(key, block, options)
  end
end

#increment(key, amount = 1, options = {}) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 147

def increment(key, amount = 1, options = {})
  options.reverse_merge! config
  key = normalize_key key

  instrument :increment, key, amount: amount do
    if amount == 1
      @data.incr key
    else
      @data.incrby key, amount
    end
  end
end

#infoObject



134
135
136
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 134

def info
  @data.info
end

#keys(key = '*') ⇒ Object



16
17
18
19
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 16

def keys(key = '*')
  key = normalize_key key
  @data.keys key
end

#read(key, options = {}) ⇒ Object Also known as: read_entry



51
52
53
54
55
56
57
58
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 51

def read(key, options = {})
  options.reverse_merge! config
  key = normalize_key key
  return nil if key.start_with?('http')
  instrument :read, key, options do
    load_entry @data.get(key)
  end
end

#read_multi(*names) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 66

def read_multi(*names)
  keys = names.map { |name| normalize_key(name) }
  values = @data.mget(*keys)

  names.zip(values).each_with_object({}) do |(name, value), results|
    if value
      entry = load_entry(value)
      results[name] = entry.value unless entry.nil?
    end
  end
end

#read_raw(key, options = {}) ⇒ Object



60
61
62
63
64
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 60

def read_raw(key, options = {})
  options.reverse_merge! config
  key = normalize_key key
  @data.get key
end

#write(key, entry, opts = {}) ⇒ Object Also known as: write_entry



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 78

def write(key, entry, opts = {})
  options = opts.reverse_merge config
  key = normalize_key key

  return false if key.start_with?('http')

  instrument :write, key, options do
    entry = dump_entry entry
    if entry.nil?
      Rails.logger.warn "CacheWarn: '#{key}' is not cacheable!"
      nil
    else
      if opts.key?(:expires_in) && opts[:expires_in].nil?
        @data.set key, entry
      else
        expires_in = options[:expires_in].to_i
        @data.setex key, expires_in, entry
        @data.setex "#{key}_updated_at", expires_in, Time.now.to_i if options[:updated_at]
      end
      load_entry entry
    end
  end
end

#write_multi(hash) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 102

def write_multi(hash)
  instrument :write_multi, hash do
    entries = hash.each_with_object({}) do |(name, value), memo|
      memo[normalize_key(name)] = dump_entry value
    end

    @data.mapped_mset entries
  end
end