|
1 | | -/* |
2 | | -Package redkit provides a comprehensive Redis-compatible server implementation. |
3 | | -
|
4 | | -This file defines all Redis command types and their registration helper functions. |
5 | | -The commands are organized into functional categories following Redis documentation. |
6 | | -
|
7 | | -Command Categories: |
8 | | -- Connection Commands: Basic server communication (PING, ECHO, QUIT) |
9 | | -- String Commands: String operations (GET, SET, INCR, APPEND, etc.) |
10 | | -- Hash Commands: Hash table operations (HGET, HSET, HKEYS, etc.) |
11 | | -- List Commands: List operations (LPUSH, RPOP, LRANGE, etc.) |
12 | | -- Set Commands: Set operations (SADD, SREM, SMEMBERS, etc.) |
13 | | -- Sorted Set Commands: Ordered set operations (ZADD, ZRANGE, ZSCORE, etc.) |
14 | | -- Stream Commands: Stream data structure operations (XADD, XREAD, etc.) |
15 | | -- Bitmap Commands: Bit manipulation (BITCOUNT, SETBIT, GETBIT, etc.) |
16 | | -- HyperLogLog Commands: Probabilistic cardinality estimation (PFADD, PFCOUNT, etc.) |
17 | | -- Geospatial Commands: Geographic operations (GEOADD, GEORADIUS, etc.) |
18 | | -- JSON Commands: JSON data operations (JSON.GET, JSON.SET, etc.) |
19 | | -- Search Commands: Full-text search (FT.SEARCH, FT.CREATE, etc.) |
20 | | -- Time Series Commands: Time-series data (TS.ADD, TS.RANGE, etc.) |
21 | | -- Vector Set Commands: Vector similarity operations (VADD, VSIM, etc.) |
22 | | -- Pub/Sub Commands: Message publishing/subscribing (PUBLISH, SUBSCRIBE, etc.) |
23 | | -- Transaction Commands: Atomic operations (MULTI, EXEC, WATCH, etc.) |
24 | | -- Scripting Commands: Lua script execution (EVAL, EVALSHA, etc.) |
25 | | -- Server Commands: Server management (INFO, CONFIG, SAVE, etc.) |
26 | | -- Cluster Commands: Redis cluster operations (CLUSTER, ASKING, etc.) |
27 | | -- Generic Commands: Key management (DEL, EXISTS, EXPIRE, TTL, etc.) |
28 | | -
|
29 | | -Usage Example: |
30 | | -
|
31 | | - server := redkit.NewServer() |
32 | | -
|
33 | | - // Register custom GET handler |
34 | | - server.registerGetHandler(func(conn *Connection, cmd *Command) RedisValue { |
35 | | - // Custom GET implementation |
36 | | - return RedisValue{Type: SimpleString, Str: "value"} |
37 | | - }) |
38 | | -
|
39 | | - // Start server |
40 | | - server.ListenAndServe(":6379") |
41 | | -
|
42 | | -Each command has a corresponding registration helper function that follows the pattern: |
43 | | -
|
44 | | - register{CommandName}Handler(f func(conn *Connection, cmd *Command) RedisValue) |
45 | | -
|
46 | | -This allows for easy customization and extension of command behavior while maintaining |
47 | | -Redis protocol compatibility. |
48 | | -*/ |
49 | 1 | package redkit |
50 | 2 |
|
51 | | -// CommandType represents Redis command names as typed string constants |
52 | | -// This ensures type safety and provides intellisense support for command names |
| 3 | +// CommandType represents Redis command names |
53 | 4 | type CommandType string |
54 | 5 |
|
55 | | -/* |
56 | | -Redis Command Type Constants |
57 | | -
|
58 | | -All Redis commands are defined as strongly-typed constants to prevent typos |
59 | | -and provide IDE autocompletion. Commands are organized by functional category |
60 | | -matching the official Redis documentation structure. |
61 | | -
|
62 | | -The constants follow the exact Redis command names (case-sensitive) to ensure |
63 | | -protocol compatibility. |
64 | | -*/ |
65 | 6 | const ( |
66 | | - // Connection Commands - Basic server communication |
| 7 | + // Connection Commands |
67 | 8 | PING CommandType = "PING" // Test server connectivity |
68 | 9 | ECHO CommandType = "ECHO" // Echo the given string |
69 | 10 | QUIT CommandType = "QUIT" // Close the connection |
70 | 11 | HELP CommandType = "HELP" // Show help information |
71 | 12 |
|
72 | | - // String Commands - Operations on string values |
73 | | - APPEND CommandType = "APPEND" // Append a value to a key |
74 | | - DECR CommandType = "DECR" // Decrement the integer value of a key by 1 |
75 | | - DECRBY CommandType = "DECRBY" // Decrement the integer value of a key by the given amount |
76 | | - DELEX CommandType = "DELEX" // Delete key based on value comparison |
77 | | - DIGEST CommandType = "DIGEST" // Return hash digest of a string value |
78 | | - GET CommandType = "GET" // Get the value of a key |
79 | | - GETDEL CommandType = "GETDEL" // Get the value of a key and delete the key |
80 | | - GETEX CommandType = "GETEX" // Get the value of a key and set its expiration |
81 | | - GETRANGE CommandType = "GETRANGE" // Get a substring of the string stored at a key |
82 | | - GETSET CommandType = "GETSET" // Set the value of a key and return its old value |
83 | | - INCR CommandType = "INCR" // Increment the integer value of a key by 1 |
84 | | - INCRBY CommandType = "INCRBY" // Increment the integer value of a key by the given amount |
85 | | - INCRBYFLOAT CommandType = "INCRBYFLOAT" // Increment the float value of a key by the given amount |
86 | | - LCS CommandType = "LCS" // Find the longest common substring |
87 | | - MGET CommandType = "MGET" // Get the values of all the given keys |
88 | | - MSET CommandType = "MSET" // Set multiple keys to multiple values |
89 | | - MSETEX CommandType = "MSETEX" // Set multiple keys with expiration time |
90 | | - MSETNX CommandType = "MSETNX" // Set multiple keys to multiple values, only if none exist |
| 13 | + // String Commands |
| 14 | + APPEND CommandType = "APPEND" |
| 15 | + DECR CommandType = "DECR" |
| 16 | + DECRBY CommandType = "DECRBY" |
| 17 | + DELEX CommandType = "DELEX" |
| 18 | + DIGEST CommandType = "DIGEST" |
| 19 | + GET CommandType = "GET" |
| 20 | + GETDEL CommandType = "GETDEL" |
| 21 | + GETEX CommandType = "GETEX" |
| 22 | + GETRANGE CommandType = "GETRANGE" |
| 23 | + GETSET CommandType = "GETSET" |
| 24 | + INCR CommandType = "INCR" |
| 25 | + INCRBY CommandType = "INCRBY" |
| 26 | + INCRBYFLOAT CommandType = "INCRBYFLOAT" |
| 27 | + LCS CommandType = "LCS" |
| 28 | + MGET CommandType = "MGET" |
| 29 | + MSET CommandType = "MSET" |
| 30 | + MSETEX CommandType = "MSETEX" |
| 31 | + MSETNX CommandType = "MSETNX" |
91 | 32 | PSETEX CommandType = "PSETEX" |
92 | 33 | SET CommandType = "SET" |
93 | 34 | SETEX CommandType = "SETEX" |
@@ -440,23 +381,6 @@ const ( |
440 | 381 | WAITAOF CommandType = "WAITAOF" |
441 | 382 | ) |
442 | 383 |
|
443 | | -/* |
444 | | -Default Command Handlers |
445 | | -
|
446 | | -registerDefaultHandlers sets up the basic Redis protocol commands that are |
447 | | -essential for client connectivity and server interaction. These handlers |
448 | | -implement the minimum functionality required for Redis compatibility: |
449 | | -
|
450 | | -- PING: Connectivity testing with optional message echo |
451 | | -- ECHO: Simple string echo for testing |
452 | | -- HELP: Basic command information |
453 | | -- QUIT: Graceful connection termination |
454 | | -
|
455 | | -Custom implementations can override these by registering new handlers |
456 | | -with the same command names, or extend functionality by registering |
457 | | -additional commands. |
458 | | -*/ |
459 | | - |
460 | 384 | // registerDefaultHandlers registers the built-in Redis commands |
461 | 385 | func (s *Server) registerDefaultHandlers() { |
462 | 386 | // PING command |
@@ -494,48 +418,22 @@ func (s *Server) registerDefaultHandlers() { |
494 | 418 | }) |
495 | 419 | } |
496 | 420 |
|
497 | | -/* |
498 | | -Command Registration Helper Functions |
499 | | -
|
500 | | -These functions provide a convenient way to register custom handlers for Redis commands. |
501 | | -Each function follows the pattern: register{CommandName}Handler(handlerFunc) |
502 | | -
|
503 | | -The handler function signature is always: |
504 | | - func(conn *Connection, cmd *Command) RedisValue |
505 | | -
|
506 | | -Where: |
507 | | -- conn: The client connection context |
508 | | -- cmd: The parsed command with arguments |
509 | | -- RedisValue: The response to send back to the client |
510 | | -
|
511 | | -Example usage: |
512 | | - server.registerGetHandler(func(conn *Connection, cmd *Command) RedisValue { |
513 | | - key := cmd.Args[0] |
514 | | - value := myStorage.Get(key) |
515 | | - return RedisValue{Type: BulkString, Bulk: []byte(value)} |
516 | | - }) |
517 | | -*/ |
518 | | - |
519 | 421 | // registerPingHandler registers a custom handler for the PING command |
520 | | -// PING [message] - Test connectivity and optionally echo a message |
521 | 422 | func (s *Server) registerPingHandler(f func(conn *Connection, cmd *Command) RedisValue) { |
522 | 423 | s.RegisterCommandFunc(string(PING), f) |
523 | 424 | } |
524 | 425 |
|
525 | 426 | // registerEchoHandler registers a custom handler for the ECHO command |
526 | | -// ECHO message - Return the given string |
527 | 427 | func (s *Server) registerEchoHandler(f func(conn *Connection, cmd *Command) RedisValue) { |
528 | 428 | s.RegisterCommandFunc(string(ECHO), f) |
529 | 429 | } |
530 | 430 |
|
531 | 431 | // registerQuitHandler registers a custom handler for the QUIT command |
532 | | -// QUIT - Close the connection |
533 | 432 | func (s *Server) registerQuitHandler(f func(conn *Connection, cmd *Command) RedisValue) { |
534 | 433 | s.RegisterCommandFunc(string(QUIT), f) |
535 | 434 | } |
536 | 435 |
|
537 | 436 | // registerHelpHandler registers a custom handler for the HELP command |
538 | | -// HELP - Show available commands and their descriptions |
539 | 437 | func (s *Server) registerHelpHandler(f func(conn *Connection, cmd *Command) RedisValue) { |
540 | 438 | s.RegisterCommandFunc(string(HELP), f) |
541 | 439 | } |
0 commit comments