-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* - Added RESTORE functionality * add FIRST_KEY_INDEX, fix tests, clean example, add example to examples table * use returnBuffers in test --------- Co-authored-by: Leibale Eidelman <[email protected]>
- Loading branch information
Showing
5 changed files
with
140 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// This example demonstrates the use of the DUMP and RESTORE commands | ||
|
||
import { commandOptions, createClient } from 'redis'; | ||
|
||
const client = createClient(); | ||
await client.connect(); | ||
|
||
// DUMP a specific key into a local variable | ||
const dump = await client.dump( | ||
commandOptions({ returnBuffers: true }), | ||
'source' | ||
); | ||
|
||
// RESTORE into a new key | ||
await client.restore('destination', 0, dump); | ||
|
||
// RESTORE and REPLACE an existing key | ||
await client.restore('destination', 0, dump, { | ||
REPLACE: true | ||
}); | ||
|
||
await client.quit(); |
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,74 @@ | ||
import { strict as assert } from 'assert'; | ||
import testUtils, { GLOBAL } from '../test-utils'; | ||
import { transformArguments } from './RESTORE'; | ||
|
||
describe('RESTORE', () => { | ||
describe('transformArguments', () => { | ||
it('simple', () => { | ||
assert.deepEqual( | ||
transformArguments('key', 0, 'value'), | ||
['RESTORE', 'key', '0', 'value'] | ||
); | ||
}); | ||
|
||
it('with REPLACE', () => { | ||
assert.deepEqual( | ||
transformArguments('key', 0, 'value', { | ||
REPLACE: true | ||
}), | ||
['RESTORE', 'key', '0', 'value', 'REPLACE'] | ||
); | ||
}); | ||
|
||
it('with ABSTTL', () => { | ||
assert.deepEqual( | ||
transformArguments('key', 0, 'value', { | ||
ABSTTL: true | ||
}), | ||
['RESTORE', 'key', '0', 'value', 'ABSTTL'] | ||
); | ||
}); | ||
|
||
it('with IDLETIME', () => { | ||
assert.deepEqual( | ||
transformArguments('key', 0, 'value', { | ||
IDLETIME: 1 | ||
}), | ||
['RESTORE', 'key', '0', 'value', 'IDLETIME', '1'] | ||
); | ||
}); | ||
|
||
it('with FREQ', () => { | ||
assert.deepEqual( | ||
transformArguments('key', 0, 'value', { | ||
FREQ: 1 | ||
}), | ||
['RESTORE', 'key', '0', 'value', 'FREQ', '1'] | ||
); | ||
}); | ||
|
||
it('with REPLACE, ABSTTL, IDLETIME and FREQ', () => { | ||
assert.deepEqual( | ||
transformArguments('key', 0, 'value', { | ||
REPLACE: true, | ||
ABSTTL: true, | ||
IDLETIME: 1, | ||
FREQ: 2 | ||
}), | ||
['RESTORE', 'key', '0', 'value', 'REPLACE', 'ABSTTL', 'IDLETIME', '1', 'FREQ', '2'] | ||
); | ||
}); | ||
}); | ||
|
||
testUtils.testWithClient('client.restore', async client => { | ||
const [, dump] = await Promise.all([ | ||
client.set('source', 'value'), | ||
client.dump(client.commandOptions({ returnBuffers: true }), 'source') | ||
]); | ||
|
||
assert.equal( | ||
await client.restore('destination', 0, dump), | ||
'OK' | ||
); | ||
}, GLOBAL.SERVERS.OPEN); | ||
}); |
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,39 @@ | ||
import { RedisCommandArgument, RedisCommandArguments } from '.'; | ||
|
||
export const FIRST_KEY_INDEX = 1; | ||
|
||
interface RestoreOptions { | ||
REPLACE?: true; | ||
ABSTTL?: true; | ||
IDLETIME?: number; | ||
FREQ?: number; | ||
} | ||
|
||
export function transformArguments( | ||
key: RedisCommandArgument, | ||
ttl: number, | ||
serializedValue: RedisCommandArgument, | ||
options?: RestoreOptions | ||
): RedisCommandArguments { | ||
const args = ['RESTORE', key, ttl.toString(), serializedValue]; | ||
|
||
if (options?.REPLACE) { | ||
args.push('REPLACE'); | ||
} | ||
|
||
if (options?.ABSTTL) { | ||
args.push('ABSTTL'); | ||
} | ||
|
||
if (options?.IDLETIME) { | ||
args.push('IDLETIME', options.IDLETIME.toString()); | ||
} | ||
|
||
if (options?.FREQ) { | ||
args.push('FREQ', options.FREQ.toString()); | ||
} | ||
|
||
return args; | ||
} | ||
|
||
export declare function transformReply(): 'OK'; |