Skip to content

Instantly share code, notes, and snippets.

@uggedal
Created February 24, 2010 22:12
Show Gist options
  • Select an option

  • Save uggedal/313926 to your computer and use it in GitHub Desktop.

Select an option

Save uggedal/313926 to your computer and use it in GitHub Desktop.
-- Return keys with a given prefix for a particular node where there are a
-- known total of nodes. Uses the reamainder of the keys' hash values to
-- segment the keys.
function slice(prefix, index_and_total)
local args = _split(index_and_total, "\t")
if #args ~= 2 then
return nil
end
local index = tonumber(args[1])
local total = tonumber(args[2])
local res = {}
local keys = _fwmkeys(prefix)
for i = 1, #keys do
local key = keys[i]
local url = string.sub(
key,
string.find(key, ":", string.find(key, ":", 1, true) + 1, true) + 1
)
local modulo = tonumber(string.sub(_hash("md5", url), 1, 10), 16) % total
if modulo == index then
table.insert(res, key)
end
end
if #res == 0 then
return nil
end
return table.concat(res, "\t")
end
-- Retrieves a given key if its timestamp is fresher than cutoff.
-- If the key is non found or the timestamp is older than cutoff,
-- nil is returned.
function get_cache(key, cutoff)
upto, upto_length = _process_timestamp(cutoff)
if not upto then return nil end
return _get_cache(key, upto, upto_length)
end
-- Compact all cached items having keys starting with prefix and
-- timestamp older than cutoff. Returns the number of compacted elements.
function compact_cache(prefix, cutoff)
upto, upto_length = _process_timestamp(cutoff)
if not upto then return nil end
local compactions = 0
local keys = _fwmkeys(prefix)
for i = 1, #keys do
local key = keys[i]
if not _get_cache(key, upto, upto_length) then
compactions = compactions + 1
end
end
return compactions
end
function _process_timestamp(cutoff)
local upto = tonumber(cutoff)
local upto_length = string.len(cutoff)
if upto and upto_length > 0 then
return upto, upto_length
end
return nil
end
function _get_cache(key, upto, upto_length)
local value = _get(key)
if value then
local stored_at = tonumber(string.sub(value, 1, upto_length))
if stored_at < upto then
if _out(key) then
return nil
end
end
end
return value
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment