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
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, = {}) .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, = {}) .reverse_merge! config key = normalize_key key instrument :delete, key, do @data.del key true end end |
#exist?(key) ⇒ 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) .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
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, = {}) .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
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, = {}) .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 |
# 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, = {}) .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 = {}) = 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
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 |