-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pubsub): Add shared context among Redis instances (#1110)
* feat(pubsub): add shared context among redis instances, fix #773 * fix: command transformers Co-authored-by: Sibelius Seraphini <sibeliusseraphini@gmail.com>
- Loading branch information
1 parent
cb17f5a
commit 3da06de
Showing
13 changed files
with
363 additions
and
128 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
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 |
---|---|---|
@@ -1,4 +1,20 @@ | ||
import contextMap, { createContext } from '../context'; | ||
import { createData } from '../data'; | ||
import { createExpires } from '../expires'; | ||
|
||
export function flushall() { | ||
this.data.clear(); | ||
const oldContext = contextMap.get(this.keyData); | ||
const newContext = createContext(oldContext.keyPrefix); | ||
|
||
contextMap.set(this.keyData, newContext); | ||
|
||
this.expires = createExpires(newContext.expires, newContext.keyPrefix); | ||
this.data = createData( | ||
newContext.data, | ||
this.expires, | ||
{}, | ||
newContext.keyPrefix | ||
); | ||
|
||
return 'OK'; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export function flushdb() { | ||
this.data.clear(); | ||
this.flushall(); | ||
return 'OK'; | ||
} |
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,19 @@ | ||
import { EventEmitter } from 'events'; | ||
import { createSharedData } from './data'; | ||
import { createSharedExpires } from './expires'; | ||
|
||
const contextMap = new Map(); | ||
|
||
export default contextMap; | ||
|
||
export function createContext(keyPrefix) { | ||
const expires = createSharedExpires(); | ||
|
||
return { | ||
channels: new EventEmitter(), | ||
expires, | ||
data: createSharedData(expires), | ||
patternChannels: new EventEmitter(), | ||
keyPrefix, | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,27 +1,39 @@ | ||
export default function createExpires(keyPrefix = '') { | ||
export function createSharedExpires() { | ||
const expires = {}; | ||
|
||
return Object.freeze({ | ||
get(key) { | ||
return expires[key]; | ||
}, | ||
set(key, timestamp) { | ||
expires[key] = +timestamp; | ||
}, | ||
has(key) { | ||
return {}.hasOwnProperty.call(expires, key); | ||
}, | ||
isExpired(key) { | ||
return expires[key] <= Date.now(); | ||
}, | ||
delete(key) { | ||
delete expires[key]; | ||
}, | ||
}); | ||
} | ||
|
||
export function createExpires(sharedExpires, keyPrefix = '') { | ||
function createInstance(prefix) { | ||
return { | ||
get(key) { | ||
return expires[`${prefix}${key}`]; | ||
}, | ||
set(key, timestamp) { | ||
expires[`${prefix}${key}`] = +timestamp; | ||
}, | ||
has(key) { | ||
return {}.hasOwnProperty.call(expires, `${prefix}${key}`); | ||
}, | ||
isExpired(key) { | ||
return expires[`${prefix}${key}`] <= Date.now(); | ||
}, | ||
delete(key) { | ||
delete expires[`${prefix}${key}`]; | ||
}, | ||
get: (key) => sharedExpires.get(`${prefix}${key}`), | ||
set: (key, timestamp) => sharedExpires.set(`${prefix}${key}`, timestamp), | ||
has: (key) => sharedExpires.has(`${prefix}${key}`), | ||
isExpired: (key) => sharedExpires.isExpired(`${prefix}${key}`), | ||
delete: (key) => sharedExpires.delete(`${prefix}${key}`), | ||
withKeyPrefix(newPrefix) { | ||
if (newPrefix === prefix) return this; | ||
return createInstance(newPrefix); | ||
}, | ||
}; | ||
} | ||
|
||
return createInstance(keyPrefix); | ||
} |
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.