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 collapse

DEFAULTS =
{
  :logger => nil,
  :string_return_types => false
}

Constants inherited from Memcached

BEHAVIORS, BEHAVIOR_VALUES, CONVERSION_FACTORS, DIRECT_VALUE_BEHAVIORS, DISTRIBUTION_VALUES, 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, #get_from_last, #increment, load_constants, #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.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/memcached/rails.rb', line 20

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]
  @string_return_types = opts[:string_return_types]

  logger.info { "memcached #{VERSION} #{servers.inspect}" } if logger
  super(servers, opts)
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



15
16
17
# File 'lib/memcached/rails.rb', line 15

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.



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

def add(key, value, ttl=@default_ttl, raw=false)
  super(key, value, ttl, !raw)
  # This causes me  pain
  @string_return_types ? "STORED\r\n" : true
rescue NotStored
  @string_return_types? "NOT STORED\r\n" : false
end

#append(*args) ⇒ Object

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



116
117
118
119
# File 'lib/memcached/rails.rb', line 116

def append(*args)
  super
rescue NotStored
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.



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

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

#decr(*args) ⇒ Object

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



110
111
112
113
# File 'lib/memcached/rails.rb', line 110

def decr(*args)
  super
rescue NotFound
end

#delete(key, expiry = 0) ⇒ Object

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



98
99
100
101
# File 'lib/memcached/rails.rb', line 98

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

#exist?(key, options = {}) ⇒ Boolean

Returns whether the key exists, even if the value is nil.

Returns:

  • (Boolean)


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

def exist?(key, options = {})
  get_orig(key, options)
  true
rescue NotFound
  false
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.



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

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

#get_multi(keys, raw = false) ⇒ Object

Wraps Memcached#get.



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

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

#incr(*args) ⇒ Object

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



104
105
106
107
# File 'lib/memcached/rails.rb', line 104

def incr(*args)
  super
rescue NotFound
end

#prepend(*args) ⇒ Object

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



122
123
124
125
# File 'lib/memcached/rails.rb', line 122

def prepend(*args)
  super
rescue NotStored
end

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

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



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

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

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

Wraps Memcached#set.



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

def set(key, value, ttl=@default_ttl, raw=false)
  super(key, value, ttl, !raw)
  true
rescue NotStored
  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.



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

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