Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, since nobody is up to help me, here is a "dumb" solution, in pseudocode.</p> <ol> <li><p>Get random element:</p> <pre><code>function maybe_get_next_item() item_name = SRANDMEMBER "items-set" item_key = "items:" + item_name new_item_count = DECR (item_key) if new_item_count &lt; 0 then LOCK -- As explained in SETNX docs new_item_count = GET (item_key) -- More added while we were locking? if new_item_count and new_item_count &lt; 0 then SREM (item_name) -- No, expire it end UNLOCK end if new_item_count and new_item_count &gt;= 0 then return item_name end return false -- this item not found end function get_next_item() item_name = maybe_get_next_item() while not item_name and (SCARD "items-set" &gt; 0) do item_name = maybe_get_next_item() end return item_name -- false if all items are expended end </code></pre></li> <li><p>Insert new elements</p> <pre><code>function insert_items(item_name, amount) LOCK -- As explained in SETNX docs SADD "items-set" (item_name) INCRBY ("items:" + item_name) amount UNLOCK end </code></pre></li> </ol> <p>Please do suggest a better solution if it exists, I'm still groking Redis, and may miss something obvious.</p> <p>I suspect that <code>LOCK</code>/<code>UNLOCK</code> in <code>insert_items()</code> may be superfluous and can be replaced with <code>MULTI</code>/<code>EXEC</code>, but I think that it is needed for <code>LOCK</code>/<code>UNLOCK</code> in <code>maybe_get_next_item()</code> to work properly (which I do not know how to replace with <code>MULTI</code>/<code>EXEC</code>)...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload