Class: ActiveSupport::Cache::LibmemcachedStore
- Inherits:
-
Object
- Object
- ActiveSupport::Cache::LibmemcachedStore
- Defined in:
- lib/active_support/cache/libmemcached_store.rb
Overview
Store using memcached gem as client
Global options can be passed to be applied to each method by default. Supported options are
-
:compress
: if set to true, data will be compress before stored -
:compress_threshold
: specify the threshold at which to compress
value, default is 4K
-
:namespace
: prepend each key with this value for simple namespacing -
:expires_in
: default TTL in seconds for each. Default value is 0, i.e. forever
Specific value can be passed per key with write and fetch command.
Options can also be passed direclty to the memcache client, via the :client
option. For example, if you want to use pipelining, you can use :client => { :no_block => true }
Direct Known Subclasses
Defined Under Namespace
Classes: FetchWithRaceConditionTTLEntry
Constant Summary collapse
- DEFAULT_CLIENT_OPTIONS =
{ distribution: :consistent_ketama, binary_protocol: true, default_ttl: 0 }
- ESCAPE_KEY_CHARS =
/[\x00-\x20%\x7F-\xFF]/n
- DEFAULT_COMPRESS_THRESHOLD =
4096
- FLAG_COMPRESSED =
0x2
Instance Attribute Summary collapse
-
#addresses ⇒ Object
readonly
Returns the value of attribute addresses.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#silence ⇒ Object
(also: #silence?)
readonly
Returns the value of attribute silence.
Instance Method Summary collapse
- #clear(options = nil) ⇒ Object
- #decrement(key, amount = 1, options = nil) ⇒ Object
- #delete(key, options = nil) ⇒ Object
- #exist?(key, options = nil) ⇒ Boolean
- #fetch(key, options = nil, &block) ⇒ Object
- #increment(key, amount = 1, options = nil) ⇒ Object
-
#initialize(*addresses) ⇒ LibmemcachedStore
constructor
A new instance of LibmemcachedStore.
-
#mute ⇒ Object
Silence the logger within a block.
- #read(key, options = nil) ⇒ Object
- #read_multi(*names) ⇒ Object
-
#silence! ⇒ Object
Silence the logger.
- #stats ⇒ Object
- #write(key, value, options = nil) ⇒ Object
Constructor Details
#initialize(*addresses) ⇒ LibmemcachedStore
Returns a new instance of LibmemcachedStore.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/active_support/cache/libmemcached_store.rb', line 67 def initialize(*addresses) addresses.flatten! = addresses. = .delete(:client) || {} if [:namespace] [:prefix_key] = .delete(:namespace) [:prefix_delimiter] = ':' end @namespace_length = "#{[:prefix_key]}#{[:prefix_delimiter]}".force_encoding("BINARY").size [:default_ttl] = .delete(:expires_in).to_i if [:expires_in] @options = {compress_threshold: DEFAULT_COMPRESS_THRESHOLD}.merge() @addresses = addresses @cache = ::LibmemcachedStore::MemcachedWithFlags.new(@addresses, DEFAULT_CLIENT_OPTIONS.merge()) end |
Instance Attribute Details
#addresses ⇒ Object (readonly)
Returns the value of attribute addresses.
43 44 45 |
# File 'lib/active_support/cache/libmemcached_store.rb', line 43 def addresses @addresses end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
50 51 52 |
# File 'lib/active_support/cache/libmemcached_store.rb', line 50 def @options end |
#silence ⇒ Object (readonly) Also known as: silence?
Returns the value of attribute silence.
50 51 52 |
# File 'lib/active_support/cache/libmemcached_store.rb', line 50 def silence @silence end |
Instance Method Details
#clear(options = nil) ⇒ Object
200 201 202 203 204 |
# File 'lib/active_support/cache/libmemcached_store.rb', line 200 def clear( = nil) instrument(:clear, "*") do @cache.flush end end |
#decrement(key, amount = 1, options = nil) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/active_support/cache/libmemcached_store.rb', line 166 def decrement(key, amount = 1, = nil) key = normalize_key(key) instrument(:decrement, key, amount: amount) do @cache.decr(key, amount) end rescue Memcached::NotFound nil rescue Memcached::Error => e log_error(e) nil end |
#delete(key, options = nil) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/active_support/cache/libmemcached_store.rb', line 133 def delete(key, = nil) key = normalize_key(key) instrument(:delete, key) do |payload| delete_entry(key, ) end end |
#exist?(key, options = nil) ⇒ Boolean
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/active_support/cache/libmemcached_store.rb', line 140 def exist?(key, = nil) key = normalize_key(key) instrument(:exist?, key) do |payload| if @cache.respond_to?(:exist) @cache.exist(key) true else read_entry(key, ) != nil end end rescue Memcached::NotFound false end |
#fetch(key, options = nil, &block) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/active_support/cache/libmemcached_store.rb', line 84 def fetch(key, = nil, &block) if block_given? if && [:race_condition_ttl] && [:expires_in] fetch_with_race_condition_ttl(key, , &block) else key = normalize_key(key) unless && [:force] entry = instrument(:read, key, ) do |payload| read_entry(key, ).tap do |result| if payload payload[:super_operation] = :fetch payload[:hit] = !!result end end end end if entry.nil? result = instrument(:generate, key, ) do |payload| yield end write_entry(key, result, ) result else instrument(:fetch_hit, key, ) { |payload| } entry end end else read(key, ) end end |
#increment(key, amount = 1, options = nil) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/active_support/cache/libmemcached_store.rb', line 154 def increment(key, amount = 1, = nil) key = normalize_key(key) instrument(:increment, key, amount: amount) do @cache.incr(key, amount) end rescue Memcached::NotFound nil rescue Memcached::Error => e log_error(e) nil end |
#mute ⇒ Object
Silence the logger within a block.
60 61 62 63 64 65 |
# File 'lib/active_support/cache/libmemcached_store.rb', line 60 def mute previous_silence, @silence = defined?(@silence) && @silence, true yield ensure @silence = previous_silence end |
#read(key, options = nil) ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/active_support/cache/libmemcached_store.rb', line 117 def read(key, = nil) key = normalize_key(key) instrument(:read, key, ) do |payload| entry = read_entry(key, ) payload[:hit] = !!entry if payload entry end end |
#read_multi(*names) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/active_support/cache/libmemcached_store.rb', line 178 def read_multi(*names) names.flatten! = names. return {} if names.empty? mapping = Hash[names.map {|name| [normalize_key(name), name] }] keys = mapping.keys raw_values, flags = instrument(:read_multi, keys, ) do @cache.get(keys, false, true) end values = {} raw_values.each do |key, value| values[mapping[key]] = convert_race_condition_entry(deserialize(value, [:raw], flags[key])) end values rescue Memcached::Error => e log_error(e) {} end |
#silence! ⇒ Object
Silence the logger.
54 55 56 57 |
# File 'lib/active_support/cache/libmemcached_store.rb', line 54 def silence! @silence = true self end |
#stats ⇒ Object
206 207 208 |
# File 'lib/active_support/cache/libmemcached_store.rb', line 206 def stats @cache.stats end |
#write(key, value, options = nil) ⇒ Object
126 127 128 129 130 131 |
# File 'lib/active_support/cache/libmemcached_store.rb', line 126 def write(key, value, = nil) key = normalize_key(key) instrument(:write, key, ) do |payload| write_entry(key, value, ) end end |