Skip to content

Commit

Permalink
Add opts.cache and share cache between all core sessions (#18)
Browse files Browse the repository at this point in the history
* Add `opts.cache` and share cache between all core sessions

* Trigger Actions

* Allow overriding session cache

* Pass cache opt to namespaces

Co-authored-by: Andrew Osheroff <andrewosh@gmail.com>
  • Loading branch information
2 people authored and mafintosh committed Aug 16, 2022
1 parent 0525fce commit 593921c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
sandbox
coverage
package-lock.json
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const safetyCatch = require('safety-catch')
const crypto = require('hypercore-crypto')
const sodium = require('sodium-universal')
const Hypercore = require('hypercore')
const Xache = require('xache')
const b4a = require('b4a')

const [NS] = crypto.namespace('corestore', 1)
Expand All @@ -20,6 +21,7 @@ module.exports = class Corestore extends EventEmitter {
this.storage = Hypercore.defaultStorage(storage, { lock: PRIMARY_KEY_FILE_NAME })
this.cores = opts._cores || new Map()
this.primaryKey = null
this.cache = !!opts.cache

this._keyStorage = null
this._primaryKey = opts.primaryKey
Expand Down Expand Up @@ -175,6 +177,7 @@ module.exports = class Corestore extends EventEmitter {
encryptionKey: opts.encryptionKey || null,
userData,
auth,
cache: opts.cache,
createIfMissing: !opts._discoveryKey,
keyPair: keyPair && keyPair.publicKey
? {
Expand Down Expand Up @@ -224,6 +227,11 @@ module.exports = class Corestore extends EventEmitter {

get (opts = {}) {
opts = validateGetOptions(opts)

if (opts.cache !== false) {
opts.cache = opts.cache === true || (this.cache && !opts.cache) ? defaultCache() : opts.cache
}

const core = new Hypercore(null, {
...opts,
name: null,
Expand Down Expand Up @@ -279,6 +287,7 @@ module.exports = class Corestore extends EventEmitter {
return new Corestore(this.storage, {
primaryKey: this._opening.then(() => this.primaryKey),
namespace: generateNamespace(this._namespace, name),
cache: this.cache,
_opening: this._opening,
_cores: this.cores,
_streams: this._replicationStreams,
Expand Down Expand Up @@ -355,6 +364,10 @@ function deriveSeed (primaryKey, namespace, name) {
return out
}

function defaultCache () {
return new Xache({ maxSize: 65536, maxAge: 0 })
}

function isStream (s) {
return typeof s === 'object' && s && typeof s.pipe === 'function'
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"hypercore": "next",
"hypercore-crypto": "^3.2.1",
"safety-catch": "^1.0.1",
"sodium-universal": "^3.0.4"
"sodium-universal": "^3.0.4",
"xache": "^1.1.0"
}
}
4 changes: 1 addition & 3 deletions test/all.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { test, configure } = require('brittle')
const test = require('brittle')
const crypto = require('hypercore-crypto')
const ram = require('random-access-memory')
const os = require('os')
Expand All @@ -8,8 +8,6 @@ const sodium = require('sodium-universal')

const Corestore = require('..')

configure({ serial: true })

test('basic get with caching', async function (t) {
const store = new Corestore(ram)
const core1a = store.get({ name: 'core-1' })
Expand Down
46 changes: 46 additions & 0 deletions test/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const test = require('brittle')
const RAM = require('random-access-memory')

const Corestore = require('..')

test('core cache', async function (t) {
const store = new Corestore(RAM, { cache: true })

const core = store.get({ name: 'core' })
await core.append(['a', 'b', 'c'])

const p = core.get(0)
const q = core.get(0)

t.is(await p, await q)
})

test('clear cache on truncate', async function (t) {
const store = new Corestore(RAM, { cache: true })

const core = store.get({ name: 'core' })
await core.append(['a', 'b', 'c'])

const p = core.get(0)

await core.truncate(0)
await core.append('d')

const q = core.get(0)

t.alike(await p, Buffer.from('a'))
t.alike(await q, Buffer.from('d'))
})

test('core cache on namespace', async function (t) {
const store = new Corestore(RAM, { cache: true })
const ns1 = store.namespace('test-namespace-1')

const c1 = store.get({ name: 'test-core' })
const c2 = ns1.get({ name: 'test-core' })

await Promise.all([c1.ready(), c2.ready()])

t.ok(c1.cache)
t.ok(c2.cache)
})

0 comments on commit 593921c

Please sign in to comment.