Class: MemCache

Inherits:
Object
  • Object
show all
Defined in:
lib/memcache.rb

Defined Under Namespace

Classes: MemCacheError

Constant Summary collapse

VERSION =
'1.5.0'
DEFAULT_OPTIONS =

Default options for the cache object.

{
  :namespace   => nil,
  :readonly    => false,
  :multithread => true,
  :pool_initial_size => 5,
  :pool_min_size => 5,
  :pool_max_size => 250,
  :pool_max_idle => (1000 * 60 * 60 * 6),
  :pool_maintenance_thread_sleep => 30,
  :pool_use_nagle => false,
  :pool_socket_timeout => 3000,
  :pool_socket_connect_timeout => 3000,
  :pool_name => 'default'
}
MARSHALLING_CHARSET =

CHARSET for Marshalling

'ISO-8859-1'
DEFAULT_PORT =

Default memcached port.

11211
DEFAULT_WEIGHT =

Default memcached server weight.

1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ MemCache

Returns a new instance of MemCache.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/memcache.rb', line 56

def initialize(*args)
  @servers = []
  opts = {}

  case args.length
  when 0 then # NOP
  when 1 then
    arg = args.shift
    case arg
    when Hash   then opts = arg
    when Array  then @servers = arg
    when String then @servers = [arg]
    else raise ArgumentError, 'first argument must be Array, Hash or String'
    end
  when 2 then
    @servers, opts = args
    @servers = [@servers].flatten
  else
    raise ArgumentError, "wrong number of arguments (#{args.length} for 2)"
  end

  opts = DEFAULT_OPTIONS.merge opts
      
  @namespace = opts[:namespace] || opts["namespace"]
  @pool_name = opts[:pool_name] || opts["pool_name"]

  @client = MemCachedClient.new(@pool_name)
  
  @client.primitiveAsString = true 
  @client.sanitizeKeys = false
  
  weights = Array.new(@servers.size, DEFAULT_WEIGHT)

  @pool = SockIOPool.getInstance(@pool_name)
  unless @pool.initialized?
    # // set the servers and the weights
    @pool.servers = @servers.to_java(:string)
    @pool.weights = weights.to_java(:Integer)
    
    # // set some basic pool settings
    # // 5 initial, 5 min, and 250 max conns
    # // and set the max idle time for a conn
    # // to 6 hours
    @pool.initConn = opts[:pool_initial_size]
    @pool.minConn = opts[:pool_min_size] 
    @pool.maxConn = opts[:pool_max_size]
    @pool.maxIdle = opts[:pool_max_idle]
    
    # // set the sleep for the maint thread
    # // it will wake up every x seconds and
    # // maintain the pool size
    @pool.maintSleep = opts[:pool_maintenance_thread_sleep]
    # 
    # // set some TCP settings
    # // disable nagle
    # // set the read timeout to 3 secs
    # // and don't set a connect timeout
    @pool.nagle = opts[:pool_use_nagle]
    @pool.socketTO = opts[:pool_socket_timeout]
    @pool.socketConnectTO = opts[:pool_socket_connect_timeout]
    @pool.aliveCheck = true
    @pool.initialize__method
  end
end

Instance Attribute Details

#multithreadObject (readonly)

The multithread setting for this instance



50
51
52
# File 'lib/memcache.rb', line 50

def multithread
  @multithread
end

#namespaceObject (readonly)

The namespace for this instance



45
46
47
# File 'lib/memcache.rb', line 45

def namespace
  @namespace
end

#pool_nameObject (readonly)

The configured socket pool name for this client.



54
55
56
# File 'lib/memcache.rb', line 54

def pool_name
  @pool_name
end

#request_timeoutObject

Returns the value of attribute request_timeout.



40
41
42
# File 'lib/memcache.rb', line 40

def request_timeout
  @request_timeout
end

Instance Method Details

#add(key, value, expiry = 0, raw = false) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/memcache.rb', line 153

def add(key, value, expiry = 0, raw = false)
  value = marshal_value(value) unless raw
  if expiry == 0
    @client.add make_cache_key(key), value
  else
    @client.add make_cache_key(key), value, expiration(expiry)
  end
end

#alive?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/memcache.rb', line 125

def alive?
  @pool.servers.to_a.any?
end

#decr(key, amount = 1) ⇒ Object



173
174
175
176
177
178
# File 'lib/memcache.rb', line 173

def decr(key, amount = 1)
  value = get(key) || 0
  value -= amount
  set key, value
  value
end

#delete(key, expiry = 0) ⇒ Object



162
163
164
# File 'lib/memcache.rb', line 162

def delete(key, expiry = 0)
  @client.delete(make_cache_key(key))
end

#flush_allObject



180
181
182
# File 'lib/memcache.rb', line 180

def flush_all
  @client.flushAll
end

#get(key, raw = false) ⇒ Object Also known as: []



129
130
131
132
133
134
135
136
137
# File 'lib/memcache.rb', line 129

def get(key, raw = false)
  value = @client.get(make_cache_key(key))
  return nil if value.nil?
  unless raw
    marshal_bytes = java.lang.String.new(value).getBytes(MARSHALLING_CHARSET)
    value = Marshal.load(String.from_java_bytes(marshal_bytes))
  end
  value
end

#incr(key, amount = 1) ⇒ Object



166
167
168
169
170
171
# File 'lib/memcache.rb', line 166

def incr(key, amount = 1)
  value = get(key) || 0
  value += amount
  set key, value
  value
end

#serversObject



121
122
123
# File 'lib/memcache.rb', line 121

def servers
  @pool.get_servers.to_a rescue []
end

#set(key, value, expiry = 0, raw = false) ⇒ Object Also known as: []=



141
142
143
144
145
146
147
148
149
# File 'lib/memcache.rb', line 141

def set(key, value, expiry = 0, raw = false)
  value = marshal_value(value) unless raw
  key = make_cache_key(key)
  if expiry == 0
    @client.set key, value
  else
    @client.set key, value, expiration(expiry)
  end
end

#statsObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/memcache.rb', line 184

def stats
  stats_hash = {}
  @client.stats.each do |server, stats|
    stats_hash[server] = Hash.new
    stats.each do |key, value|
      unless key == 'version'
        value = value.to_f
        value = value.to_i if value == value.ceil
      end
      stats_hash[server][key] = value
    end
  end
  stats_hash
end