Skip to content

Commit

Permalink
implements #14
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrodger committed Jul 10, 2020
1 parent 9003bb2 commit 8ebdee5
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 6 deletions.
22 changes: 16 additions & 6 deletions entity-cache.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright © 2012-2019 Richard Rodger and other contributors, MIT License. */
/* Copyright © 2012-2020 Richard Rodger and other contributors, MIT License. */
'use strict'

var LRUCache = require('lru-cache')
Expand Down Expand Up @@ -88,6 +88,10 @@ function entity_cache(options) {
}

++stats.set

ent.cache$ = ent.cache$ || {}
ent.cache$.k = key

return reply(key)
}
)
Expand All @@ -114,8 +118,6 @@ function entity_cache(options) {
) {
var version = result && result.value

// console.log('RESULT', vkey, result)

if (err) {
++stats.cache_errs
return reply(err)
Expand Down Expand Up @@ -156,7 +158,7 @@ function entity_cache(options) {

// Verify id format is compatible

if (!msg.q.id || Object.keys(msg.q).length !== 1) {
if (!msg.q.id || Object.keys(msg.q).length !== 1 || null != msg.q.fields$) {
return load_prior.call(self, msg, reply)
}

Expand All @@ -165,6 +167,7 @@ function entity_cache(options) {
// Lookup version key

var vkey = intern.make_version_key(qent, id, options.prefix)

this.act({ role: 'cache', cmd: 'get' }, { key: vkey }, function(
err,
result
Expand Down Expand Up @@ -195,6 +198,8 @@ function entity_cache(options) {
return reply(err)
}

ent.cache$ = {v:vkey}

writeKey(self, vkey, function(err) {
if (err) {
return reply(err)
Expand All @@ -217,7 +222,9 @@ function entity_cache(options) {

self.log.debug('hit', 'hot', key)
++stats.hot_hit
return reply(qent.make$(record))
var ent = qent.make$(record)
ent.cache$ = {k:key,v:vkey}
return reply(ent)
}

// Entry not found (evicted from hotcache)
Expand All @@ -242,7 +249,9 @@ function entity_cache(options) {
++stats.net_hit
hotcache && hotcache.set(key, ent_data)
self.log.debug('hit', 'net', key)
return reply(qent.make$(ent_data))
var ent = qent.make$(ent_data)
ent.cache$ = { k: key, v: vkey }
return reply(ent)
}

// Not found (upstream)
Expand All @@ -255,6 +264,7 @@ function entity_cache(options) {
return reply(err)
}
return writeData(self, ent, version, function(err) {
ent.cache$.v = vkey
return reply(err || ent)
})
})
Expand Down
82 changes: 82 additions & 0 deletions test/entity-cache.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,45 @@ describe('save()', function() {
})

describe('load()', function() {
it('happy-load', function(done) {
var seneca = seneca_instance()

seneca.ready(function() {
var type = internals.type()
var entry = seneca.make(type, { a: 1, b: 2, c: 3 })

// Save

entry.save$(function(err, saved) {
expect(saved).to.exist()
expect(err).to.not.exist()

seneca
.make(type)
.load$({ id: saved.id },
function(err, loaded) {
expect(err).to.not.exist()
expect(loaded).to.exist()

expect(loaded.data$(false)).equal({
a: 1,
b: 2,
c: 3,
id: saved.id
})

expect(loaded.cache$).exists()

saved.remove$(function(err) {
expect(err).to.not.exist()
done()
})
})
})
})
})


it('handles an object criteria', function(done) {
var seneca = seneca_instance()

Expand All @@ -572,6 +611,8 @@ describe('load()', function() {
expect(err).to.not.exist()
expect(loaded).to.exist()

expect(loaded.cache$).not.exists()

saved.remove$(function(err) {
expect(err).to.not.exist()
done()
Expand All @@ -598,6 +639,8 @@ describe('load()', function() {
expect(err).to.not.exist()
expect(loaded).to.exist()

expect(loaded.cache$).not.exists()

saved.remove$(function(err) {
expect(err).to.not.exist()
done()
Expand All @@ -607,6 +650,45 @@ describe('load()', function() {
})
})


it('skips-fields', function(done) {
var seneca = seneca_instance()

seneca.ready(function() {
var type = internals.type()
var entry = seneca.make(type, { a: 1, b: 2, c: 3 })

// Save

entry.save$(function(err, saved) {
expect(saved).to.exist()
expect(err).to.not.exist()

seneca
.make(type)
.load$({ id: saved.id, fields$:['a','b'] },
function(err, loaded) {
expect(err).to.not.exist()
expect(loaded).to.exist()

expect(loaded.data$(false)).equal({
a: 1,
b: 2,
id: saved.id
})

expect(loaded.cache$).not.exists()

saved.remove$(function(err) {
expect(err).to.not.exist()
done()
})
})
})
})
})


it('handles a number id', function(done) {
var seneca = seneca_instance()

Expand Down

0 comments on commit 8ebdee5

Please sign in to comment.