feat(ttl): added ttl support in dump/restore
This commit is contained in:
parent
d68bc8979b
commit
98793b334a
2 changed files with 16 additions and 4 deletions
14
cmd/dump.go
14
cmd/dump.go
|
|
@ -12,10 +12,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type dumpEntry struct {
|
type dumpEntry struct {
|
||||||
Key string `json:"key"`
|
Key string `json:"key"`
|
||||||
Value string `json:"value"`
|
Value string `json:"value"`
|
||||||
Encoding string `json:"encoding,omitempty"`
|
Encoding string `json:"encoding,omitempty"`
|
||||||
Secret bool `json:"secret,omitempty"`
|
Secret bool `json:"secret,omitempty"`
|
||||||
|
ExpiresAt *int64 `json:"expires_at,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var dumpCmd = &cobra.Command{
|
var dumpCmd = &cobra.Command{
|
||||||
|
|
@ -76,11 +77,16 @@ func dump(cmd *cobra.Command, args []string) error {
|
||||||
if isSecret && !includeSecret {
|
if isSecret && !includeSecret {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
expiresAt := item.ExpiresAt()
|
||||||
if err := item.Value(func(v []byte) error {
|
if err := item.Value(func(v []byte) error {
|
||||||
entry := dumpEntry{
|
entry := dumpEntry{
|
||||||
Key: string(key),
|
Key: string(key),
|
||||||
Secret: isSecret,
|
Secret: isSecret,
|
||||||
}
|
}
|
||||||
|
if expiresAt > 0 {
|
||||||
|
ts := int64(expiresAt)
|
||||||
|
entry.ExpiresAt = &ts
|
||||||
|
}
|
||||||
switch mode {
|
switch mode {
|
||||||
case "base64":
|
case "base64":
|
||||||
encodeBase64(&entry, v)
|
encodeBase64(&entry, v)
|
||||||
|
|
|
||||||
|
|
@ -81,6 +81,12 @@ func restore(cmd *cobra.Command, args []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
writeEntry := badger.NewEntry([]byte(entry.Key), value).WithMeta(entryMeta)
|
writeEntry := badger.NewEntry([]byte(entry.Key), value).WithMeta(entryMeta)
|
||||||
|
if entry.ExpiresAt != nil {
|
||||||
|
if *entry.ExpiresAt < 0 {
|
||||||
|
return fmt.Errorf("line %d: expires_at must be >= 0", lineNo)
|
||||||
|
}
|
||||||
|
writeEntry.ExpiresAt = uint64(*entry.ExpiresAt)
|
||||||
|
}
|
||||||
|
|
||||||
if err := wb.SetEntry(writeEntry); err != nil {
|
if err := wb.SetEntry(writeEntry); err != nil {
|
||||||
return fmt.Errorf("line %d: %w", lineNo, err)
|
return fmt.Errorf("line %d: %w", lineNo, err)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue