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

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(key = nil) ⇒ Object

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

If key is nil then the returned hash will be in the form of {server1 => {stat_key => value, ...}, server2 => {stat_key => value, ...}, ...} for all of the servers and stats.

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.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/memcache_stats.rb', line 10

def stats(key = nil)
  stats = {}

  @mutex.synchronize do
    raise MemCacheError, "No active servers" unless self.active?
    @servers.each {|server| stats[server.key] = server.stats}
  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) ⇒ 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.



29
30
31
# File 'lib/memcache_stats.rb', line 29

def summed_stat(key)
  stats(key).values.inject(0) {|sum, value| sum + value.to_i}
end