Class: ActiveSupport::Cache::H2ocubeRailsCache
- Inherits:
-
Store
- Object
- Store
- ActiveSupport::Cache::H2ocubeRailsCache
- Defined in:
- lib/active_support/cache/h2ocube_rails_cache.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
Instance Method Summary collapse
- #clear ⇒ Object
- #decrement(key, amount = 1, options = {}) ⇒ Object
- #delete(key, options = {}) ⇒ Object (also: #delete_entry)
- #exist?(key) ⇒ Boolean
- #expire(key, expires_in) ⇒ Object
- #fetch(key, options = {}, &block) ⇒ Object
- #fetch_raw(key, options = {}, &block) ⇒ Object
- #increment(key, amount = 1, options = {}) ⇒ Object
- #info ⇒ Object
-
#initialize(options = {}) ⇒ H2ocubeRailsCache
constructor
A new instance of H2ocubeRailsCache.
- #keys(key = '*') ⇒ Object
- #read(key, options = {}) ⇒ Object (also: #read_entry)
- #read_multi(*names) ⇒ Object
- #read_raw(key, options = {}) ⇒ Object
- #write(key, entry, opts = {}) ⇒ Object (also: #write_entry)
- #write_multi(hash) ⇒ Object
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( = {}) ||= {} @config = @namespace = [:namespace] @data = Redis.new() super() end |
Instance Attribute Details
#config ⇒ Object (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 |
#data ⇒ Object (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 |
#namespace ⇒ Object (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
#clear ⇒ Object
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, = {}) .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, = {}) .reverse_merge! config key = normalize_key key instrument :delete, key, do @data.keys(key).each { |k| @data.del k } true end end |
#exist?(key) ⇒ 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) .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, = {}, &block) key = normalize_key key if @data.exists(key) if .key?(:force) force = [:force].is_a?(Proc) ? [:force].call(key, ) : [:force] if force write key, yield, else fetch_raw key, do yield end end else fetch_raw(key, ) do yield end end else write key, yield, 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, = {}, &block) key = normalize_key key instrument :fetch, key, do exist?(key) ? read(key) : write(key, block, ) 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, = {}) .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 |
#info ⇒ Object
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, = {}) .reverse_merge! config key = normalize_key key return nil if key.start_with?('http') instrument :read, key, 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, = {}) .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 = {}) = opts.reverse_merge config key = normalize_key key return false if key.start_with?('http') instrument :write, key, 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 = [:expires_in].to_i @data.setex key, expires_in, entry @data.setex "#{key}_updated_at", expires_in, Time.now.to_i if [: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 |