RedisManager.coffee 1.3 KB

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