### Home Classes Methods

### In Files

- lib/memcache/server.rb

### Parent

Base

### Methods

- ::new
- #add
- #alive?
- #append
- #cache_key
- #cas
- #clone
- #close
- #count
- #decr
- #delete
- #flush_all
- #get
- #incr
- #input_key
- #inspect
- #name
- #prepend
- #replace
- #set
- #stats
- #strict_reads?

### Files

- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- Memcache
- Memcache::Base
- Memcache::ClientError
- Memcache::ConnectionError
- Memcache::Error
- Memcache::LocalServer
- Memcache::Migration
- Memcache::NullServer
- Memcache::PGServer
- Memcache::Pool
- Memcache::Segmented
- Memcache::SegmentedNativeServer
- Memcache::SegmentedServer
- Memcache::Server
- Memcache::ServerError
- Object
- PGconn

No matching classes.

# Memcache::Server

### Constants

CONNECT_TIMEOUT
DEFAULT_PORT
ESCAPE
READ_RETRY_DELAY
UNESCAPE

### Attributes

host[R]
port[R]
retry_at[R]
status[R]

### Public Class Methods

new(opts) click to toggle source

    # File lib/memcache/server.rb, line 13
    def initialize(opts)
      @host         = opts[:host]
      @port         = opts[:port] || DEFAULT_PORT
      @strict_reads = opts[:strict_reads]
      @status       = 'NOT CONNECTED'
    end

### Public Instance Methods

add(key, value, expiry = 0, flags = 0) click to toggle source

    # File lib/memcache/server.rb, line 133
    def add(key, value, expiry = 0, flags = 0)
      response = write_command("add #{cache_key(key)} #{flags.to_i} #{expiry.to_i} #{value.to_s.size}", value)
      response == "STORED\r\n" ? value : nil
    end

alive?() click to toggle source

    # File lib/memcache/server.rb, line 32
    def alive?
      @retry_at.nil? or @retry_at < Time.now
    end

append(key, value) click to toggle source

    # File lib/memcache/server.rb, line 143
    def append(key, value)
      response = write_command("append #{cache_key(key)} 0 0 #{value.to_s.size}", value)
      response == "STORED\r\n"
    end

cas(key, value, cas, expiry = 0, flags = 0) click to toggle source

    # File lib/memcache/server.rb, line 128
    def cas(key, value, cas, expiry = 0, flags = 0)
      response = write_command("cas #{cache_key(key)} #{flags.to_i} #{expiry.to_i} #{value.to_s.size} #{cas.to_i}", value)
      response == "STORED\r\n" ? value : nil
    end

clone() click to toggle source

    # File lib/memcache/server.rb, line 20
    def clone
      self.class.new(:host => host, :port => port, :strict_reads => strict_reads?)
    end

close(error = nil) click to toggle source

    # File lib/memcache/server.rb, line 40
    def close(error = nil)
      # Close the socket. If there is an error, mark the server dead.
      @socket.close if @socket and not @socket.closed?
      @socket = nil

      if error
        @retry_at = Time.now + READ_RETRY_DELAY
        @status   = "DEAD: %s: %s, will retry at %s" % [error.class, error.message, @retry_at]
      else
        @retry_at = nil
        @status   = "NOT CONNECTED"
      end
    end

count() click to toggle source

    # File lib/memcache/server.rb, line 70
    def count
      stats['curr_items']
    end

decr(key, amount = 1) click to toggle source

    # File lib/memcache/server.rb, line 112
    def decr(key, amount = 1)
      raise Error, "decr requires unsigned value" if amount < 0
      response = write_command("decr #{cache_key(key)} #{amount}")
      response == "NOT_FOUND\r\n" ? nil : response.slice(0..-3).to_i
    end

delete(key) click to toggle source

    # File lib/memcache/server.rb, line 118
    def delete(key)
      write_command("delete #{cache_key(key)}") == "DELETED\r\n" ? true : nil
    end

flush_all(delay = nil) click to toggle source

    # File lib/memcache/server.rb, line 74
    def flush_all(delay = nil)
      write_command("flush_all #{delay}")
    end

get(keys, cas = nil) click to toggle source

    # File lib/memcache/server.rb, line 78
    def get(keys, cas = nil)
      return get([keys], cas)[keys.to_s] unless keys.kind_of?(Array)
      return {} if keys.empty?

      method = cas ? 'gets' : 'get'

      results = {}
      keys = keys.collect {|key| cache_key(key)}

      read_command("#{method} #{keys.join(' ')}") do |response|
        if cas
          key, flags, length, cas = match_response!(response, /^VALUE ([^\s]+) ([^\s]+) ([^\s]+) ([^\s]+)/)
        else
          key, flags, length = match_response!(response, /^VALUE ([^\s]+) ([^\s]+) ([^\s]+)/)
        end

        value = socket.read(length.to_i)
        match_response!(socket.read(2), "\r\n")

        value.memcache_flags = flags.to_i
        value.memcache_cas   = cas

        key = input_key(key)
        results[key] = value
      end
      results
    end

incr(key, amount = 1) click to toggle source

    # File lib/memcache/server.rb, line 106
    def incr(key, amount = 1)
      raise Error, "incr requires unsigned value" if amount < 0
      response = write_command("incr #{cache_key(key)} #{amount}")
      response == "NOT_FOUND\r\n" ? nil : response.slice(0..-3).to_i
    end

inspect() click to toggle source

    # File lib/memcache/server.rb, line 24
    def inspect
      "<#{self.class.name}: %s:%d (%s)>" % [@host, @port, @status]
    end

name() click to toggle source

    # File lib/memcache/server.rb, line 28
    def name
      "#{host}:#{port}"
    end

prepend(key, value) click to toggle source

    # File lib/memcache/server.rb, line 148
    def prepend(key, value)
      response = write_command("prepend #{cache_key(key)} 0 0 #{value.to_s.size}", value)
      response == "STORED\r\n"
    end

replace(key, value, expiry = 0, flags = 0) click to toggle source

    # File lib/memcache/server.rb, line 138
    def replace(key, value, expiry = 0, flags = 0)
      response = write_command("replace #{cache_key(key)} #{flags.to_i} #{expiry.to_i} #{value.to_s.size}", value)
      response == "STORED\r\n" ? value : nil
    end

set(key, value, expiry = 0, flags = 0) click to toggle source

    # File lib/memcache/server.rb, line 122
    def set(key, value, expiry = 0, flags = 0)
      return delete(key) if value.nil?
      write_command("set #{cache_key(key)} #{flags.to_i} #{expiry.to_i} #{value.to_s.size}", value)
      value
    end

stats() click to toggle source

    # File lib/memcache/server.rb, line 54
    def stats
      stats = {}
      read_command('stats') do |response|
        key, value = match_response!(response, /^STAT ([\w]+) (-?[\w\.\:]+)/)

        if ['rusage_user', 'rusage_system'].include?(key)
          seconds, microseconds = value.split(/:/, 2)
          microseconds ||= 0
          stats[key] = Float(seconds) + (Float(microseconds) / 1_000_000)
        else
          stats[key] = (value =~ /^-?\d+$/ ? value.to_i : value)
        end
      end
      stats
    end

strict_reads?() click to toggle source

    # File lib/memcache/server.rb, line 36
    def strict_reads?
      @strict_reads
    end

### Protected Instance Methods

cache_key(key) click to toggle source

    # File lib/memcache/server.rb, line 171
    def cache_key(key)
      key = key.gsub(/[\s\\]/) {|c| ESCAPE[c]}
      super(key)
    end

input_key(key) click to toggle source

    # File lib/memcache/server.rb, line 165
    def input_key(key)
      key = key[prefix.size..-1] if prefix # Remove prefix from key.
      key = key.gsub(/\\./) {|c| UNESCAPE[c]}
      key
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.