-
Notifications
You must be signed in to change notification settings - Fork 540
/
MSetPx.cs
51 lines (45 loc) · 1.67 KB
/
MSetPx.cs
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using Garnet.common;
using Garnet.server;
namespace Garnet
{
/// <summary>
/// Functions to implement custom transaction MSETPX - set multiple keys with given expiration in milliseconds
///
/// Format: MSETPX 60000 key1 value1 key2 value2 ...
///
/// Description: Perform a non-transactional multi-set with expiry for the given set of key-value pairs
/// </summary>
sealed class MSetPxTxn : CustomTransactionProcedure
{
/// <summary>
/// No transactional phase, skip Prepare
/// </summary>
public override bool Prepare<TGarnetReadApi>(TGarnetReadApi api, ArgSlice input)
=> false;
/// <summary>
/// Main will not be called because Prepare returns false
/// </summary>
public override void Main<TGarnetApi>(TGarnetApi api, ArgSlice input, ref MemoryResult<byte> output)
=> throw new InvalidOperationException();
/// <summary>
/// Perform the MSETPX operation
/// </summary>
public override void Finalize<TGarnetApi>(TGarnetApi api, ArgSlice input, ref MemoryResult<byte> output)
{
int offset = 0;
// Read expiry
var expiryMs = GetNextArg(input, ref offset);
// Read and set key-value pairs with expiry
ArgSlice key, value;
while ((key = GetNextArg(input, ref offset)).Length > 0)
{
value = GetNextArg(input, ref offset);
api.SETEX(key, value, expiryMs);
}
WriteSimpleString(ref output, "OK");
}
}
}