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 and Rails 3.2. (Note that ActiveSupport::Duration objects are supported, but not recommended, as ttl parameters. Using Fixnum ttls, such as provided by time_constants.gem, is much faster.)

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, FATAL, FLAGS, HASH_VALUES, IGNORED, Lib, NONFATAL, VERSION

Instance Attribute Summary collapse

Attributes inherited from Memcached

#options

Instance Method Summary collapse

Methods inherited from Memcached

#clone, #decrement, #exist, #flush, #get_from_last, #increment, load_constants, #prefix_key, #quit, #replace, #reset, #server_by_key, #set_prefix_key, #should_retry, #stats

Constructor Details

#initialize(*args) ⇒ Rails

See Memcached#new for details.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/memcached/rails.rb', line 36

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] if opts[:namespace]
  opts[:prefix_delimiter] = opts[:namespace_separator] if opts[:namespace_separator]

  @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.



31
32
33
# File 'lib/memcached/rails.rb', line 31

def logger
  @logger
end

Instance Method Details

#active?Boolean

Check if there are any servers defined?

Returns:

  • (Boolean)


57
58
59
# File 'lib/memcached/rails.rb', line 57

def active?
  servers.any?
end

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

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



131
132
133
134
135
136
137
138
139
# File 'lib/memcached/rails.rb', line 131

def add(key, value, ttl=@default_ttl, raw=false)
  super(key, value, ttl, !raw)
  @string_return_types ? "STORED\r\n" : true
rescue TypeError => e
  # Maybe we got an ActiveSupport::Duration
  ttl = ttl.value and retry rescue raise e
rescue *NONFATAL
  @string_return_types? "NOT STORED\r\n" : false
end

#append(*args) ⇒ Object

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



160
161
162
163
# File 'lib/memcached/rails.rb', line 160

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



83
84
85
86
87
88
89
90
91
# File 'lib/memcached/rails.rb', line 83

def cas(key, ttl=@default_ttl, raw=false, &block)
  super(key, ttl, !raw, &block)
  true
rescue TypeError => e
  # Maybe we got an ActiveSupport::Duration
  ttl = ttl.value and retry rescue raise e
rescue *NONFATAL
  false
end

#decr(*args) ⇒ Object

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



154
155
156
157
# File 'lib/memcached/rails.rb', line 154

def decr(*args)
  super
rescue *NONFATAL
end

#delete(key, expiry = 0) ⇒ Object

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



142
143
144
145
# File 'lib/memcached/rails.rb', line 142

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

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

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

Returns:

  • (Boolean)


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

def exist?(key, options = {})
  exist(key)
  true
rescue *NONFATAL
  false
end

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



119
120
121
122
123
124
125
126
127
128
# File 'lib/memcached/rails.rb', line 119

def fetch(key, options={})
  result = read(key, options)
  if result.nil?
    result = yield
    write(key, result, options)
    result
  else
    result
  end
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.



63
64
65
66
# File 'lib/memcached/rails.rb', line 63

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

#get_multi(keys, raw = false) ⇒ Object

Wraps Memcached#get.



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

def get_multi(keys, raw=false)
  get_orig(keys, !raw)
rescue *NONFATAL
  {}
end

#incr(*args) ⇒ Object

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



148
149
150
151
# File 'lib/memcached/rails.rb', line 148

def incr(*args)
  super
rescue *NONFATAL
end

#prepend(*args) ⇒ Object

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



166
167
168
169
# File 'lib/memcached/rails.rb', line 166

def prepend(*args)
  super
rescue *NONFATAL
end

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

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



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

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

#read_multi(*keys) ⇒ Object



177
178
179
180
# File 'lib/memcached/rails.rb', line 177

def read_multi(*keys)
  return {} if keys.empty?
  get_multi(keys)
end

#serversObject

Return an array of server objects.



183
184
185
186
187
188
189
# File 'lib/memcached/rails.rb', line 183

def servers
  server_structs.each do |server|
    def server.alive?
      next_retry <= Time.now
    end
  end
end

#servers=Object



33
# File 'lib/memcached/rails.rb', line 33

alias :servers= :set_servers

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

Wraps Memcached#set.



103
104
105
106
107
108
109
110
111
# File 'lib/memcached/rails.rb', line 103

def set(key, value, ttl=@default_ttl, raw=false)
  super(key, value, ttl, !raw)
  true
rescue TypeError => e
  # Maybe we got an ActiveSupport::Duration
  ttl = ttl.value and retry rescue raise e
rescue *NONFATAL
  false
end

#set_servers(servers) ⇒ Object

Wraps Memcached#set_servers to convert server objects to strings.



192
193
194
195
196
197
198
# File 'lib/memcached/rails.rb', line 192

def set_servers(servers)
  servers = Array(servers)
  servers.map! do |server|
    server.is_a?(String) ? server : inspect_server(server)
  end
  super
end

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

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



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

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