1 /* 2 * Hunt - A redis client library for D programming language. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.redis.params.SetParams; 13 14 import hunt.redis.params.Params; 15 16 import hunt.collection.ArrayList; 17 import hunt.io.BufferUtils; 18 19 import hunt.redis.util.SafeEncoder; 20 21 import std.conv; 22 23 class SetParams : Params { 24 25 private enum string XX = "xx"; 26 private enum string NX = "nx"; 27 private enum string PX = "px"; 28 private enum string EX = "ex"; 29 30 alias getByteParams = Params.getByteParams; 31 32 this() { 33 } 34 35 static SetParams setParams() { 36 return new SetParams(); 37 } 38 39 /** 40 * Set the specified expire time, in seconds. 41 * @param secondsToExpire 42 * @return SetParams 43 */ 44 SetParams ex(int secondsToExpire) { 45 addParam(EX, secondsToExpire); 46 return this; 47 } 48 49 /** 50 * Set the specified expire time, in milliseconds. 51 * @param millisecondsToExpire 52 * @return SetParams 53 */ 54 SetParams px(long millisecondsToExpire) { 55 addParam(PX, millisecondsToExpire); 56 return this; 57 } 58 59 /** 60 * Only set the key if it does not already exist. 61 * @return SetParams 62 */ 63 SetParams nx() { 64 addParam(NX); 65 return this; 66 } 67 68 /** 69 * Only set the key if it already exist. 70 * @return SetParams 71 */ 72 SetParams xx() { 73 addParam(XX); 74 return this; 75 } 76 77 const(ubyte)[][] getByteParams(const(ubyte)[][] args...) { 78 ArrayList!(const(ubyte)[]) byteParams = new ArrayList!(const(ubyte)[])(); 79 foreach(const(ubyte)[] arg ; args) { 80 byteParams.add(arg); 81 } 82 83 if (contains(NX)) { 84 byteParams.add(SafeEncoder.encode(NX)); 85 } 86 if (contains(XX)) { 87 byteParams.add(SafeEncoder.encode(XX)); 88 } 89 90 if (contains(EX)) { 91 byteParams.add(SafeEncoder.encode(EX)); 92 int v = getParam!int(EX); 93 byteParams.add(SafeEncoder.encode(v.to!string())); 94 } 95 96 if (contains(PX)) { 97 byteParams.add(SafeEncoder.encode(PX)); 98 long v = getParam!long(PX); 99 byteParams.add(SafeEncoder.encode(v.to!string())); 100 } 101 102 return byteParams.toArray(); 103 } 104 105 }