Skip to content

Commit

Permalink
perf: do parallel when is possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Sep 15, 2021
1 parent 98b9551 commit fed8ded
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@keyvhq/multicache",
"name": "@keyvhq/multi",
"description": "Layered cache with any backend",
"homepage": "https://keyv.js.org",
"version": "1.2.7",
Expand Down Expand Up @@ -27,10 +27,10 @@
"value"
],
"dependencies": {
"@keyvhq/core": "~1.2.6",
"@keyvhq/keyv-sqlite": "^1.0.0"
"@keyvhq/core": "~1.2.6"
},
"devDependencies": {
"@keyvhq/keyv-sqlite": "latest",
"ava": "latest",
"delay": "latest",
"nyc": "latest"
Expand Down
65 changes: 65 additions & 0 deletions packages/multi/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
'use strict'

const Keyv = require('@keyvhq/core')

class MultiCache {
constructor ({ remote = new Keyv(), local = new Keyv(), ...options }) {
const normalizedOptions = Object.assign(
{
validator: () => true
},
options
)
this.remote = remote
this.local = local

Object.keys(normalizedOptions).forEach(
key => (this[key] = normalizedOptions[key])
)
}

async get (...args) {
let res = await this.local.get(...args)
if (res === undefined || !this.validator(res, ...args)) {
res = await this.remote.get(...args)
}
return res
}

async has (...args) {
let res = await this.local.has(...args)
if (res === false || !this.validator(res, ...args)) {
res = await this.remote.has(...args)
}
return res
}

async set (key, value, ttl) {
await Promise.all(
['local', 'remote'].map(store => this[store].set(key, value, ttl))
)
return true
}

async delete (key, { localOnly = false } = {}) {
await Promise.all(
['local', !localOnly && 'remote']
.filter(Boolean)
.map(store => this[store].delete(key))
)

return true
}

async clear ({ localOnly = false } = {}) {
await Promise.all(
['local', !localOnly && 'remote']
.filter(Boolean)
.map(store => this[store].clear())
)

return true
}
}

module.exports = MultiCache
24 changes: 13 additions & 11 deletions packages/multicache/test/index.js → packages/multi/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const test = require('ava')
const delay = require('delay')

const MultiCache = require('..')
const KeyvMulti = require('..')
const Keyv = require('@keyvhq/core')
const KeyvSqlite = require('@keyvhq/keyv-sqlite')

Expand All @@ -19,14 +19,14 @@ const localStore = () => new Keyv()
test.beforeEach(async () => {
const remote = remoteStore()
const local = localStore()
const store = new MultiCache(remote, local)
const store = new KeyvMulti({ remote, local })
return store.clear()
})

test.serial('.set() sets to both stores', async t => {
const remote = remoteStore()
const local = localStore()
const store = new MultiCache(remote, local)
const store = new KeyvMulti({ remote, local })

await store.set('foo', 'bar')

Expand All @@ -42,7 +42,7 @@ test.serial('.set() sets to both stores', async t => {
test.serial('.has() returns boolean', async t => {
const remote = remoteStore()
const local = localStore()
const store = new MultiCache(remote, local)
const store = new KeyvMulti({ remote, local })

await store.set('foo', 'bar')

Expand All @@ -51,7 +51,7 @@ test.serial('.has() returns boolean', async t => {

test.serial('.has() checks both stores', async t => {
const remote = remoteStore()
const store = new MultiCache(remote)
const store = new KeyvMulti({ remote })

await remote.set('fizz', 'buzz')

Expand All @@ -61,7 +61,7 @@ test.serial('.has() checks both stores', async t => {
test.serial('.delete() deletes both stores', async t => {
const remote = remoteStore()
const local = localStore()
const store = new MultiCache(remote, local)
const store = new KeyvMulti({ remote, local })

await store.set('fizz', 'buzz')
await store.delete('fizz')
Expand All @@ -76,7 +76,7 @@ test.serial(
async t => {
const remote = remoteStore()
const local = localStore()
const store = new MultiCache(remote, local)
const store = new KeyvMulti({ remote, local })

await store.set('fizz', 'buzz')
await store.delete('fizz', { localOnly: true })
Expand All @@ -90,7 +90,7 @@ test.serial(
test.serial('.clear() clears both stores', async t => {
const remote = remoteStore()
const local = localStore()
const store = new MultiCache(remote, local)
const store = new KeyvMulti({ remote, local })

await store.set('fizz', 'buzz')
await store.clear()
Expand All @@ -101,7 +101,7 @@ test.serial('.clear() clears both stores', async t => {
test.serial('.clear({ localOnly: true }) clears local store alone', async t => {
const remote = remoteStore()
const local = localStore()
const store = new MultiCache(remote, local)
const store = new KeyvMulti({ remote, local })

await store.set('fizz', 'buzz')
await store.clear({ localOnly: true })
Expand All @@ -114,7 +114,7 @@ test.serial('.clear({ localOnly: true }) clears local store alone', async t => {
test.serial('ttl is valid', async t => {
const remote = remoteStore()
const local = new Keyv({ ttl: 100 }) // set local ttl
const store = new MultiCache(remote, local)
const store = new KeyvMulti({ remote, local })

await store.set('foo', 'bar')
await remote.set('foo', 'notbar')
Expand All @@ -126,7 +126,9 @@ test.serial('ttl is valid', async t => {
test.serial('custom validator', async t => {
const remote = remoteStore()
const local = new Keyv()
const store = new MultiCache(remote, local, {
const store = new KeyvMulti({
remote,
local,
validator: val => {
if (val.timeSensitiveData) return false // fetch from remote store only
return true
Expand Down
57 changes: 0 additions & 57 deletions packages/multicache/src/index.js

This file was deleted.

0 comments on commit fed8ded

Please sign in to comment.