Skip to content

Commit 10a42fe

Browse files
committed
support hsetex, hpttl, httl, hpexpire and hpexpiretime commands
1 parent 387ce9c commit 10a42fe

File tree

6 files changed

+534
-0
lines changed

6 files changed

+534
-0
lines changed

src/sw/redis++/command.h

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,208 @@ void hset_range(Connection &connection, const StringView &key, Input first, Inpu
792792
connection.send(args);
793793
}
794794

795+
template <typename Input>
796+
void hsetex_keep_ttl_range(Connection &connection,
797+
const StringView &key,
798+
Input first,
799+
Input last,
800+
bool keep_ttl,
801+
HSetExOption opt) {
802+
assert(first != last);
803+
804+
CmdArgs args;
805+
args << "HSETEX" << key;
806+
807+
switch (opt) {
808+
case HSetExOption::FNX:
809+
args << "FNX";
810+
break;
811+
case HSetExOption::FXX:
812+
args << "FXX";
813+
break;
814+
case HSetExOption::ALWAYS:
815+
break;
816+
default:
817+
throw Error("unknown HSetExOption");
818+
}
819+
820+
if (keep_ttl) {
821+
args << "KEEPTTL";
822+
}
823+
824+
auto keys_num = std::distance(first, last);
825+
args << "FIELDS" << keys_num << std::make_pair(first, last);
826+
827+
connection.send(args);
828+
}
829+
830+
template <typename Input>
831+
void hsetex_ttl_range(Connection &connection,
832+
const StringView &key,
833+
Input first,
834+
Input last,
835+
const std::chrono::milliseconds &ttl,
836+
HSetExOption opt) {
837+
assert(first != last);
838+
839+
CmdArgs args;
840+
args << "HSETEX" << key;
841+
842+
switch (opt) {
843+
case HSetExOption::FNX:
844+
args << "FNX";
845+
break;
846+
case HSetExOption::FXX:
847+
args << "FXX";
848+
break;
849+
case HSetExOption::ALWAYS:
850+
break;
851+
default:
852+
throw Error("unknown HSetExOption");
853+
}
854+
855+
args << "PX" << ttl.count();
856+
857+
auto keys_num = std::distance(first, last);
858+
args << "FIELDS" << keys_num << std::make_pair(first, last);
859+
860+
connection.send(args);
861+
}
862+
863+
template <typename Input>
864+
void hsetex_time_point_range(Connection &connection,
865+
const StringView &key,
866+
Input first,
867+
Input last,
868+
const std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> &tp,
869+
HSetExOption opt) {
870+
assert(first != last);
871+
872+
CmdArgs args;
873+
args << "HSETEX" << key;
874+
875+
switch (opt) {
876+
case HSetExOption::FNX:
877+
args << "FNX";
878+
break;
879+
case HSetExOption::FXX:
880+
args << "FXX";
881+
break;
882+
case HSetExOption::ALWAYS:
883+
break;
884+
default:
885+
throw Error("unknown HSetExOption");
886+
}
887+
888+
args << "PXAT" << tp.time_since_epoch().count();
889+
890+
auto keys_num = std::distance(first, last);
891+
args << "FIELDS" << keys_num << std::make_pair(first, last);
892+
893+
connection.send(args);
894+
}
895+
896+
template <typename Input>
897+
void httl_range(Connection &connection,
898+
const StringView &key,
899+
Input first,
900+
Input last) {
901+
assert(first != last);
902+
903+
CmdArgs args;
904+
args << "HTTL" << key << "FIELDS";
905+
906+
auto keys_num = std::distance(first, last);
907+
args << keys_num << std::make_pair(first, last);
908+
909+
connection.send(args);
910+
}
911+
912+
template <typename Input>
913+
void hpttl_range(Connection &connection,
914+
const StringView &key,
915+
Input first,
916+
Input last) {
917+
assert(first != last);
918+
919+
CmdArgs args;
920+
args << "HPTTL" << key << "FIELDS";
921+
922+
auto keys_num = std::distance(first, last);
923+
args << keys_num << std::make_pair(first, last);
924+
925+
connection.send(args);
926+
}
927+
928+
template <typename Input>
929+
void hexpiretime_range(Connection &connection,
930+
const StringView &key,
931+
Input first,
932+
Input last) {
933+
assert(first != last);
934+
935+
CmdArgs args;
936+
args << "HEXPIRETIME" << key << "FIELDS";
937+
938+
auto keys_num = std::distance(first, last);
939+
args << keys_num << std::make_pair(first, last);
940+
941+
connection.send(args);
942+
}
943+
944+
template <typename Input>
945+
void hpexpiretime_range(Connection &connection,
946+
const StringView &key,
947+
Input first,
948+
Input last) {
949+
assert(first != last);
950+
951+
CmdArgs args;
952+
args << "HPEXPIRETIME" << key << "FIELDS";
953+
954+
auto keys_num = std::distance(first, last);
955+
args << keys_num << std::make_pair(first, last);
956+
957+
connection.send(args);
958+
}
959+
960+
template <typename Input>
961+
void hpexpire_range(Connection &connection,
962+
const StringView &key,
963+
Input first,
964+
Input last,
965+
const std::chrono::milliseconds &ttl,
966+
HPExpireOption opt) {
967+
assert(first != last);
968+
969+
CmdArgs args;
970+
args << "HPEXPIRE" << key << ttl.count();
971+
972+
switch (opt) {
973+
case HPExpireOption::NX:
974+
args << "NX";
975+
break;
976+
case HPExpireOption::XX:
977+
args << "XX";
978+
break;
979+
case HPExpireOption::GT:
980+
args << "GT";
981+
break;
982+
case HPExpireOption::LT:
983+
args << "LT";
984+
break;
985+
case HPExpireOption::ALWAYS:
986+
break;
987+
default:
988+
throw Error("unknown hpexpire option");
989+
}
990+
991+
auto keys_num = std::distance(first, last);
992+
args << "FIELDS" << keys_num << std::make_pair(first, last);
993+
994+
connection.send(args);
995+
}
996+
795997
inline void hsetnx(Connection &connection,
796998
const StringView &key,
797999
const StringView &field,

src/sw/redis++/command_options.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,20 @@ struct WithHash : TupleWithType<long long, T> {};
216216

217217
std::string to_string(ListWhence whence);
218218

219+
enum class HSetExOption {
220+
FNX = 0,
221+
FXX,
222+
ALWAYS
223+
};
224+
225+
enum class HPExpireOption {
226+
NX = 0,
227+
XX,
228+
GT,
229+
LT,
230+
ALWAYS
231+
};
232+
219233
}
220234

221235
}

src/sw/redis++/redis.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,57 @@ class Redis {
16751675
return hset(key, il.begin(), il.end());
16761676
}
16771677

1678+
template <typename Input>
1679+
auto hsetex(const StringView &key,
1680+
Input first,
1681+
Input last,
1682+
bool keep_ttl = false,
1683+
HSetExOption opt = HSetExOption::ALWAYS)
1684+
-> typename std::enable_if<!std::is_convertible<Input, StringView>::value, long long>::type;
1685+
1686+
template <typename Input>
1687+
auto hsetex(const StringView &key,
1688+
Input first,
1689+
Input last,
1690+
const std::chrono::milliseconds &ttl,
1691+
HSetExOption opt = HSetExOption::ALWAYS)
1692+
-> typename std::enable_if<!std::is_convertible<Input, StringView>::value, long long>::type;
1693+
1694+
template <typename Input>
1695+
auto hsetex(const StringView &key,
1696+
Input first,
1697+
Input last,
1698+
const std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> &tp,
1699+
HSetExOption opt = HSetExOption::ALWAYS)
1700+
-> typename std::enable_if<!std::is_convertible<Input, StringView>::value, long long>::type;
1701+
1702+
template <typename Input, typename Output>
1703+
void httl(const StringView &key, Input first, Input last, Output output);
1704+
1705+
template <typename Input, typename Output>
1706+
void hpttl(const StringView &key, Input first, Input last, Output output);
1707+
1708+
template <typename Input, typename Output>
1709+
void hexpiretime(const StringView &key, Input first, Input last, Output output);
1710+
1711+
template <typename Input, typename Output>
1712+
void hpexpiretime(const StringView &key, Input first, Input last, Output output);
1713+
1714+
template <typename Input, typename Output>
1715+
void hpexpire(const StringView &key,
1716+
Input first,
1717+
Input last,
1718+
const std::chrono::milliseconds &ttl,
1719+
Output output);
1720+
1721+
template <typename Input, typename Output>
1722+
void hpexpire(const StringView &key,
1723+
Input first,
1724+
Input last,
1725+
const std::chrono::milliseconds &ttl,
1726+
HPExpireOption opt,
1727+
Output output);
1728+
16781729
/// @brief Set hash field to value, only if the given field does not exist.
16791730
/// @param key Key where the hash is stored.
16801731
/// @param field Field.

0 commit comments

Comments
 (0)