@@ -13,12 +13,23 @@ import (
1313)
1414
1515const (
16- // expiretimeReplyNoExpiration is returned by [Miniredis.cmdExpireTime] if the key exists but has no associated expiration time
16+ // expiretimeReplyNoExpiration is return value for EXPIRETIME and PEXPIRETIME if the key exists but has no associated expiration time
1717 expiretimeReplyNoExpiration = - 1
18- // expiretimeReplyMissingKey is returned by [Miniredis.cmdExpireTime] if the key does not exist
18+ // expiretimeReplyMissingKey is return value for EXPIRETIME and PEXPIRETIME if the key does not exist
1919 expiretimeReplyMissingKey = - 2
2020)
2121
22+ func inSeconds (t time.Time ) int {
23+ return int (t .Unix ())
24+ }
25+
26+ func inMilliSeconds (t time.Time ) int {
27+ // Time.UnixMilli() was added in go 1.17
28+ // return int(t.UnixNano() / 1000000) is limited to dates between year 1678 and 2262
29+ // by using following calculation we extend this time without too much complexity
30+ return int (t .Unix ())* 1000 + t .Nanosecond ()/ 1000000
31+ }
32+
2233// commandsGeneric handles EXPIRE, TTL, PERSIST, &c.
2334func commandsGeneric (m * Miniredis ) {
2435 m .srv .Register ("COPY" , m .cmdCopy )
@@ -27,7 +38,8 @@ func commandsGeneric(m *Miniredis) {
2738 m .srv .Register ("EXISTS" , m .cmdExists )
2839 m .srv .Register ("EXPIRE" , makeCmdExpire (m , false , time .Second ))
2940 m .srv .Register ("EXPIREAT" , makeCmdExpire (m , true , time .Second ))
30- m .srv .Register ("EXPIRETIME" , m .cmdExpireTime )
41+ m .srv .Register ("EXPIRETIME" , m .makeCmdExpireTime (inSeconds ))
42+ m .srv .Register ("PEXPIRETIME" , m .makeCmdExpireTime (inMilliSeconds ))
3143 m .srv .Register ("KEYS" , m .cmdKeys )
3244 // MIGRATE
3345 m .srv .Register ("MOVE" , m .cmdMove )
@@ -153,41 +165,45 @@ func makeCmdExpire(m *Miniredis, unix bool, d time.Duration) func(*server.Peer,
153165 }
154166}
155167
156- // cmdExpireTime returns the absolute Unix timestamp (since January 1, 1970) in seconds at which the given key will expire.
157- // See [redis documentation].
168+ // makeCmdExpireTime creates server command function that returns the absolute Unix timestamp (since January 1, 1970)
169+ // at which the given key will expire, in unit selected by time result strategy (e.g. seconds, milliseconds).
170+ // For more information see redis documentation for [expiretime] and [pexpiretime].
158171//
159- // [redis documentation]: https://redis.io/commands/expiretime/
160- func (m * Miniredis ) cmdExpireTime (c * server.Peer , cmd string , args []string ) {
161- if len (args ) != 1 {
162- setDirty (c )
163- c .WriteError (errWrongNumber (cmd ))
164- return
165- }
166-
167- if ! m .handleAuth (c ) {
168- return
169- }
170- if m .checkPubsub (c , cmd ) {
171- return
172- }
173-
174- key := args [0 ]
175- withTx (m , c , func (c * server.Peer , ctx * connCtx ) {
176- db := m .db (ctx .selectedDB )
177-
178- if _ , ok := db .keys [key ]; ! ok {
179- c .WriteInt (expiretimeReplyMissingKey )
172+ // [expiretime]: https://redis.io/commands/expiretime/
173+ // [pexpiretime]: https://redis.io/commands/pexpiretime/
174+ func (m * Miniredis ) makeCmdExpireTime (timeResultStrategy func (time.Time ) int ) server.Cmd {
175+ return func (c * server.Peer , cmd string , args []string ) {
176+ if len (args ) != 1 {
177+ setDirty (c )
178+ c .WriteError (errWrongNumber (cmd ))
180179 return
181180 }
182181
183- ttl , ok := db .ttl [key ]
184- if ! ok {
185- c .WriteInt (expiretimeReplyNoExpiration )
182+ if ! m .handleAuth (c ) {
183+ return
184+ }
185+ if m .checkPubsub (c , cmd ) {
186186 return
187187 }
188188
189- c .WriteInt (int (m .effectiveNow ().Add (ttl ).Unix ()))
190- })
189+ key := args [0 ]
190+ withTx (m , c , func (c * server.Peer , ctx * connCtx ) {
191+ db := m .db (ctx .selectedDB )
192+
193+ if _ , ok := db .keys [key ]; ! ok {
194+ c .WriteInt (expiretimeReplyMissingKey )
195+ return
196+ }
197+
198+ ttl , ok := db .ttl [key ]
199+ if ! ok {
200+ c .WriteInt (expiretimeReplyNoExpiration )
201+ return
202+ }
203+
204+ c .WriteInt (timeResultStrategy (m .effectiveNow ().Add (ttl )))
205+ })
206+ }
191207}
192208
193209// TOUCH
0 commit comments