RedisManager.coffee 1.2 KB

1234567891011121314151617181920212223242526272829
  1. Settings = require "settings-sharelatex"
  2. redis = require("redis-sharelatex")
  3. rclient = redis.createClient(Settings.redis.web)
  4. rawUpdatesKey = (doc_id) -> "UncompressedHistoryOps:#{doc_id}"
  5. docsWithHistoryOpsKey = (project_id) -> "DocsWithHistoryOps:#{project_id}"
  6. module.exports = RedisManager =
  7. getOldestRawUpdates: (doc_id, batchSize, callback = (error, rawUpdates) ->) ->
  8. key = rawUpdatesKey(doc_id)
  9. rclient.lrange key, 0, batchSize - 1, (error, jsonUpdates) ->
  10. try
  11. rawUpdates = ( JSON.parse(update) for update in jsonUpdates or [] )
  12. catch e
  13. return callback(e)
  14. callback null, rawUpdates
  15. deleteOldestRawUpdates: (project_id, doc_id, batchSize, callback = (error) ->) ->
  16. # It's ok to delete the doc_id from the set here. Even though the list
  17. # of updates may not be empty, we will continue to process it until it is.
  18. multi = rclient.multi()
  19. multi.ltrim rawUpdatesKey(doc_id), batchSize, -1
  20. multi.srem docsWithHistoryOpsKey(project_id), doc_id
  21. multi.exec (error, results) ->
  22. return callback(error) if error?
  23. callback null
  24. getDocIdsWithHistoryOps: (project_id, callback = (error, doc_ids) ->) ->
  25. rclient.smembers docsWithHistoryOpsKey(project_id), callback