-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Simone Sanfratello
committed
Nov 15, 2021
1 parent
95f5525
commit 0e826d5
Showing
9 changed files
with
257 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict' | ||
|
||
const createStorage = require('../storage') | ||
const { Cache } = require('../') | ||
const Redis = require('ioredis') | ||
|
||
// TODO | ||
|
||
async function main () { | ||
const redisClient = new Redis() | ||
|
||
const cache = new Cache({ | ||
ttl: 2, // default ttl, in seconds | ||
storage: createStorage('redis', { client: redisClient, log: console }), | ||
onDedupe: (key) => { | ||
console.log('deduped', key) | ||
} | ||
}) | ||
|
||
cache.define('fetchSomething', { | ||
references: (args, key, result) => ['somethings', `some-${result}`] | ||
}, async (k) => { | ||
console.log('query', k) | ||
// query 42 | ||
// query 24 | ||
|
||
return { k } | ||
}) | ||
|
||
const p1 = cache.fetchSomething(42) | ||
const p2 = cache.fetchSomething(24) | ||
const p3 = cache.fetchSomething(42) | ||
|
||
const res = await Promise.all([p1, p2, p3]) | ||
|
||
console.log(res) | ||
} | ||
// [ | ||
// { k: 42 }, | ||
// { k: 24 } | ||
// { k: 42 } | ||
// ] | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
'use strict' | ||
|
||
const { workerData } = require('worker_threads') | ||
const Redis = require('ioredis') | ||
|
||
async function sync (options) { | ||
options = options ? JSON.parse(options) : {} | ||
const db = options.db || 0 | ||
|
||
const redisSubscription = new Redis({ | ||
...options, | ||
db, | ||
connectionName: 'acd-redis-worker-sub', | ||
readOnly: true | ||
}) | ||
|
||
const redisClient = new Redis({ | ||
...options, | ||
db, | ||
connectionName: 'acd-redis-worker-rw' | ||
}) | ||
|
||
try { | ||
const subscription = await redisSubscription.subscribe(`__keyevent@${db}__:expire`) | ||
console.log({ subscription }) | ||
// TODO if subscription !== 1 throw new Error('cant subscribe to redis') | ||
} catch (err) { | ||
console.error(err) | ||
// TODO log.error | ||
throw err | ||
} | ||
|
||
console.log('****************') | ||
|
||
redisSubscription.on('message', (channel, key) => { | ||
clearReferences(redisClient, key) | ||
}) | ||
|
||
// @see https://redis.io/topics/notifications | ||
// redis-cli config set notify-keyspace-events KEA | ||
// redis-cli --csv psubscribe '__key*__:*' | ||
} | ||
|
||
async function clearReferences (redisClient, key) { | ||
console.log(`redis-worker, removed ${key}`) | ||
try { | ||
const references = await redisClient.smembers(`k:${key}`) | ||
|
||
if (!references || references.length < 1) { | ||
console.log('no references for:', key) | ||
return | ||
} | ||
|
||
console.log({ references }) | ||
|
||
const reads = references.map(reference => (['smembers', `r:${reference}`])) | ||
|
||
console.log({ reads }) | ||
|
||
const referencesKeys = await redisClient.pipeline(reads).exec() | ||
|
||
console.log({ referencesKeys }) | ||
|
||
const writes = [] | ||
for (let i = 0; i < referencesKeys.length; i++) { | ||
const rk = referencesKeys[i] | ||
rk[1].forEach(r => { | ||
console.log({ r }) | ||
writes.push(['srem', 'r:' + references[i], key]) | ||
}) | ||
} | ||
writes.push(['del', 'k:' + key]) | ||
|
||
console.log({ writes }) | ||
|
||
await redisClient.pipeline(writes).exec() | ||
} catch (err) { | ||
// TODO log.error | ||
console.error(err) | ||
} | ||
} | ||
|
||
sync(workerData) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.