### Home Classes Methods

### In Files

- lib/memcache/pg_server.rb

### Parent

Memcache::Base

### Methods

- ::new
- #add
- #append
- #decr
- #delete
- #flush_all
- #get
- #incr
- #name
- #prepend
- #replace
- #set

### 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::PGServer

### Attributes

db[R]
table[R]

### Public Class Methods

new(opts) click to toggle source

    # File lib/memcache/pg_server.rb, line 14
    def initialize(opts)
      @table = opts[:table]
      @db    = opts[:db] || ActiveRecord::Base.connection.raw_connection
    end

### Public Instance Methods

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

    # File lib/memcache/pg_server.rb, line 82
    def add(key, value, expiry = 0)
      delete_expired(key)
      insert(key, value, expiry)
      value
    rescue PGError => e
      nil
    end

append(key, value) click to toggle source

    # File lib/memcache/pg_server.rb, line 96
    def append(key, value)
      delete_expired(key)
      result = db.exec %{
        UPDATE #{table}
          SET value = value || #{quote(value)}, updated_at = NOW()
          WHERE key = #{quote(key)} AND #{prefix_clause}
      }
      result.cmdtuples == 1
    end

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

    # File lib/memcache/pg_server.rb, line 62
    def decr(key, amount = 1)
      incr(key, -amount)
    end

delete(key) click to toggle source

    # File lib/memcache/pg_server.rb, line 66
    def delete(key)
      result = db.exec %{
        DELETE FROM #{table}
          WHERE key = #{quote(key)} AND #{prefix_clause}
      }
      result.cmdtuples == 1
    end

flush_all(delay = nil) click to toggle source

    # File lib/memcache/pg_server.rb, line 26
    def flush_all(delay = nil)
      db.exec("TRUNCATE #{table}")
    end

get(keys) click to toggle source

    # File lib/memcache/pg_server.rb, line 30
    def get(keys)
      return get([keys])[keys.to_s] unless keys.kind_of?(Array)
      return {} if keys.empty?

      keys = keys.collect {|key| quote(key.to_s)}.join(',')
      sql = %{
        SELECT key, value FROM #{table}
          WHERE key IN (#{keys}) AND #{prefix_clause} AND #{expiry_clause}
      }
      results = {}
      db.query(sql).each do |key, value|
        results[key] = value
      end
      results
    end

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

    # File lib/memcache/pg_server.rb, line 46
    def incr(key, amount = 1)
      transaction do
        value = get(key)
        return unless value
        return unless value =~ /^\d+$/

        value = value.to_i + amount
        value = 0 if value < 0
        db.exec %{
          UPDATE #{table} SET value = #{quote(value)}, updated_at = NOW()
            WHERE key = #{quote(key)} AND #{prefix_clause}
        }
        value
      end
    end

name() click to toggle source

    # File lib/memcache/pg_server.rb, line 19
    def name
      @name ||= begin
        db_config = db.instance_variable_get(:@config)
        "#{db_config[:host]}:#{db_config[:database]}:#{table}"
      end
    end

prepend(key, value) click to toggle source

    # File lib/memcache/pg_server.rb, line 106
    def prepend(key, value)
      delete_expired(key)
      result = db.exec %{
        UPDATE #{table}
          SET value = #{quote(value)} || value, updated_at = NOW()
          WHERE key = #{quote(key)} AND #{prefix_clause}
      }
      result.cmdtuples == 1
    end

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

    # File lib/memcache/pg_server.rb, line 90
    def replace(key, value, expiry = 0)
      delete_expired(key)
      result = update(key, value, expiry)
      result.cmdtuples == 1 ? value : nil
    end

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

    # File lib/memcache/pg_server.rb, line 74
    def set(key, value, expiry = 0)
      transaction do
        delete(key)
        insert(key, value, expiry)
      end
      value
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.