22using RESPite . Messages ;
33using StackExchange . Redis ;
44
5+ // ReSharper disable MemberCanBePrivate.Global
6+ // ReSharper disable InconsistentNaming
57namespace RESPite . StackExchange . Redis ;
68
79internal static partial class RedisCommands
@@ -41,43 +43,63 @@ public static partial RespOperation<bool> Copy(
4143 [ RespCommand ]
4244 public static partial RespOperation < long > Exists ( this in KeyCommands context , [ RespKey ] RedisKey [ ] keys ) ;
4345
44- public static RespOperation < bool > Expire ( this in KeyCommands context , RedisKey key , TimeSpan ? expiry , ExpireWhen when = ExpireWhen . Always )
46+ public static RespOperation < bool > Expire (
47+ this in KeyCommands context ,
48+ RedisKey key ,
49+ TimeSpan ? expiry ,
50+ ExpireWhen when = ExpireWhen . Always )
4551 {
4652 if ( expiry is null || expiry == TimeSpan . MaxValue )
4753 {
4854 if ( when != ExpireWhen . Always ) Throw ( when ) ;
4955 return Persist ( context , key ) ;
5056 static void Throw ( ExpireWhen when ) => throw new ArgumentException ( $ "PERSIST cannot be used with { when } .") ;
5157 }
58+
5259 var millis = ( long ) expiry . GetValueOrDefault ( ) . TotalMilliseconds ;
5360 if ( millis % 1000 == 0 ) // use seconds
5461 {
5562 return Expire ( context , key , millis / 1000 , when ) ;
5663 }
64+
5765 return PExpire ( context , key , millis , when ) ;
5866 }
5967
60- [ RespCommand ( Formatter = "ExpireFormatter.Instance" ) ]
61- public static partial RespOperation < bool > Expire ( this in KeyCommands context , RedisKey key , long seconds , ExpireWhen when = ExpireWhen . Always ) ;
68+ [ RespCommand ]
69+ public static partial RespOperation < bool > Expire (
70+ this in KeyCommands context ,
71+ RedisKey key ,
72+ long seconds ,
73+ [ RespIgnore ( ExpireWhen . Always ) ] ExpireWhen when = ExpireWhen . Always ) ;
6274
63- public static RespOperation < bool > ExpireAt ( this in KeyCommands context , RedisKey key , DateTime ? expiry , ExpireWhen when = ExpireWhen . Always )
75+ public static RespOperation < bool > ExpireAt (
76+ this in KeyCommands context ,
77+ RedisKey key ,
78+ DateTime ? expiry ,
79+ ExpireWhen when = ExpireWhen . Always )
6480 {
6581 if ( expiry is null || expiry == DateTime . MaxValue )
6682 {
6783 if ( when != ExpireWhen . Always ) Throw ( when ) ;
6884 return Persist ( context , key ) ;
6985 static void Throw ( ExpireWhen when ) => throw new ArgumentException ( $ "PERSIST cannot be used with { when } .") ;
7086 }
87+
7188 var millis = RedisDatabase . GetUnixTimeMilliseconds ( expiry . GetValueOrDefault ( ) ) ;
7289 if ( millis % 1000 == 0 ) // use seconds
7390 {
7491 return ExpireAt ( context , key , millis / 1000 , when ) ;
7592 }
93+
7694 return PExpireAt ( context , key , millis , when ) ;
7795 }
7896
79- [ RespCommand ( Formatter = "ExpireFormatter.Instance" ) ]
80- public static partial RespOperation < bool > ExpireAt ( this in KeyCommands context , RedisKey key , long seconds , ExpireWhen when = ExpireWhen . Always ) ;
97+ [ RespCommand ]
98+ public static partial RespOperation < bool > ExpireAt (
99+ this in KeyCommands context ,
100+ RedisKey key ,
101+ long seconds ,
102+ [ RespIgnore ( ExpireWhen . Always ) ] ExpireWhen when = ExpireWhen . Always ) ;
81103
82104 [ RespCommand ( Parser = "RespParsers.DateTimeFromSeconds" ) ]
83105 public static partial RespOperation < DateTime ? > ExpireTime ( this in KeyCommands context , RedisKey key ) ;
@@ -86,22 +108,38 @@ public static RespOperation<bool> ExpireAt(this in KeyCommands context, RedisKey
86108 public static partial RespOperation < bool > Move ( this in KeyCommands context , RedisKey key , int db ) ;
87109
88110 [ RespCommand ( "object" ) ]
89- public static partial RespOperation < string ? > ObjectEncoding ( this in KeyCommands context , [ RespPrefix ( "ENCODING" ) ] RedisKey key ) ;
111+ public static partial RespOperation < string ? > ObjectEncoding (
112+ this in KeyCommands context ,
113+ [ RespPrefix ( "ENCODING" ) ] RedisKey key ) ;
90114
91115 [ RespCommand ( "object" ) ]
92- public static partial RespOperation < long ? > ObjectFreq ( this in KeyCommands context , [ RespPrefix ( "FREQ" ) ] RedisKey key ) ;
116+ public static partial RespOperation < long ? > ObjectFreq (
117+ this in KeyCommands context ,
118+ [ RespPrefix ( "FREQ" ) ] RedisKey key ) ;
93119
94120 [ RespCommand ( "object" , Parser = "RespParsers.TimeSpanFromSeconds" ) ]
95- public static partial RespOperation < TimeSpan ? > ObjectIdleTime ( this in KeyCommands context , [ RespPrefix ( "IDLETIME" ) ] RedisKey key ) ;
121+ public static partial RespOperation < TimeSpan ? > ObjectIdleTime (
122+ this in KeyCommands context ,
123+ [ RespPrefix ( "IDLETIME" ) ] RedisKey key ) ;
96124
97125 [ RespCommand ( "object" ) ]
98- public static partial RespOperation < long ? > ObjectRefCount ( this in KeyCommands context , [ RespPrefix ( "REFCOUNT" ) ] RedisKey key ) ;
126+ public static partial RespOperation < long ? > ObjectRefCount (
127+ this in KeyCommands context ,
128+ [ RespPrefix ( "REFCOUNT" ) ] RedisKey key ) ;
99129
100- [ RespCommand ( Formatter = "ExpireFormatter.Instance" ) ]
101- public static partial RespOperation < bool > PExpire ( this in KeyCommands context , RedisKey key , long milliseconds , ExpireWhen when = ExpireWhen . Always ) ;
130+ [ RespCommand ]
131+ public static partial RespOperation < bool > PExpire (
132+ this in KeyCommands context ,
133+ RedisKey key ,
134+ long milliseconds ,
135+ [ RespIgnore ( ExpireWhen . Always ) ] ExpireWhen when = ExpireWhen . Always ) ;
102136
103- [ RespCommand ( Formatter = "ExpireFormatter.Instance" ) ]
104- public static partial RespOperation < bool > PExpireAt ( this in KeyCommands context , RedisKey key , long milliseconds , ExpireWhen when = ExpireWhen . Always ) ;
137+ [ RespCommand ]
138+ public static partial RespOperation < bool > PExpireAt (
139+ this in KeyCommands context ,
140+ RedisKey key ,
141+ long milliseconds ,
142+ [ RespIgnore ( ExpireWhen . Always ) ] ExpireWhen when = ExpireWhen . Always ) ;
105143
106144 [ RespCommand ( Parser = "RespParsers.DateTimeFromMilliseconds" ) ]
107145 public static partial RespOperation < DateTime ? > PExpireTime ( this in KeyCommands context , RedisKey key ) ;
@@ -137,7 +175,11 @@ public static RespOperation<bool> Rename(this in KeyCommands context, RedisKey k
137175 public static partial RespOperation < bool > RenameNx ( this in KeyCommands context , RedisKey key , RedisKey newKey ) ;
138176
139177 [ RespCommand ( Formatter = "RestoreFormatter.Instance" ) ]
140- public static partial RespOperation Restore ( this in KeyCommands context , RedisKey key , TimeSpan ? ttl , byte [ ] serializedValue ) ;
178+ public static partial RespOperation Restore (
179+ this in KeyCommands context ,
180+ RedisKey key ,
181+ TimeSpan ? ttl ,
182+ byte [ ] serializedValue ) ;
141183
142184 [ RespCommand ]
143185 public static partial RespOperation < bool > Touch ( this in KeyCommands context , RedisKey key ) ;
@@ -191,43 +233,6 @@ public void Format(
191233 }
192234 }
193235
194- private sealed class ExpireFormatter : IRespFormatter < ( RedisKey Key , long Value , ExpireWhen When ) >
195- {
196- public static readonly ExpireFormatter Instance = new ( ) ;
197- private ExpireFormatter ( ) { }
198-
199- public void Format (
200- scoped ReadOnlySpan < byte > command ,
201- ref RespWriter writer ,
202- in ( RedisKey Key , long Value , ExpireWhen When ) request )
203- {
204- writer . WriteCommand ( command , request . When == ExpireWhen . Always ? 2 : 3 ) ;
205- writer . Write ( request . Key ) ;
206- writer . Write ( request . Value ) ;
207- switch ( request . When )
208- {
209- case ExpireWhen . Always :
210- break ;
211- case ExpireWhen . HasExpiry :
212- writer . WriteRaw ( "$2\r \n XX\r \n "u8 ) ;
213- break ;
214- case ExpireWhen . HasNoExpiry :
215- writer . WriteRaw ( "$2\r \n NX\r \n "u8 ) ;
216- break ;
217- case ExpireWhen . GreaterThanCurrentExpiry :
218- writer . WriteRaw ( "$2\r \n GT\r \n "u8 ) ;
219- break ;
220- case ExpireWhen . LessThanCurrentExpiry :
221- writer . WriteRaw ( "$2\r \n LT\r \n "u8 ) ;
222- break ;
223- default :
224- Throw ( ) ;
225- static void Throw ( ) => throw new ArgumentOutOfRangeException ( nameof ( request . When ) ) ;
226- break ;
227- }
228- }
229- }
230-
231236 private sealed class RestoreFormatter : IRespFormatter < ( RedisKey Key , TimeSpan ? Ttl , byte [ ] SerializedValue ) >
232237 {
233238 public static readonly RestoreFormatter Instance = new ( ) ;
@@ -248,6 +253,7 @@ public void Format(
248253 {
249254 writer . WriteRaw ( "$1\r \n 0\r \n "u8 ) ;
250255 }
256+
251257 writer . WriteBulkString ( request . SerializedValue ) ;
252258 }
253259 }
0 commit comments