added cache to learn words manager

This commit is contained in:
Henry Oswald 2015-03-03 14:28:47 +00:00
parent 536e014600
commit 5b0f69a50d
3 changed files with 32 additions and 26 deletions

View file

@ -1,18 +1,11 @@
db = require("./DB")
LRU = require("lru-cache")
cacheOpts =
max: 5000
maxAge: 1000 * 60 * 60
cache = LRU(cacheOpts)
mongoCache = require("./MongoCache")
logger = require 'logger-sharelatex'
metrics = require('metrics-sharelatex')
module.exports = LearnedWordsManager =
learnWord: (user_token, word, callback = (error)->) ->
cache.del(user_token)
mongoCache.del(user_token)
db.spellingPreferences.update {
token: user_token
}, {
@ -22,19 +15,19 @@ module.exports = LearnedWordsManager =
}, callback
getLearnedWords: (user_token, callback = (error, words)->) ->
cachedWords = cache.get(user_token)
if cachedWords
logger.info user_token:user_token, "cache hit"
metrics.inc "cache-hit", 0.1
return callback(null, cachedWords)
mongoCachedWords = mongoCache.get(user_token)
if mongoCachedWords?
logger.info user_token:user_token, "mongoCache hit"
metrics.inc "mongoCache-hit", 0.1
return callback(null, mongoCachedWords)
metrics.inc "cache-miss", 0.1
logger.info user_token:user_token, "cache miss"
metrics.inc "mongoCache-miss", 0.1
logger.info user_token:user_token, "mongoCache miss"
db.spellingPreferences.findOne token: user_token, (error, preferences) ->
return callback error if error?
words = preferences?.learnedWords || []
cache.set(user_token, words)
mongoCache.set(user_token, words)
callback null, words

View file

@ -0,0 +1,8 @@
LRU = require("lru-cache")
cacheOpts =
max: 5000
maxAge: 1000 * 60 * 60
cache = LRU(cacheOpts)
module.exports = cache

View file

@ -3,7 +3,8 @@ chai = require 'chai'
expect = chai.expect
SandboxedModule = require('sandboxed-module')
modulePath = require('path').join __dirname, '../../../app/js/LearnedWordsManager'
assert = require("chai").assert
should = require("chai").should()
describe "LearnedWordsManager", ->
beforeEach ->
@token = "a6b3cd919ge"
@ -11,8 +12,13 @@ describe "LearnedWordsManager", ->
@db =
spellingPreferences:
update: sinon.stub().callsArg(3)
@cache =
get:sinon.stub()
set:sinon.stub()
del:sinon.stub()
@LearnedWordsManager = SandboxedModule.require modulePath, requires:
"./DB" : @db
"./MongoCache":@cache
describe "learnWord", ->
beforeEach ->
@ -49,28 +55,27 @@ describe "LearnedWordsManager", ->
it "should return the word list in the callback", ->
expect(@callback.calledWith null, @wordList).to.equal true
###
describe "caching the result", ->
it 'should use the cache first if it is primed', (done)->
@wordList = ["apples", "bananas", "pears"]
@cache.get.callsArgWith(1, null, learnedWords: @wordList)
@cache.get.returns(@wordList)
@db.spellingPreferences.findOne = sinon.stub()
@LearnedWordsManager.getLearnedWords @token, (err, spellings)=>
@db.spellingPreferences.find.called.should.equal false
@wordList.should.equal spellings
@db.spellingPreferences.findOne.called.should.equal false
assert.deepEqual @wordList, spellings
done()
it 'should set the cache after hitting the db', (done)->
@wordList = ["apples", "bananas", "pears"]
@cache.get.callsArgWith(1)
@db.spellingPreferences.findOne = sinon.stub().callsArgWith(1, null, learnedWords: @wordList)
@LearnedWordsManager.getLearnedWords @token, (err, spellings)=>
@cache.set.calledWith(@token, learnedWords:@wordList).should.equal true
@cache.set.calledWith(@token, @wordList).should.equal true
done()
it 'should break cache when update is called', (done)->
@word = "instanton"
@LearnedWordsManager.learnWord @token, @word, =>
@cache.break.calledWith(@token).should.equal true
@cache.del.calledWith(@token).should.equal true
done()
###