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, 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, #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
# 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]
  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.



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

def add(key, value, ttl=@default_ttl, raw=false)
  super(key, value, ttl, !raw)
  # This causes me physical pain.
  opts[:string_return_types] ? "STORED\r\n" : true
rescue NotStored # This can still throw exceptions. What's the right behavior if the
                 # server goes away?
  opts[:string_return_types] ? "NOT STORED\r\n" : false
end

#append(*args) ⇒ Object

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



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

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



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

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

#decr(*args) ⇒ Object

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



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

def decr(*args)
  super
rescue
end

#delete(key, expiry = 0) ⇒ Object

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



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

def delete(key, expiry=0)
  super(key)
rescue
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.



44
45
46
47
# File 'lib/memcached/rails.rb', line 44

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

#get_multi(keys, raw = false) ⇒ Object

Wraps Memcached#get.



66
67
68
# File 'lib/memcached/rails.rb', line 66

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

#incr(*args) ⇒ Object

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



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

def incr(*args)
  super
rescue
end

#prepend(*args) ⇒ Object

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



119
120
121
122
# File 'lib/memcached/rails.rb', line 119

def prepend(*args)
  super
rescue
end

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

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



51
52
53
# File 'lib/memcached/rails.rb', line 51

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

#serversObject



32
33
34
35
36
# File 'lib/memcached/rails.rb', line 32

def servers
  server_structs.map do |server|
    Server.new server
  end
end

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

Wraps Memcached#set.



71
72
73
74
75
76
# File 'lib/memcached/rails.rb', line 71

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



80
81
82
# File 'lib/memcached/rails.rb', line 80

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