Skip to content
This repository was archived by the owner on Dec 28, 2022. It is now read-only.

Commit c586d19

Browse files
authored
Allow overriding cache of sessions (#107)
1 parent 1a80420 commit c586d19

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,10 @@ module.exports = class Hypercore extends EventEmitter {
191191

192192
s._passCapabilities(this)
193193

194-
// Pass on the cache unless explicitly disabled.
195-
if (opts.cache !== false) s.cache = this.cache
194+
// Configure the cache unless explicitly disabled.
195+
if (opts.cache !== false) {
196+
s.cache = opts.cache === true || !opts.cache ? this.cache : opts.cache
197+
}
196198

197199
ensureEncryption(s, opts)
198200

test/cache.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const test = require('brittle')
2+
const Xache = require('xache')
23
const { create, replicate } = require('./helpers')
34

45
test('cache', async function (t) {
@@ -11,7 +12,7 @@ test('cache', async function (t) {
1112
t.is(await p, await q, 'blocks are identical')
1213
})
1314

14-
test('session cache', async function (t) {
15+
test('session cache inheritance', async function (t) {
1516
const a = await create({ cache: true })
1617
await a.append(['a', 'b', 'c'])
1718

@@ -35,6 +36,20 @@ test('session cache opt-out', async function (t) {
3536
t.not(await p, await q, 'blocks are not identical')
3637
})
3738

39+
test('session cache override', async function (t) {
40+
const a = await create({ cache: true })
41+
await a.append(['a', 'b', 'c'])
42+
43+
const s = a.session({ cache: new Xache({ maxSize: 64, maxAge: 0 }) })
44+
45+
const p = a.get(0)
46+
const q = s.get(0)
47+
const r = s.get(0)
48+
49+
t.not(await p, await q, 'blocks are not identical')
50+
t.is(await q, await r, 'blocks are identical')
51+
})
52+
3853
test('clear cache on truncate', async function (t) {
3954
const a = await create({ cache: true })
4055
await a.append(['a', 'b', 'c'])

0 commit comments

Comments
 (0)