Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x | import * as util from "util"; import { Logger } from "../../../logger"; import { IRequest } from "../../../server/request"; import { DataType } from "../../data/data-type"; import { Database } from "../../data/database"; import { DatabaseValue } from "../../data/database-value"; import { RedisToken } from "../../protocol/redis-token"; import { IRespCommand } from "../resp-command"; interface IParameters { ifExists: boolean; ifNotExists: boolean; ttl: number | null; } /** * Available (in this form) since v2.6.12 * * Set key to hold the string value. * If key already holds a value, it is overwritten, * regardless of its type. * * Any previous time to live associated with the key is discarded on successful SET operation. * * EX seconds -- Set the specified expire time, in seconds. * PX milliseconds -- Set the specified expire time, in milliseconds. * NX -- Only set the key if it does not already exist. * XX -- Only set the key if it already exist. * * RETURNS: * Simple string reply: * OK if SET was executed correctly. * Null reply: a Null Bulk Reply is returned if the SET operation was not performed * because the user specified the NX or XX option but the condition was not met. * * Note that XX or NX can be specified multiple times without change in behavior */ export class SetCommand extends IRespCommand { public dbDataType = DataType.STRING public maxParams = 6 public minParams = 2 public name = "set" private logger: Logger = new Logger(module.id); public execSync(request: IRequest, db: Database): RedisToken { this.logger.debug( `${request.getCommand()}.execute(%s)`, ...request.getParams() ); try { const parameters: IParameters = this.parse(request), key: string = request.getParam(0).toString(); this.logger.debug( `Creating key ${key} with parameters %s`, `${parameters}` ); const value: DatabaseValue = this.parseValue( request, parameters ), savedValue = this.saveValue( db, parameters, key, value ); return savedValue && savedValue.toString() === value.toString() ? RedisToken.responseOk() : RedisToken.nullString(); } catch (ex: any) { this.logger.warn( `Exception processing request SET ${request.getParams()}`, ex ); return RedisToken.error(ex.message); } } private parse(request: IRequest): IParameters { const parameters: IParameters = { "ifExists": false, "ifNotExists": false, "ttl": null }; if (request.getLength() > 2) { for (let i = 2; i < request.getLength(); i++) { const option: string = request.getParam(i); if (this.match( "EX", option )) { if (parameters.ttl != null) { throw new Error("ERR syntax error"); } parameters.ttl = this.parseTtl( request, ++i ) * 1000; } else if (this.match( "PX", option )) { if (parameters.ttl != null) { throw new Error("ERR syntax error"); } parameters.ttl = this.parseTtl( request, ++i ); } else if (this.match( "NX", option )) { if (parameters.ifExists) { throw new Error("ERR syntax error"); } parameters.ifNotExists = true; } else if (this.match( "XX", option )) { if (parameters.ifNotExists) { throw new Error("ERR syntax error"); } parameters.ifExists = true; } else { throw new Error("ERR syntax error"); } } } return parameters; } private match(str: string, option: string): boolean { return str.toLowerCase() === option.toString().toLowerCase(); } private parseTtl(request: IRequest, i: number): number { const ttlOption: string = request.getParam(i), value: number = parseInt( ttlOption.toString(), 10 ); if (value < 1) { throw new Error("ERR invalid expire time in set"); } return value; } private parseValue(request: IRequest, parameters: IParameters): DatabaseValue { const value: DatabaseValue = new DatabaseValue( DataType.STRING, request.getParam(1).toString(), parameters.ttl ? new Date().getTime() + parameters.ttl : undefined ); return value; } private saveValue(db: Database, params: IParameters, key: string, value: DatabaseValue): DatabaseValue { let savedValue: DatabaseValue; if (params.ifExists) { savedValue = db.putIfPresent( key, value ); } else if (params.ifNotExists) { savedValue = db.putIfAbsent( key, value ); } else { this.logger.debug(`Setting ${util.inspect(key)} to ${util.inspect(value)}`); savedValue = db.put( key, value ); this.logger.debug(`Returning ${util.inspect(savedValue)}`); } return savedValue; } } |