🔀 Merge pull request #200 from agix/redis_expire

Add expire option to redis set
This commit is contained in:
Jan 2020-01-08 08:21:06 -06:00 committed by GitHub
commit 21e8d72311
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -250,6 +250,42 @@ export class Redis implements INodeType {
default: 'automatic', default: 'automatic',
description: 'The type of the key to set.', description: 'The type of the key to set.',
}, },
{
displayName: 'Expire',
name: 'expire',
type: 'boolean',
displayOptions: {
show: {
operation: [
'set'
],
},
},
default: false,
description: 'Set a timeout on key ?',
},
{
displayName: 'TTL',
name: 'ttl',
type: 'number',
typeOptions: {
minValue: 1,
},
displayOptions: {
show: {
operation: [
'set'
],
expire: [
true,
],
},
},
default: 60,
description: 'Number of seconds before key expiration.',
}
] ]
}; };
@ -319,7 +355,7 @@ export class Redis implements INodeType {
} }
async function setValue(client: redis.RedisClient, keyName: string, value: string | number | object | string[] | number[], type?: string) { async function setValue(client: redis.RedisClient, keyName: string, value: string | number | object | string[] | number[], expire: boolean, ttl: number, type?: string) {
if (type === undefined || type === 'automatic') { if (type === undefined || type === 'automatic') {
// Request the type first // Request the type first
if (typeof value === 'string') { if (typeof value === 'string') {
@ -335,20 +371,24 @@ export class Redis implements INodeType {
if (type === 'string') { if (type === 'string') {
const clientSet = util.promisify(client.set).bind(client); const clientSet = util.promisify(client.set).bind(client);
return await clientSet(keyName, value.toString()); await clientSet(keyName, value.toString());
} else if (type === 'hash') { } else if (type === 'hash') {
const clientHset = util.promisify(client.hset).bind(client); const clientHset = util.promisify(client.hset).bind(client);
for (const key of Object.keys(value)) { for (const key of Object.keys(value)) {
await clientHset(keyName, key, (value as IDataObject)[key]!.toString()); await clientHset(keyName, key, (value as IDataObject)[key]!.toString());
} }
return;
} else if (type === 'list') { } else if (type === 'list') {
const clientLset = util.promisify(client.lset).bind(client); const clientLset = util.promisify(client.lset).bind(client);
for (let index = 0; index < (value as string[]).length; index++) { for (let index = 0; index < (value as string[]).length; index++) {
await clientLset(keyName, index, (value as IDataObject)[index]!.toString()); await clientLset(keyName, index, (value as IDataObject)[index]!.toString());
} }
return;
} }
if (expire === true) {
const clientExpire = util.promisify(client.expire).bind(client);
await clientExpire(keyName, ttl);
}
return;
} }
@ -434,8 +474,10 @@ export class Redis implements INodeType {
const keySet = this.getNodeParameter('key', itemIndex) as string; const keySet = this.getNodeParameter('key', itemIndex) as string;
const value = this.getNodeParameter('value', itemIndex) as string; const value = this.getNodeParameter('value', itemIndex) as string;
const keyType = this.getNodeParameter('keyType', itemIndex) as string; const keyType = this.getNodeParameter('keyType', itemIndex) as string;
const expire = this.getNodeParameter('expire', itemIndex, false) as boolean;
const ttl = this.getNodeParameter('ttl', itemIndex, -1) as number;
await setValue(client, keySet, value, keyType); await setValue(client, keySet, value, expire, ttl, keyType);
returnItems.push(items[itemIndex]); returnItems.push(items[itemIndex]);
} }
} }