HosipLan

Untitled

Sep 4th, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.71 KB | None | 0 0
  1.  
  2. local formatKey = function (key, suffix)
  3.     local res = "Nette.Journal:" .. key
  4.     if suffix ~= nil then
  5.         res = res .. ":" .. suffix
  6.     end
  7.  
  8.     return res
  9. end
  10.  
  11. local formatStorageKey = function(key, suffix)
  12.     local res = "Nette.Storage:" .. key
  13.     if suffix ~= nil then
  14.         res = res .. ":" .. suffix
  15.     end
  16.  
  17.     return res
  18. end
  19.  
  20. local priorityEntries = function (priority)
  21.     return redis.call('zRangeByScore', formatKey("priority"), 0, priority)
  22. end
  23.  
  24. local entryTags = function (key)
  25.     return redis.call('sMembers', formatKey(key, "tags"))
  26. end
  27.  
  28. local tagEntries = function (tag)
  29.     return redis.call('sMembers', formatKey(tag, "keys"))
  30. end
  31.  
  32. local cleanEntry = function (keys)
  33.     for i, key in pairs(keys) do
  34.         local tags = entryTags(key)
  35.  
  36.         -- redis.call('multi')
  37.         for i, tag in pairs(tags) do
  38.             redis.call('sRem', formatKey(tag, "keys"), key)
  39.         end
  40.  
  41.         -- drop tags of entry and priority, in case there are some
  42.         redis.call('del', formatKey(key, "tags"), formatKey(key, "priority"))
  43.         redis.call('zRem', formatKey("priority"), key)
  44.  
  45.         -- redis.call('exec')
  46.     end
  47. end
  48.  
  49. local dp = cjson.decode(ARGV[1])
  50.  
  51. -- clean the entry key
  52. cleanEntry({KEYS[1]})
  53.  
  54. -- write the entry key
  55. -- redis.call('multi')
  56.  
  57. -- add entry to each tag & tag to entry
  58. if dp["tags"] ~= nil then
  59.     for i, tag in pairs(dp["tags"]) do
  60.         redis.call('sAdd', formatKey(tag, "keys") , KEYS[1])
  61.         redis.call('sAdd', formatKey(KEYS[1], "tags") , tag)
  62.     end
  63. end
  64.  
  65. if dp["priority"] ~= nil then
  66.     redis.call('zAdd', formatKey("priority"), dp["priority"], KEYS[1])
  67. end
  68.  
  69. -- redis.call('exec')
  70.  
  71. return redis.status_reply("Ok")
Advertisement
Add Comment
Please, Sign In to add comment