Commit 2de1cc08 authored by Rajko Albrecht's avatar Rajko Albrecht

add method documentation

parent c9562b7d
class Redis
# simple enhancement of Redis::Store
class StoreWithCas < Store
# Implements Compare-And-Swap (or as Redis says Compare-And-Save)
# on top of Redis::Store using Redis::Store::watch. It is designated for simple values, not redis-lists/hashes etc
module Cas
# Single CAS
#
# Trys to save change the value of a redis-key when it may not done in a atomic matter. Eg, you have do some
# checks on old value before setting the new one and so on. If key content changes meanwhile it refuses to
# set and you will not overwrite changes from other Thread or Process.
#
# This method works only on existing keys in redis.
# It works only with keys holding a value, eg, read/writeable with get/set
#
# @yield [value] the current value of given key
# @yieldparam [String] key the key to get and set the value
# @yieldreturn [String] the new value to store in key
# @return [Boolean] true if new value was stored, otherwise false
# @param key [String] the key to set. Must not be nil and key must exists in Redis
# @param ttl [Integer] if not nil and integer > 0 set a TTL to the changed key
# @example
#
# storewithcas.cas('examplekey') do |value|
# # value is the CURRENT value!
# new_value = do_some_important_stuff_here(value)
# new_value # write back to redis unless key has changed meanwhile
# end
def cas key, ttl=nil
return false unless exists(key)
watch(key) do
......@@ -15,6 +41,22 @@ class Redis
end
end
# Multi CAS
#
# Safe changing multiple keys at once. It works only with keys holding a value, eg, read/writeable with get/set
#
# @example
#
# storewithcas.cas_multi('key1','key1') do |currenthash|
# newhashedvalues = make_something_with_current(currenthash)
# newhashedvalues
# end
#
# @yield [values] a key=>value hash with values of given keys. keys not existing are not yielded
# @yieldparam [Array] keys the keys to change
# @yieldreturn [Hash] key-value-pairs to change. Keys not given in parameter or not existing in redis are ignored.
# @return [Boolean] true if tried making changes, nil when keylist empty
# @param keys [Array] the keys to set. Must not be nil and keys must exists in Redis
def cas_multi *keys
#options = extract_options keys
return if keys.empty?
......@@ -34,6 +76,13 @@ class Redis
end
end
# Read list of keys and return values as Hash
#
# @param keys Array the keys to read
# @return [Hash] key-value-pairs of key found in redis, eg, exists.
#
# @example
# values = read_multi("key1","key2","key3") #=> {"key1" => "1", "key2" => "2", "key3" => "3"}
def read_multi *keys
options = extract_options keys
keys = keys.select {|k| exists(k)}
......
class Redis
class Store < self
# Add namespaced methods missing in #Redis::Store
module Namespace
# redis.watch for a list of keys respecting the namespace for keys of the
# Redis::Store instance
# @parameter keys Array a list of keys to watch
# @see Redis::Store::watch in redis-store-gem
# @see Redis::watch in redis-gem
def watch *keys
if keys.any?
super(*keys.map {|key| interpolate(key) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment