Class: MemCache
- Inherits:
-
Object
- Object
- MemCache
- 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
'UTF-8'- DEFAULT_PORT =
Default memcached port.
11211- DEFAULT_WEIGHT =
Default memcached server weight.
1
Instance Attribute Summary collapse
-
#multithread ⇒ Object
readonly
The multithread setting for this instance.
-
#namespace ⇒ Object
readonly
The namespace for this instance.
-
#pool_name ⇒ Object
readonly
The configured socket pool name for this client.
-
#request_timeout ⇒ Object
Returns the value of attribute request_timeout.
Instance Method Summary collapse
- #add(key, value, expiry = 0, raw = false) ⇒ Object
- #alive? ⇒ Boolean
- #decr(key, amount = 1) ⇒ Object
- #delete(key, expiry = 0) ⇒ Object
- #flush_all ⇒ Object
- #get(key, raw = false) ⇒ Object (also: #[])
- #get_multi(keys, raw = false) ⇒ Object
- #incr(key, amount = 1) ⇒ Object
-
#initialize(*args) ⇒ MemCache
constructor
A new instance of MemCache.
- #servers ⇒ Object
- #set(key, value, expiry = 0, raw = false) ⇒ Object (also: #[]=)
- #stats ⇒ Object
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
#multithread ⇒ Object (readonly)
The multithread setting for this instance
50 51 52 |
# File 'lib/memcache.rb', line 50 def multithread @multithread end |
#namespace ⇒ Object (readonly)
The namespace for this instance
45 46 47 |
# File 'lib/memcache.rb', line 45 def namespace @namespace end |
#pool_name ⇒ Object (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_timeout ⇒ Object
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
170 171 172 173 174 175 176 177 |
# File 'lib/memcache.rb', line 170 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
125 126 127 |
# File 'lib/memcache.rb', line 125 def alive? @pool.servers.to_a.any? end |
#decr(key, amount = 1) ⇒ Object
190 191 192 193 194 195 |
# File 'lib/memcache.rb', line 190 def decr(key, amount = 1) value = get(key) || 0 value -= amount set key, value value end |
#delete(key, expiry = 0) ⇒ Object
179 180 181 |
# File 'lib/memcache.rb', line 179 def delete(key, expiry = 0) @client.delete(make_cache_key(key)) end |
#flush_all ⇒ Object
197 198 199 |
# File 'lib/memcache.rb', line 197 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 |
#get_multi(keys, raw = false) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/memcache.rb', line 141 def get_multi(keys, raw = false) keys = keys.map {|k| make_cache_key(k)} keys = keys.to_java :String values = {} values_j = @client.getMulti(keys) values_j.to_a.each {|kv| k,v = kv next if v.nil? unless raw marshal_bytes = java.lang.String.new(v).getBytes(MARSHALLING_CHARSET) v = Marshal.load(String.from_java_bytes(marshal_bytes)) end values[k] = v } values end |
#incr(key, amount = 1) ⇒ Object
183 184 185 186 187 188 |
# File 'lib/memcache.rb', line 183 def incr(key, amount = 1) value = get(key) || 0 value += amount set key, value value end |
#servers ⇒ Object
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: []=
158 159 160 161 162 163 164 165 166 |
# File 'lib/memcache.rb', line 158 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 |
#stats ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/memcache.rb', line 201 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 |