write cache into cache dir

This commit is contained in:
Henry Oswald 2016-12-13 09:14:09 +00:00
parent 8239bcd0b5
commit a695fd33b2
4 changed files with 17 additions and 6 deletions

View file

@ -5,3 +5,4 @@ app.js
test/UnitTests/js/* test/UnitTests/js/*
node_modules/* node_modules/*
test/unit/js/ test/unit/js/
cache/spell.cache

View file

@ -3,27 +3,32 @@ ASpellWorkerPool = require "./ASpellWorkerPool"
LRU = require "lru-cache" LRU = require "lru-cache"
logger = require 'logger-sharelatex' logger = require 'logger-sharelatex'
fs = require 'fs' fs = require 'fs'
settings = require("settings-sharelatex")
Path = require("path")
cache = LRU(10000) cache = LRU(10000)
OneMinute = 60 * 1000 OneMinute = 60 * 1000
cacheFsPath = Path.resolve(settings.cacheDir, "spell.cache")
cacheFsPathTmp = cacheFsPath + ".tmp"
# load any existing cache # load any existing cache
try try
oldCache = fs.readFileSync "spell.cache" oldCache = fs.readFileSync cacheFsPath
cache.load JSON.parse(oldCache) cache.load JSON.parse(oldCache)
catch err catch err
logger.log {err}, "could not load cache file" logger.log err:err, cacheFsPath:cacheFsPath, "could not load the cache file"
# write the cache every 30 minutes # write the cache every 30 minutes
setInterval () -> setInterval () ->
dump = JSON.stringify cache.dump() dump = JSON.stringify cache.dump()
fs.writeFile "spell.cache.tmp", dump, (err) -> fs.writeFile cacheFsPathTmp, dump, (err) ->
if err? if err?
logger.log {err}, "error writing cache file" logger.log {err}, "error writing cache file"
fs.unlink "spell.cache.tmp" fs.unlink cacheFsPathTmp
else else
fs.rename "spell.cache.tmp", "spell.cache" fs.rename cacheFsPathTmp, cacheFsPath
logger.log {len: dump.length}, "wrote cache file" logger.log {len: dump.length, cacheFsPath:cacheFsPath}, "wrote cache file"
, 30 * OneMinute , 30 * OneMinute
class ASpellRunner class ASpellRunner

1
services/spelling/cache/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.

View file

@ -1,3 +1,5 @@
Path = require('path')
module.exports = Settings = module.exports = Settings =
internal: internal:
spelling: spelling:
@ -12,5 +14,7 @@ module.exports = Settings =
mongo: mongo:
url : 'mongodb://127.0.0.1/sharelatex' url : 'mongodb://127.0.0.1/sharelatex'
cacheDir: Path.resolve "cache"
healthCheckUserId: "53c64d2fd68c8d000010bb5f" healthCheckUserId: "53c64d2fd68c8d000010bb5f"