mirror of
https://github.com/nalgeon/redka.git
synced 2025-10-19 22:34:35 +08:00
49 lines
989 B
Go
49 lines
989 B
Go
package key
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/nalgeon/redka/internal/core"
|
|
"github.com/nalgeon/redka/internal/parser"
|
|
"github.com/nalgeon/redka/internal/redis"
|
|
)
|
|
|
|
// Sets the expiration time of a key to a Unix timestamp.
|
|
// EXPIREAT key unix-time-seconds
|
|
// https://redis.io/commands/expireat
|
|
type ExpireAt struct {
|
|
redis.BaseCmd
|
|
Key string
|
|
At time.Time
|
|
}
|
|
|
|
func ParseExpireAt(b redis.BaseCmd, multi int) (*ExpireAt, error) {
|
|
cmd := &ExpireAt{BaseCmd: b}
|
|
|
|
var at int
|
|
err := parser.New(
|
|
parser.String(&cmd.Key),
|
|
parser.Int(&at),
|
|
).Required(2).Run(cmd.Args())
|
|
if err != nil {
|
|
return cmd, err
|
|
}
|
|
|
|
cmd.At = time.UnixMilli(int64(multi * at))
|
|
return cmd, nil
|
|
}
|
|
|
|
func (cmd *ExpireAt) Run(w redis.Writer, red redis.Redka) (any, error) {
|
|
err := red.Key().ExpireAt(cmd.Key, cmd.At)
|
|
if err != nil && err != core.ErrNotFound {
|
|
w.WriteError(cmd.Error(err))
|
|
return nil, err
|
|
}
|
|
if err == core.ErrNotFound {
|
|
w.WriteInt(0)
|
|
return false, nil
|
|
}
|
|
w.WriteInt(1)
|
|
return true, nil
|
|
}
|