module Porpoise::String

Public Class Methods

append(key, value) click to toggle source
# File lib/porpoise/string.rb, line 4
def append(key, value)
  o = find_stored_object(key)
  o.value += value
  o.save

  o.value.size
end
decr(key) click to toggle source
# File lib/porpoise/string.rb, line 12
def decr(key)
  o = find_stored_object(key)
  o.value = (o.value.to_i - 1).to_s
  o.save

  o.value
end
decrby(key, decrement) click to toggle source
# File lib/porpoise/string.rb, line 20
def decrby(key, decrement)
  o = find_stored_object(key)
  o.value = (o.value.to_i - decrement.to_i).to_s
  o.save

  o.value
end
get(key) click to toggle source
# File lib/porpoise/string.rb, line 28
def get(key)
  o = find_stored_object(key)
  return nil if o.new_record?
  o.value
end
getrange(key, first, last) click to toggle source
# File lib/porpoise/string.rb, line 34
def getrange(key, first, last)
  o = find_stored_object(key)
  return nil if o.new_record?
  o.value[first..last]
end
getset(key, value) click to toggle source
# File lib/porpoise/string.rb, line 40
def getset(key, value)
  o = find_stored_object(key)
  return nil if o.new_record?
  
  ov = o.value
  o.value = value
  o.save

  return ov
end
incr(key) click to toggle source
# File lib/porpoise/string.rb, line 51
def incr(key)
  o = find_stored_object(key)
  o.value = (o.value.to_i + 1).to_s
  o.save

  o.value
end
incrby(key, increment) click to toggle source
# File lib/porpoise/string.rb, line 59
def incrby(key, increment)
  o = find_stored_object(key)
  o.value = (o.value.to_i + increment.to_i).to_s
  o.save

  o.value
end
mget(key, *other_keys) click to toggle source
# File lib/porpoise/string.rb, line 67
def mget(key, *other_keys)
  o = find_stored_object(key)
  values = o.new_record? ? [nil] : [o.value]
  other_keys = other_keys.map { |k| Porpoise::key_with_namespace(k) }

  oo = Porpoise::KeyValueObject.not_expired.where(key: other_keys).all.index_by(&:key)
  other_keys.each do |ok|
    values << (oo.has_key?(ok) ? oo[ok].value : nil)
  end

  return values
end
mset(key, value, *other_keys_and_values) click to toggle source
# File lib/porpoise/string.rb, line 80
def mset(key, value, *other_keys_and_values)
  Porpoise::KeyValueObject.transaction do
    o = find_stored_object(key)
    o.value = value
    o.save

    new_keys_and_values = ::Hash[*other_keys_and_values]
    new_keys_and_values.each do |nk, nv|
      oo = find_stored_object(nk)
      oo.value = nv
      oo.save
    end
  end
  
  return true
end
set(key, value, ex = nil, px = nil, nx_or_xx = nil) click to toggle source
# File lib/porpoise/string.rb, line 104
def set(key, value, ex = nil, px = nil, nx_or_xx = nil)
  o = find_stored_object(key, false)
  o.value = value.to_s

  if nx_or_xx
    if nx_or_xx.downcase.eql?('nx')
      return nil if !o.new_record?
    elsif nx_or_xx.downcase.eql?('xx')
      return nil if o.new_record?
    end
  end

  o.expiration_date = (Time.now + ex) unless ex.nil?
  o.expiration_date = (Time.now + (px / 1000)) unless px.nil?

  o.save
end
setex(key, seconds, value) click to toggle source
# File lib/porpoise/string.rb, line 97
def setex(key, seconds, value)
  o = find_stored_object(key)
  o.value = value
  o.expiration_date = Time.now + seconds
  return o.save
end
strlen(key) click to toggle source
# File lib/porpoise/string.rb, line 122
def strlen(key)
  o = find_stored_object(key)
  o.value.size
end

Private Class Methods

find_stored_object(key, raise_on_type_mismatch = true, raise_on_not_found = false) click to toggle source
# File lib/porpoise/string.rb, line 129
def find_stored_object(key,
  raise_on_type_mismatch = true,
  raise_on_not_found = false)
  
  key = Porpoise::key_with_namespace(key)
  o = Porpoise::KeyValueObject.where(key: key).first
  
  if raise_on_type_mismatch && !o.nil? && o.data_type != 'String'
    raise Porpoise::TypeMismatch.new(
      "Key #{key} is not of type String (is #{o.data_type})"
    )
  end
  
  if raise_on_not_found && o.nil?
    raise Porpoise::KeyNotFound.new("Key #{key} could not be found")
  elsif o.nil?
    o = Porpoise::KeyValueObject.new(key: key, value: ::String.new)
  elsif o.expired?
    Porpoise::KeyValueObject.retry_lock_error(20) { o.delete }
    o = Porpoise::KeyValueObject.new(key: key, value: ::String.new)
  end

  return o
end