Class: Memcached::Rails

Inherits:
Memcached show all
Defined in:
lib/memcached/rails.rb

Overview

A legacy compatibility wrapper for the Memcached class. It has basic compatibility with the memcache-client API.

Constant Summary

Constants inherited from Memcached

BEHAVIORS, BEHAVIOR_VALUES, CONVERSION_FACTORS, DEFAULTS, DIRECT_VALUE_BEHAVIORS, DISTRIBUTION_VALUES, EMPTY_STRUCT, ERRNO_HASH, EXCEPTIONS, FLAGS, HASH_VALUES, IGNORED, Lib, VERSION

Instance Attribute Summary collapse

Attributes inherited from Memcached

#options

Instance Method Summary collapse

Methods inherited from Memcached

#clone, #decrement, #destroy_credentials, #flush, #increment, load_constants, #prefix_key, #prefix_key=, #quit, #replace, #reset, #server_by_key, #servers, #set_prefix_key, #set_servers, #should_retry, #stats

Constructor Details

#initialize(*args) ⇒ Rails

See Memcached#new for details.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/memcached/rails.rb', line 17

def initialize(*args)
  opts = args.last.is_a?(Hash) ? args.pop : {}
  servers = Array(
    args.any? ? args.unshift : opts.delete(:servers)
  ).flatten.compact

  opts[:prefix_key] ||= opts[:namespace]
  @logger = opts[:logger]
  logger.info { "memcached #{VERSION} #{servers.inspect}" } if logger
  super(servers, opts)
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/memcached/rails.rb', line 12

def logger
  @logger
end

Instance Method Details

#add(key, value, ttl = @default_ttl, raw = false) ⇒ Object

Wraps Memcached#add so that it doesn’t raise.



75
76
77
78
79
80
# File 'lib/memcached/rails.rb', line 75

def add(key, value, ttl=@default_ttl, raw=false)
  super(key, value, ttl, !raw)
  "STORED\r\n" # This causes me physical pain.
rescue NotStored, NotFound # Just in case of binary protocol implementation bugs
  "NOT STORED\r\n"
end

#append(*args) ⇒ Object

Wraps Memcached#append so that it doesn’t raise.



101
102
103
104
# File 'lib/memcached/rails.rb', line 101

def append(*args)
  super
rescue NotStored, NotFound
end

#cas(key, ttl = @default_ttl, raw = false, &block) ⇒ Object Also known as: compare_and_swap

Wraps Memcached#cas so that it doesn’t raise. Doesn’t set anything if no value is present.



47
48
49
50
# File 'lib/memcached/rails.rb', line 47

def cas(key, ttl=@default_ttl, raw=false, &block)
  super(key, ttl, !raw, &block)
rescue NotFound
end

#decr(*args) ⇒ Object

Wraps Memcached#decr so that it doesn’t raise.



95
96
97
98
# File 'lib/memcached/rails.rb', line 95

def decr(*args)
  super
rescue NotFound
end

#delete(key, expiry = 0) ⇒ Object

Wraps Memcached#delete so that it doesn’t raise.



83
84
85
86
# File 'lib/memcached/rails.rb', line 83

def delete(key, expiry=0)
  super(key)
rescue NotFound
end

#get(key, raw = false) ⇒ Object Also known as: []

Wraps Memcached#get so that it doesn’t raise. This has the side-effect of preventing you from storing nil values.



35
36
37
38
# File 'lib/memcached/rails.rb', line 35

def get(key, raw=false)
  super(key, !raw)
rescue NotFound
end

#get_multi(keys, raw = false) ⇒ Object

Wraps Memcached#get.



55
56
57
# File 'lib/memcached/rails.rb', line 55

def get_multi(keys, raw=false)
  get_orig(keys, !raw)
end

#incr(*args) ⇒ Object

Wraps Memcached#incr so that it doesn’t raise.



89
90
91
92
# File 'lib/memcached/rails.rb', line 89

def incr(*args)
  super
rescue NotFound
end

#prepend(*args) ⇒ Object

Wraps Memcached#prepend so that it doesn’t raise.



107
108
109
110
# File 'lib/memcached/rails.rb', line 107

def prepend(*args)
  super
rescue NotStored, NotFound
end

#read(key, options = {}) ⇒ Object

Alternative to #get. Accepts a key and an optional options hash supporting the single option :raw.



42
43
44
# File 'lib/memcached/rails.rb', line 42

def read(key, options = {})
  get(key, options[:raw])
end

#set(key, value, ttl = @default_ttl, raw = false) ⇒ Object Also known as: []=

Wraps Memcached#set.



60
61
62
63
64
65
# File 'lib/memcached/rails.rb', line 60

def set(key, value, ttl=@default_ttl, raw=false)
  super(key, value, ttl, !raw)
  true
rescue NotStored, NotFound
  false
end

#write(key, value, options = {}) ⇒ Object

Alternative to #set. Accepts a key, value, and an optional options hash supporting the options :raw and :ttl.



69
70
71
72
# File 'lib/memcached/rails.rb', line 69

def write(key, value, options = {})
  set(key, value, options[:ttl] || @default_ttl, options[:raw])
  true
end