Class: MemCache

Inherits:
Object
  • Object
show all
Defined in:
lib/memcache_stats.rb,
lib/memcache_config.rb,
lib/memcache_server.rb

Defined Under Namespace

Classes: Server

Constant Summary collapse

STAT_TYPES =
%w[items malloc slabs]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_config(filename = nil, environment = nil) ⇒ Object

Loads the configuration from the given filename and creates a new MemCache instance that uses the options and servers from the configuration file.

See examples/memcache.yml for an example configuration file.



6
7
8
9
10
11
12
13
14
15
# File 'lib/memcache_config.rb', line 6

def MemCache.load_config(filename = nil, environment = nil)
  filename ||= "#{RAILS_ROOT}/config/memcache.yml"
  environment ||= RAILS_ENV

  config = YAML.load_file(filename)[environment]
  instance = MemCache.new(config['options'])
  instance.servers = config['servers']
    
  instance
end

Instance Method Details

#stats(options = {}) ⇒ Object

Creates a hash for the stat(s) for all of the active servers.

By default, the returned hash will be in the form of {server1 => {stat_key => value, ...}, server2 => {stat_key => value, ...}, ...} for all of the servers and stats.

Stats accepts a hash of optional arguments:

If :key is not nil then the returned hash will be in the form of {server1 => value, server2 => value, ...} for all servers for the single requested stat.

i.e. CACHE.stats(:key => 'bytes')

If :type is not nil, the passed stats type will be returned as a hash.

i.e. CACHE.stats(:type => 'slabs')

Valid :type options are: slabs malloc items

Deprecation notice: if options is not a hash then it will be used as the key. Please note that this usage is considered deprecated and will possibily be removed in future versions.

i.e. CACHE.stats('bytes')



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/memcache_stats.rb', line 25

def stats(options = {})
  stats = {}

  key  = options.is_a?(Hash) ? options[:key] : options
  type = options[:type] if options.is_a?(Hash)

  @mutex.synchronize do
    raise MemCacheError, "No active servers" unless self.active?
    @servers.each {|server| stats[server.key] = server.stats(type)}
  end
  
  unless key.nil?
    new_stats = {}
    stats.each_pair {|k,v| new_stats[k] = v[key]}
    stats = new_stats
  end

  stats
end

#summed_stat(key, type = nil) ⇒ Object

Returns a summed value for the requested stat. This is useful for determining things such as the global number of curr_items or bytes.

Can optionally be passed a type as a second parameter.



49
50
51
# File 'lib/memcache_stats.rb', line 49

def summed_stat(key, type = nil)
  stats({:key => key, :type => type}).values.inject(0) {|sum, value| sum + value.to_i}
end