|
| 1 | +using System.Runtime.CompilerServices; |
| 2 | +using System.Runtime.InteropServices.ComTypes; |
| 3 | +using RESPite.Messages; |
| 4 | +using StackExchange.Redis; |
| 5 | + |
| 6 | +namespace RESPite.StackExchange.Redis; |
| 7 | + |
| 8 | +internal static partial class RedisCommands |
| 9 | +{ |
| 10 | + // this is just a "type pun" - it should be an invisible/magic pointer cast to the JIT |
| 11 | + public static ref readonly KeyCommands Keys(this in RespContext context) |
| 12 | + => ref Unsafe.As<RespContext, KeyCommands>(ref Unsafe.AsRef(in context)); |
| 13 | +} |
| 14 | + |
| 15 | +public readonly struct KeyCommands(in RespContext context) |
| 16 | +{ |
| 17 | + public readonly RespContext Context = context; // important: this is the only field |
| 18 | +} |
| 19 | + |
| 20 | +internal static partial class KeyCommandsExtensions |
| 21 | +{ |
| 22 | + [RespCommand] |
| 23 | + public static partial RespOperation<bool> Del(this in KeyCommands context, RedisKey key); |
| 24 | + |
| 25 | + [RespCommand] |
| 26 | + public static partial RespOperation<long> Del(this in KeyCommands context, [RespKey] RedisKey[] keys); |
| 27 | + |
| 28 | + [RespCommand] |
| 29 | + public static partial RespOperation<byte[]?> Dump(this in KeyCommands context, RedisKey key); |
| 30 | + |
| 31 | + [RespCommand("object")] |
| 32 | + public static partial RespOperation<string?> ObjectEncoding(this in KeyCommands context, [RespPrefix("ENCODING")] RedisKey key); |
| 33 | + |
| 34 | + [RespCommand("object", Parser = "RespParsers.TimeSpanFromSeconds")] |
| 35 | + public static partial RespOperation<TimeSpan?> ObjectIdleTime(this in KeyCommands context, [RespPrefix("IDLETIME")] RedisKey key); |
| 36 | + |
| 37 | + [RespCommand("object")] |
| 38 | + public static partial RespOperation<long?> ObjectRefCount(this in KeyCommands context, [RespPrefix("REFCOUNT")] RedisKey key); |
| 39 | + |
| 40 | + [RespCommand("object")] |
| 41 | + public static partial RespOperation<long?> ObjectFreq(this in KeyCommands context, [RespPrefix("FREQ")] RedisKey key); |
| 42 | + |
| 43 | + [RespCommand(Parser = "RespParsers.TimeSpanFromSeconds")] |
| 44 | + public static partial RespOperation<TimeSpan?> Ttl(this in KeyCommands context, RedisKey key); |
| 45 | + |
| 46 | + [RespCommand(Parser = "RespParsers.TimeSpanFromMilliseconds")] |
| 47 | + public static partial RespOperation<TimeSpan?> Pttl(this in KeyCommands context, RedisKey key); |
| 48 | + |
| 49 | + [RespCommand(Parser = "RespParsers.DateTimeFromSeconds")] |
| 50 | + public static partial RespOperation<DateTime?> ExpireTime(this in KeyCommands context, RedisKey key); |
| 51 | + |
| 52 | + [RespCommand(Parser = "RespParsers.DateTimeFromMilliseconds")] |
| 53 | + public static partial RespOperation<DateTime?> PExpireTime(this in KeyCommands context, RedisKey key); |
| 54 | + |
| 55 | + [RespCommand] |
| 56 | + public static partial RespOperation<bool> Exists(this in KeyCommands context, RedisKey key); |
| 57 | + |
| 58 | + [RespCommand] |
| 59 | + public static partial RespOperation<bool> Move(this in KeyCommands context, RedisKey key, int db); |
| 60 | + |
| 61 | + [RespCommand] |
| 62 | + public static partial RespOperation<long> Exists(this in KeyCommands context, [RespKey] RedisKey[] keys); |
| 63 | + |
| 64 | + public static RespOperation<bool> Expire(this in KeyCommands context, RedisKey key, TimeSpan? expiry, ExpireWhen when = ExpireWhen.Always) |
| 65 | + { |
| 66 | + if (expiry is null || expiry == TimeSpan.MaxValue) |
| 67 | + { |
| 68 | + if (when != ExpireWhen.Always) Throw(when); |
| 69 | + return Persist(context, key); |
| 70 | + static void Throw(ExpireWhen when) => throw new ArgumentException($"PERSIST cannot be used with {when}."); |
| 71 | + } |
| 72 | + var millis = (long)expiry.GetValueOrDefault().TotalMilliseconds; |
| 73 | + if (millis % 1000 == 0) // use seconds |
| 74 | + { |
| 75 | + return Expire(context, key, millis / 1000, when); |
| 76 | + } |
| 77 | + return PExpire(context, key, millis, when); |
| 78 | + } |
| 79 | + |
| 80 | + public static RespOperation<bool> ExpireAt(this in KeyCommands context, RedisKey key, DateTime? expiry, ExpireWhen when = ExpireWhen.Always) |
| 81 | + { |
| 82 | + if (expiry is null || expiry == DateTime.MaxValue) |
| 83 | + { |
| 84 | + if (when != ExpireWhen.Always) Throw(when); |
| 85 | + return Persist(context, key); |
| 86 | + static void Throw(ExpireWhen when) => throw new ArgumentException($"PERSIST cannot be used with {when}."); |
| 87 | + } |
| 88 | + var millis = RedisDatabase.GetUnixTimeMilliseconds(expiry.GetValueOrDefault()); |
| 89 | + if (millis % 1000 == 0) // use seconds |
| 90 | + { |
| 91 | + return ExpireAt(context, key, millis / 1000, when); |
| 92 | + } |
| 93 | + return PExpireAt(context, key, millis, when); |
| 94 | + } |
| 95 | + |
| 96 | + [RespCommand] |
| 97 | + public static partial RespOperation<bool> Persist(this in KeyCommands context, RedisKey key); |
| 98 | + |
| 99 | + [RespCommand] |
| 100 | + public static partial RespOperation<bool> Touch(this in KeyCommands context, RedisKey key); |
| 101 | + |
| 102 | + [RespCommand] |
| 103 | + public static partial RespOperation<long> Touch(this in KeyCommands context, [RespKey] RedisKey[] keys); |
| 104 | + |
| 105 | + [RespCommand(Parser = "RedisTypeParser.Instance")] |
| 106 | + public static partial RespOperation<RedisType> Type(this in KeyCommands context, RedisKey key); |
| 107 | + |
| 108 | + private sealed class RedisTypeParser : IRespParser<RedisType> |
| 109 | + { |
| 110 | + public static readonly RedisTypeParser Instance = new(); |
| 111 | + private RedisTypeParser() { } |
| 112 | + |
| 113 | + public RedisType Parse(ref RespReader reader) |
| 114 | + { |
| 115 | + if (reader.IsNull) return RedisType.None; |
| 116 | + if (reader.Is("zset"u8)) return RedisType.SortedSet; |
| 117 | + return reader.ReadEnum(RedisType.Unknown); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + [RespCommand] |
| 122 | + public static partial RespOperation<bool> Rename(this in KeyCommands context, RedisKey key, RedisKey newKey); |
| 123 | + |
| 124 | + [RespCommand(Formatter = "RestoreFormatter.Instance")] |
| 125 | + public static partial RespOperation Restore(this in KeyCommands context, RedisKey key, TimeSpan? ttl, byte[] serializedValue); |
| 126 | + |
| 127 | + private sealed class RestoreFormatter : IRespFormatter<(RedisKey Key, TimeSpan? Ttl, byte[] SerializedValue)> |
| 128 | + { |
| 129 | + public static readonly RestoreFormatter Instance = new(); |
| 130 | + private RestoreFormatter() { } |
| 131 | + |
| 132 | + public void Format( |
| 133 | + scoped ReadOnlySpan<byte> command, |
| 134 | + ref RespWriter writer, |
| 135 | + in (RedisKey Key, TimeSpan? Ttl, byte[] SerializedValue) request) |
| 136 | + { |
| 137 | + writer.WriteCommand(command, 3); |
| 138 | + writer.Write(request.Key); |
| 139 | + if (request.Ttl.HasValue) |
| 140 | + { |
| 141 | + writer.WriteBulkString((long)request.Ttl.Value.TotalMilliseconds); |
| 142 | + } |
| 143 | + else |
| 144 | + { |
| 145 | + writer.WriteRaw("$1\r\n0\r\n"u8); |
| 146 | + } |
| 147 | + writer.WriteBulkString(request.SerializedValue); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + [RespCommand] |
| 152 | + public static partial RespOperation<RedisKey> RandomKey(this in KeyCommands context); |
| 153 | + |
| 154 | + [RespCommand(Formatter = "ExpireFormatter.Instance")] |
| 155 | + public static partial RespOperation<bool> Expire(this in KeyCommands context, RedisKey key, long seconds, ExpireWhen when = ExpireWhen.Always); |
| 156 | + |
| 157 | + [RespCommand(Formatter = "ExpireFormatter.Instance")] |
| 158 | + public static partial RespOperation<bool> PExpire(this in KeyCommands context, RedisKey key, long milliseconds, ExpireWhen when = ExpireWhen.Always); |
| 159 | + |
| 160 | + [RespCommand(Formatter = "ExpireFormatter.Instance")] |
| 161 | + public static partial RespOperation<bool> ExpireAt(this in KeyCommands context, RedisKey key, long seconds, ExpireWhen when = ExpireWhen.Always); |
| 162 | + |
| 163 | + [RespCommand(Formatter = "ExpireFormatter.Instance")] |
| 164 | + public static partial RespOperation<bool> PExpireAt(this in KeyCommands context, RedisKey key, long milliseconds, ExpireWhen when = ExpireWhen.Always); |
| 165 | + |
| 166 | + private sealed class ExpireFormatter : IRespFormatter<(RedisKey Key, long Value, ExpireWhen When)> |
| 167 | + { |
| 168 | + public static readonly ExpireFormatter Instance = new(); |
| 169 | + private ExpireFormatter() { } |
| 170 | + |
| 171 | + public void Format( |
| 172 | + scoped ReadOnlySpan<byte> command, |
| 173 | + ref RespWriter writer, |
| 174 | + in (RedisKey Key, long Value, ExpireWhen When) request) |
| 175 | + { |
| 176 | + writer.WriteCommand(command, request.When == ExpireWhen.Always ? 2 : 3); |
| 177 | + writer.Write(request.Key); |
| 178 | + writer.Write(request.Value); |
| 179 | + switch (request.When) |
| 180 | + { |
| 181 | + case ExpireWhen.Always: |
| 182 | + break; |
| 183 | + case ExpireWhen.HasExpiry: |
| 184 | + writer.WriteRaw("$2\r\nXX\r\n"u8); |
| 185 | + break; |
| 186 | + case ExpireWhen.HasNoExpiry: |
| 187 | + writer.WriteRaw("$2\r\nNX\r\n"u8); |
| 188 | + break; |
| 189 | + case ExpireWhen.GreaterThanCurrentExpiry: |
| 190 | + writer.WriteRaw("$2\r\nGT\r\n"u8); |
| 191 | + break; |
| 192 | + case ExpireWhen.LessThanCurrentExpiry: |
| 193 | + writer.WriteRaw("$2\r\nLT\r\n"u8); |
| 194 | + break; |
| 195 | + default: |
| 196 | + Throw(); |
| 197 | + static void Throw() => throw new ArgumentOutOfRangeException(nameof(request.When)); |
| 198 | + break; |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments