examples/expire.rb
def expire_example(client)
  key = Key.new(Shared.namespace, Shared.set_name, "expirekey ")
  bin = Bin.new("expirebin", "expirevalue")

  Shared.logger.info("Put: namespace=#{key.namespace} set=#{key.set_name} key=#{key.user_key} bin=#{bin.name} value=#{bin.value} ttl=2")

  # Specify that record expires 2 seconds after it's written.
  client.put(key, [bin], ttl: 2)

  # Read the record before it expires, showing it is there.
  Shared.logger.info("Get: namespace=#{key.namespace} set=#{key.set_name} key=#{key.user_key}")

  record = client.get(key, [bin.name], Shared.policy)

  received = record.bins[bin.name]
  expected = bin.value

  if received == expected
    Shared.logger.info("Get record successful: namespace=#{key.namespace} set=#{key.set_name} key=#{key.user_key} bin=#{bin.name} value=#{received}")
  else
    Shared.logger.fatal("Expire record mismatch: Expected #{expected}. Received #{received}.")
    exit
  end

  # Read the record after it expires, showing it's gone.
  Shared.logger.info("Sleeping for 3 seconds ...")
  sleep(3)

  record = client.get(key, [bin.name], Shared.policy)

  if record.nil?
    Shared.logger.info("Expiry of record successful. Record not found.")
  else
    Shared.logger.fatal("Found record when it should have expired.")
    exit
  end
end
#
# Write and twice read an expiration record.
#
Write and twice read an expiration record.
Write and twice read an expiration record .
def expire_example(client)
  key = Key.new(Shared.namespace, Shared.set_name, "expirekey ")
  bin = Bin.new("expirebin", "expirevalue")
  Shared.logger.info("Put: namespace=#{key.namespace} set=#{key.set_name} key=#{key.user_key} bin=#{bin.name} value=#{bin.value} ttl=2")
  client.put(key, [bin], ttl: 2)
  Shared.logger.info("Get: namespace=#{key.namespace} set=#{key.set_name} key=#{key.user_key}")
  record = client.get(key, [bin.name], Shared.policy)
  received = record.bins[bin.name]
  expected = bin.value
  if received == expected
    Shared.logger.info("Get record successful: namespace=#{key.namespace} set=#{key.set_name} key=#{key.user_key} bin=#{bin.name} value=#{received}")
  else
    Shared.logger.fatal("Expire record mismatch: Expected #{expected}. Received #{received}.")
    exit
  end
  Shared.logger.info("Sleeping for 3 seconds ...")
  sleep(3)
  record = client.get(key, [bin.name], Shared.policy)
  if record.nil?
    Shared.logger.info("Expiry of record successful. Record not found.")
  else
    Shared.logger.fatal("Found record when it should have expired.")
    exit
  end
end
def expire_example ( client ) key = Key . new ( Shared . namespace , Shared . set_name , "expirekey " ) bin = Bin . new ( "expirebin" , "expirevalue" ) Shared . logger . info ( "Put: namespace=#{key.namespace} set=#{key.set_name} key=#{key.user_key} bin=#{bin.name} value=#{bin.value} ttl=2" ) client . put ( key , [ bin ] , ttl : 2 ) Shared . logger . info ( "Get: namespace=#{key.namespace} set=#{key.set_name} key=#{key.user_key}" ) record = client . get ( key , [ bin . name ] , Shared . policy ) received = record . bins [ bin . name ] expected = bin . value if received == expected Shared . logger . info ( "Get record successful: namespace=#{key.namespace} set=#{key.set_name} key=#{key.user_key} bin=#{bin.name} value=#{received}" ) else Shared . logger . fatal ( "Expire record mismatch: Expected #{expected}. Received #{received}." ) exit end Shared . logger . info ( "Sleeping for 3 seconds ..." ) sleep ( 3 ) record = client . get ( key , [ bin . name ] , Shared . policy ) if record . nil? Shared . logger . info ( "Expiry of record successful. Record not found." ) else Shared . logger . fatal ( "Found record when it should have expired." ) exit end end
Write and twice read an expiration record.
Write and twice read an expiration record .
# Specify that record expires 2 seconds after it's written. # Read the record before it expires, showing it is there. # Read the record after it expires, showing it's gone.
client