Sha256: 99ed21126f5ae6b68304222d3836fb1f962c5575afdfc2838bcb67d10b5abc1a

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

class Memcache
  class LocalServer
    def initialize
      @data = {}
      @expiry = {}
    end

    def name
      "local:#{hash}"
    end

    def flush_all(delay = nil)
      raise 'flush_all not supported with delay' if delay
      @data.clear
      @expiry.clear
    end

    def get(keys)
      if keys.kind_of?(Array)
        hash = {}
        keys.each do |key|
          key = key.to_s
          val = get(key)
          hash[key] = val if val
        end
        hash
      else
        key = keys.to_s
        if @expiry[key] and Time.now > @expiry[key]
          @data[key]   = nil
          @expiry[key] = nil
        end
        @data[key]
      end
    end

    def incr(key, amount = 1)
      key = key.to_s
      value = get(key)
      return unless value
      return unless value =~ /^\d+$/

      value = value.to_i + amount
      value = 0 if value < 0
      @data[key] = value.to_s
      value
    end

    def decr(key, amount = 1)
      incr(key, -amount)
    end

    def delete(key, expiry = nil)
      if expiry
        old_expiry = @expiry[key.to_s] || Time.now + expiry
        @expiry[key.to_s] = [old_expiry, expiry].min
      else
        @data.delete(key.to_s)
      end
    end

    def set(key, value, expiry = nil)
      key = key.to_s
      @data[key]   = value
      @expiry[key] = Time.now + expiry if expiry and expiry != 0
      value
    end

    def add(key, value, expiry = nil)
      return nil if get(key)
      set(key, value, expiry)
    end

    def replace(key, value, expiry = nil)
      return nil if get(key).nil?
      set(key, value, expiry)
    end

    def append(key, value)
      existing = get(key)
      return nil if existing.nil?
      set(key, existing + value)
    end

    def prepend(key, value)
      existing = get(key)
      return nil if existing.nil?
      set(key, value + existing)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ninjudd-memcache-0.9.0 lib/memcache/local_server.rb