Memcached 缓存存储¶ ↑
一种缓存存储实现,它将数据存储在 Memcached 中: memcached.org
这通常是生产网站中最受欢迎的缓存存储。
特殊功能
-
集群和负载均衡。可以指定多个 memcached 服务器,并且
MemCacheStore将在所有可用服务器之间进行负载均衡。如果服务器宕机,那么MemCacheStore将忽略它,直到它恢复正常。
MemCacheStore 实现 Strategy::LocalCache 策略,该策略在块内实现内存缓存。
常量
| ESCAPE_KEY_CHARS | = | /[\x00-\x20%\x7F-\xFF]/n |
| OVERRIDDEN_OPTIONS | = | UNIVERSAL_OPTIONS |
这些选项表示被此实现覆盖的行为,不应传递给 Dalli 客户端 |
||
类公共方法
new(*addresses) 链接
创建一个新的 MemCacheStore 对象,并提供给定的 memcached 服务器地址。每个地址要么是主机名,要么是“主机名:端口”格式的主机和端口字符串。例如
ActiveSupport::Cache::MemCacheStore.new("localhost", "server-downstairs.localnetwork:8229")
如果没有提供地址,但定义了 ENV['MEMCACHE_SERVERS'],则将使用它。否则,MemCacheStore 将连接到 localhost:11211(默认 memcached 端口)。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/cache/mem_cache_store.rb, line 76 def initialize(*addresses) addresses = addresses.flatten options = addresses.extract_options! if options.key?(:cache_nils) options[:skip_nil] = !options.delete(:cache_nils) end options[:max_key_size] ||= MAX_KEY_SIZE super(options) unless [String, Dalli::Client, NilClass].include?(addresses.first.class) raise ArgumentError, "First argument must be an empty array, address, or array of addresses." end @mem_cache_options = options.dup # The value "compress: false" prevents duplicate compression within Dalli. @mem_cache_options[:compress] = false (OVERRIDDEN_OPTIONS - %i(compress)).each { |name| @mem_cache_options.delete(name) } @data = self.class.build_mem_cache(*(addresses + [@mem_cache_options])) end
supports_cache_versioning?() 链接
声明缓存版本支持。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/cache/mem_cache_store.rb, line 38 def self.supports_cache_versioning? true end
实例公共方法
clear(options = nil) 链接
清除所有 memcached 服务器上的整个缓存。在使用共享缓存时,应谨慎使用此方法。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/cache/mem_cache_store.rb, line 178 def clear(options = nil) rescue_error_with(nil) { @data.with { |c| c.flush_all } } end
decrement(name, amount = 1, options = nil) 链接
使用 memcached decr 原子操作递减缓存的整数值。返回更新后的值。
如果键未设置或已过期,它将被设置为 0。Memcached 不支持负计数器。
cache.decrement("foo") # => 0
要设置特定值,请调用 write 并传递 raw: true
cache.write("baz", 5, raw: true) cache.decrement("baz") # => 4
递减非数值、或未使用 raw: true 写入的值将失败并返回 nil。
要稍后读取该值,请调用 read_counter
cache.decrement("baz") # => 3 cache.read_counter("baz") # 3
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/cache/mem_cache_store.rb, line 165 def decrement(name, amount = 1, options = nil) options = merged_options(options) key = normalize_key(name, options) instrument(:decrement, key, amount: amount) do rescue_error_with nil do @data.with { |c| c.decr(key, amount, options[:expires_in], 0) } end end end
increment(name, amount = 1, options = nil) 链接
使用 memcached incr 原子操作递增缓存的整数值。返回更新后的值。
如果键未设置或已过期,它将被设置为 amount
cache.increment("foo") # => 1 cache.increment("bar", 100) # => 100
要设置特定值,请调用 write 并传递 raw: true
cache.write("baz", 5, raw: true) cache.increment("baz") # => 6
递增非数值、或未使用 raw: true 写入的值将失败并返回 nil。
要稍后读取该值,请调用 read_counter
cache.increment("baz") # => 7 cache.read_counter("baz") # 7
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/cache/mem_cache_store.rb, line 134 def increment(name, amount = 1, options = nil) options = merged_options(options) key = normalize_key(name, options) instrument(:increment, key, amount: amount) do rescue_error_with nil do @data.with { |c| c.incr(key, amount, options[:expires_in], amount) } end end end
inspect() 链接
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/cache/mem_cache_store.rb, line 96 def inspect instance = @data || @mem_cache_options "#<#{self.class} options=#{options.inspect} mem_cache=#{instance.inspect}>" end
stats() 链接
从 memcached 服务器获取统计信息。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/cache/mem_cache_store.rb, line 183 def stats @data.with { |c| c.stats } end
write(name, value, options = nil) 链接
行为与 ActiveSupport::Cache::Store#write 相同,但支持 memcached 特有的其他选项。
附加选项¶ ↑
-
raw: true- 将值作为原始字节直接发送到服务器。值必须是字符串或数字。您只能对原始值使用 memcached 的直接操作,如increment和decrement。
来源: 在 GitHub 上
# File activesupport/lib/active_support/cache/mem_cache_store.rb, line 102