index.js 939 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict'
  2. /**
  3. * @module storage/lib/chunk_buffer
  4. */
  5. const chunkStore = require('../chunk_store')
  6. const redisBackend = require('../chunk_store/redis')
  7. const metrics = require('@overleaf/metrics')
  8. /**
  9. * Load the latest Chunk stored for a project, including blob metadata.
  10. *
  11. * @param {string} projectId
  12. * @return {Promise.<Chunk>}
  13. */
  14. async function loadLatest(projectId) {
  15. const chunkRecord = await chunkStore.loadLatestRaw(projectId)
  16. const cachedChunk = await redisBackend.getCurrentChunkIfValid(
  17. projectId,
  18. chunkRecord
  19. )
  20. if (cachedChunk) {
  21. metrics.inc('chunk_buffer.loadLatest', 1, {
  22. status: 'cache-hit',
  23. })
  24. return cachedChunk
  25. } else {
  26. metrics.inc('chunk_buffer.loadLatest', 1, {
  27. status: 'cache-miss',
  28. })
  29. const chunk = await chunkStore.loadLatest(projectId)
  30. await redisBackend.setCurrentChunk(projectId, chunk)
  31. return chunk
  32. }
  33. }
  34. module.exports = {
  35. loadLatest,
  36. }