-
Notifications
You must be signed in to change notification settings - Fork 669
Expand file tree
/
Copy pathProgram.cs
More file actions
97 lines (82 loc) · 4.25 KB
/
Copy pathProgram.cs
File metadata and controls
97 lines (82 loc) · 4.25 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using Garnet.server;
namespace Garnet
{
/// <summary>
/// Garnet server entry point
/// </summary>
public class Program
{
static void Main(string[] args)
{
try
{
using var server = new GarnetServer(args);
// Optional: register custom extensions
RegisterExtensions(server);
// Start the server
server.Start();
Thread.Sleep(Timeout.Infinite);
}
catch (Exception ex)
{
Console.WriteLine($"Unable to initialize server due to exception: {ex.Message}");
}
}
/// <summary>
/// Register new commands with the server. You can access these commands from clients using
/// commands such as db.Execute in StackExchange.Redis. Example:
/// db.Execute("SETIFPM", key, value, prefix);
/// </summary>
static void RegisterExtensions(GarnetServer server)
{
// Register custom command on raw strings (SETIFPM = "set if prefix match")
// Add RESP command info to registration for command to appear when client runs COMMAND / COMMAND INFO
var setIfPmCmdInfo = new RespCommandsInfo
{
Name = "SETIFPM",
Arity = 4,
FirstKey = 1,
LastKey = 1,
Step = 1,
Flags = RespCommandFlags.DenyOom | RespCommandFlags.Write,
AclCategories = RespAclCategories.String | RespAclCategories.Write,
};
server.Register.NewCommand("SETIFPM", CommandType.ReadModifyWrite, new SetIfPMCustomCommand(), setIfPmCmdInfo);
// Register custom command on raw strings (SETWPIFPGT = "set with prefix, if prefix greater than")
server.Register.NewCommand("SETWPIFPGT", CommandType.ReadModifyWrite, new SetWPIFPGTCustomCommand());
// Register custom command on raw strings (DELIFM = "delete if value matches")
server.Register.NewCommand("DELIFM", CommandType.ReadModifyWrite, new DeleteIfMatchCustomCommand());
// Register custom commands on objects
var factory = new MyDictFactory();
server.Register.NewType(factory);
server.Register.NewCommand("MYDICTSET", CommandType.ReadModifyWrite, factory, new MyDictSet(), new RespCommandsInfo { Arity = 4 });
server.Register.NewCommand("MYDICTGET", CommandType.Read, factory, new MyDictGet(), new RespCommandsInfo { Arity = 3 });
// Register stored procedure to run a transactional command
// Add RESP command info to registration for command to appear when client runs COMMAND / COMMAND INFO
var readWriteTxCmdInfo = new RespCommandsInfo
{
Name = "READWRITETX",
Arity = 4,
FirstKey = 1,
LastKey = 3,
Step = 1,
Flags = RespCommandFlags.DenyOom | RespCommandFlags.Write,
AclCategories = RespAclCategories.Write,
};
server.Register.NewTransactionProc("READWRITETX", () => new ReadWriteTxn(), readWriteTxCmdInfo);
// Register stored procedure to run a transactional command
server.Register.NewTransactionProc("MSETPX", () => new MSetPxTxn());
// Register stored procedure to run a transactional command
server.Register.NewTransactionProc("MGETIFPM", () => new MGetIfPM());
// Register stored procedure to run a non-transactional command
server.Register.NewTransactionProc("GETTWOKEYSNOTXN", () => new GetTwoKeysNoTxn(), new RespCommandsInfo { Arity = 3 });
// Register sample transactional procedures
server.Register.NewTransactionProc("SAMPLEUPDATETX", () => new SampleUpdateTxn(), new RespCommandsInfo { Arity = 9 });
server.Register.NewTransactionProc("SAMPLEDELETETX", () => new SampleDeleteTxn(), new RespCommandsInfo { Arity = 6 });
server.Register.NewProcedure("SUM", () => new Sum());
server.Register.NewProcedure("SETMAINANDOBJECT", () => new SetStringAndList());
}
}
}