[2.4. Database row caching](https://livebook.manning.com/book/redis-in-action/chapter-2/59)
zput opened this issue · comments
def schedule_row_cache(conn, row_id, delay): conn.zadd('delay:', row_id, delay) #A conn.zadd('schedule:', row_id, time.time()) #B
`def cache_rows(conn):
while not QUIT:
next = conn.zrange('schedule:', 0, 0, withscores=True) #A
now = time.time()
if not next or next[0][1] > now:
time.sleep(.05) #B
continue
row_id = next[0][0]
delay = conn.zscore('delay:', row_id) #C
if delay <= 0:
conn.zrem('delay:', row_id) #D
conn.zrem('schedule:', row_id) #D
conn.delete('inv:' + row_id) #D
continue
row = Inventory.get(row_id) #E
conn.zadd('schedule:', row_id, now + delay) #F
conn.set('inv:' + row_id, json.dumps(row.to_dict())) #F`
commit error. sorry