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.



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

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

#append(*args) ⇒ Object

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



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

def append(*args)
  super
rescue Memcached::NotStored
rescue Memcached::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
51
52
53
54
# File 'lib/memcached/rails.rb', line 47

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

#decr(*args) ⇒ Object

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



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

def decr(*args)
  super
rescue Memcached::NotFound
end

#delete(key, expiry = 0) ⇒ Object

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



87
88
89
90
# File 'lib/memcached/rails.rb', line 87

def delete(key, expiry=0)
  super(key)
rescue Memcached::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 Memcached::NotFound
end

#get_multi(keys, raw = false) ⇒ Object

Wraps Memcached#get.



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

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

#incr(*args) ⇒ Object

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



93
94
95
96
# File 'lib/memcached/rails.rb', line 93

def incr(*args)
  super
rescue Memcached::NotFound
end

#prepend(*args) ⇒ Object

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



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

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



64
65
66
67
68
69
70
# File 'lib/memcached/rails.rb', line 64

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



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

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