Skip to content

Commit

Permalink
feat: account cache
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Mar 8, 2017
1 parent 5dc93aa commit 74df207
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 20 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ var removeAccount = require('./lib/accounts/remove')
var accountsOn = require('./lib/accounts/on')

var promiseThen = require('./lib/utils/promise-then')
var cache = require('./lib/utils/cache')

var startListeningToAccountChanges = require('./lib/utils/start-listening-to-account-changes')

function accountApi (options) {
options.PouchDB.plugin(require('pouchdb-users'))
var accountsEmitter = new EventEmitter()
var db = new options.PouchDB(options.usersDb || '_users')
var state = {
db: new options.PouchDB(options.usersDb || '_users'),
db: db,
secret: options.secret,
accountsEmitter: accountsEmitter
accountsEmitter: accountsEmitter,
cache: cache(db)
}

// returns promise
Expand Down
4 changes: 2 additions & 2 deletions lib/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ function account (setupPromise, state, findAccountOptions) {
return setupPromise

.then(function () {
return addTokenToUserDoc(state.db, findAccountOptions, tokenOptions)
return addTokenToUserDoc(state.db, state.cache, findAccountOptions, tokenOptions)
})
},
find: function (id) {
return setupPromise

.then(function () {
return findUserDoc(state.db, findAccountOptions)
return findUserDoc(state.db, state.cache, findAccountOptions)
})

.then(function (userDoc) {
Expand Down
2 changes: 1 addition & 1 deletion lib/accounts/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function addAccount (state, properties, options) {
doc.profile = properties.profile
}

return state.db.put(doc)
return state.cache.set(doc)

.catch(function (error) {
if (error.status === 409) {
Expand Down
2 changes: 1 addition & 1 deletion lib/accounts/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function findAccount (state, idOrObject, options) {
if (!options) {
options = {}
}
return findUserDoc(state.db, idOrObject)
return findUserDoc(state.db, state.cache, idOrObject)

.then(function (doc) {
return toAccount(doc, {
Expand Down
10 changes: 5 additions & 5 deletions lib/accounts/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function updateAccount (state, idOrObject, change, options) {
if (!options) {
options = {}
}
return findUserDoc(state.db, idOrObject)
return findUserDoc(state.db, state.cache, idOrObject)

.then(function (doc) {
var newAccount, docProperties
Expand Down Expand Up @@ -49,7 +49,7 @@ function updateAccount (state, idOrObject, change, options) {
})

// 1) add the new doc
return state.db.put(doc)
return state.cache.set(doc)

.then(function (response) {
doc._rev = response.rev
Expand All @@ -64,7 +64,7 @@ function updateAccount (state, idOrObject, change, options) {
}, oldDoc)

// 2) delete the old doc
return state.db.put(deletedDoc)
return state.cache.set(deletedDoc)
})

.then(() => doc)
Expand All @@ -85,7 +85,7 @@ function updateAccount (state, idOrObject, change, options) {
doc[property] = docProperties[property]
})

return state.db.put(doc)
return state.cache.set(doc)

.then(function (response) {
doc._rev = response.rev
Expand All @@ -99,7 +99,7 @@ function updateAccount (state, idOrObject, change, options) {
if (idOrObject.token) {
promise = promise.then(function () {
delete doc.tokens[idOrObject.token]
return state.db.put(doc)
return state.cache.set(doc)
})
}

Expand Down
6 changes: 3 additions & 3 deletions lib/sessions/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function addSession (state, options) {
var promise

if (options.account.username) {
promise = state.db.get('org.couchdb.user:' + options.account.username)
promise = state.cache.get('org.couchdb.user:' + options.account.username)

.then(function (doc) {
// no auth, skip authentication (act as admin)
Expand Down Expand Up @@ -42,7 +42,7 @@ function addSession (state, options) {
}
})
} else {
promise = findUserDoc(state.db, {
promise = findUserDoc(state.db, state.cache, {
token: options.account.token
})
}
Expand All @@ -66,7 +66,7 @@ function addSession (state, options) {
if (options.account.token) {
promise = promise.then(function () {
delete doc.tokens[options.account.token]
return state.db.put(doc)
return state.cache.set(doc)
})
}

Expand Down
2 changes: 1 addition & 1 deletion lib/sessions/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function findSession (state, id, options) {
}
var username = decodeSessionId(id).name

return state.db.get('org.couchdb.user:' + username)
return state.cache.get('org.couchdb.user:' + username)

.then(function (user) {
if (!internals.isValidSessionId(state.secret, user.salt, id)) {
Expand Down
6 changes: 3 additions & 3 deletions lib/utils/add-token-to-user-doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ var uuid = require('uuid')

var findUserDoc = require('./find-user-doc-by-username-or-id-or-token')

function addTokenToUserDoc (db, account, token) {
return findUserDoc(db, account)
function addTokenToUserDoc (db, cache, account, token) {
return findUserDoc(db, cache, account)

.then(function (userDoc) {
if (!userDoc.tokens) {
Expand All @@ -23,7 +23,7 @@ function addTokenToUserDoc (db, account, token) {

userDoc.tokens[id] = token

return db.put(userDoc)
return cache.set(userDoc)

.then(function (response) {
token.id = id
Expand Down
53 changes: 53 additions & 0 deletions lib/utils/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module.exports = cache

function cache (db) {
var memory = {}

db.changes({
since: 'now',
live: true,
include_docs: true
}).on('change', function (change) {
if (change.deleted) {
delete memory[change.id]
return
}
memory[change.id] = change.doc
})

return {
get: function (key) {
if (memory[key]) {
return Promise.resolve(memory[key])
}

return db.get(key)

.then(function (doc) {
memory[key] = doc
return doc
})
},

set: function (doc) {
return db.put(doc)

.then(function (response) {
if (doc._deleted) {
delete memory[doc._id]
return response
}

doc._rev = response.rev
memory[doc._id] = doc

return response
})
},

unset: function (key) {
delete memory[key]
return Promise.resolve()
}
}
}
4 changes: 2 additions & 2 deletions lib/utils/find-user-doc-by-username-or-id-or-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var PouchDBErrors = require('pouchdb-errors')
var errors = require('../utils/errors')
var isTokenExpired = require('../utils/is-token-expired')

function findUserDoc (db, idOrObject) {
function findUserDoc (db, cache, idOrObject) {
var id = idOrObject
var username
var tokenId
Expand All @@ -16,7 +16,7 @@ function findUserDoc (db, idOrObject) {
}

if (username) {
return db.get('org.couchdb.user:' + username)
return cache.get('org.couchdb.user:' + username)
}

if (id) {
Expand Down

0 comments on commit 74df207

Please sign in to comment.