Class: Memcached::Rails

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

Constants

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

Aliases

Old nameNew name
set_servers servers=
flush flush_all

Attributes

NameRead/write?
logger R

Public Class Methods


new (*args)

See Memcached#new for details.

    # File lib/memcached/rails.rb, line 20
20:     def initialize(*args)
21:       opts = args.last.is_a?(Hash) ? args.pop : {}
22:       servers = Array(
23:         args.any? ? args.unshift : opts.delete(:servers)
24:       ).flatten.compact
25: 
26:       opts[:prefix_key] ||= opts[:namespace]
27:       @logger = opts[:logger]
28:       @string_return_types = opts[:string_return_types]
29:       
30:       logger.info { "memcached #{VERSION} #{servers.inspect}" } if logger
31:       super(servers, opts)
32:     end

Public Instance Methods




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

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

    # File lib/memcached/rails.rb, line 81
81:     def add(key, value, ttl=@default_ttl, raw=false)
82:       super(key, value, ttl, !raw)
83:       # This causes me  pain
84:       @string_return_types ? "STORED\r\n" : true
85:     rescue NotStored
86:       @string_return_types? "NOT STORED\r\n" : false
87:     end

append (*args)

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

     # File lib/memcached/rails.rb, line 108
108:     def append(*args)
109:       super
110:     rescue NotStored
111:     end

cas (key, ttl=@default_ttl, raw=false, &block)

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

    # File lib/memcached/rails.rb, line 52
52:     def cas(key, ttl=@default_ttl, raw=false, &block)
53:       super(key, ttl, !raw, &block)
54:       true
55:     rescue NotFound
56:       false
57:     end


decr (*args)

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

     # File lib/memcached/rails.rb, line 102
102:     def decr(*args)
103:       super
104:     rescue NotFound
105:     end

delete (key, expiry=0)

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

    # File lib/memcached/rails.rb, line 90
90:     def delete(key, expiry=0)
91:       super(key)
92:     rescue NotFound
93:     end

get (key, raw=false)

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

    # File lib/memcached/rails.rb, line 40
40:     def get(key, raw=false)
41:       super(key, !raw)
42:     rescue NotFound
43:     end

get_multi (keys, raw=false)

Wraps Memcached#get.

    # File lib/memcached/rails.rb, line 62
62:     def get_multi(keys, raw=false)
63:       get_orig(keys, !raw)
64:     end

incr (*args)

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

    # File lib/memcached/rails.rb, line 96
96:     def incr(*args)
97:       super
98:     rescue NotFound
99:     end

logger= (logger)

    # File lib/memcached/rails.rb, line 34
34:     def logger=(logger)
35:       @logger = logger
36:     end

prepend (*args)

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

     # File lib/memcached/rails.rb, line 114
114:     def prepend(*args)
115:       super
116:     rescue NotStored
117:     end

read (key, options = {})

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

    # File lib/memcached/rails.rb, line 47
47:     def read(key, options = {})
48:       get(key, options[:raw])
49:     end

set (key, value, ttl=@default_ttl, raw=false)

Wraps Memcached#set.

    # File lib/memcached/rails.rb, line 67
67:     def set(key, value, ttl=@default_ttl, raw=false)
68:       super(key, value, ttl, !raw)
69:       true
70:     rescue NotStored
71:       false
72:     end

write (key, value, options = {})

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

    # File lib/memcached/rails.rb, line 76
76:     def write(key, value, options = {})
77:       set(key, value, options[:ttl] || @default_ttl, options[:raw])
78:     end