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



123
124
125
126
127
128
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 123

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



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 156

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



108
109
110
111
112
113
114
115
116
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 108

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

  instrument :delete, key, options do
    @data.del key
    true
  end
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 118

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

#expire(key, expires_in) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 134

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



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 143

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



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

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
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 66

def read_multi(*names)
  result = {}
  names.each do |name|
    result[name] = read name
  end
  result
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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 74

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



98
99
100
101
102
103
104
105
106
# File 'lib/active_support/cache/h2ocube_rails_cache.rb', line 98

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