Sha256: 7e5867497277ef8b44cbdc85667272ebe0d96d09ccbd5ddaa86a0e4ae797f390

Contents?: true

Size: 1.38 KB

Versions: 7

Compression:

Stored size: 1.38 KB

Contents

class Memcache
  class Base
    attr_accessor :prefix

    def clear
      flush_all
    end

    # Default implementations based on get and set.

    def gets(keys)
      get(keys, true)
    end

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

      value = value.to_i + amount
      value = 0 if value < 0
      set(key, value.to_s)
      value
    end

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

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

    def cas(key, value, cas, expiry = 0, flags = 0)
      # No cas implementation yet, just do a set for now.
      set(key, value, expiry, flags)
    end

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

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

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

  protected

    def cache_key(key)
      raise Memcache::Error, "length zero key not permitted" if key.length == 0
      key = "#{prefix}#{key}"
      raise Memcache::Error, "key too long #{key.inspect}" if key.length > 250
      key
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
memcache-1.3.0 lib/memcache/base.rb
memcache-1.2.13 lib/memcache/base.rb
memcache-1.2.12 lib/memcache/base.rb
memcache-1.2.11 lib/memcache/base.rb
memcache-1.2.10 lib/memcache/base.rb
memcache-1.2.9 lib/memcache/base.rb
memcache-1.2.8 lib/memcache/base.rb