Skip to content

Commit f477e8a

Browse files
authored
feat: add --hourly and --daily flags to the client (#61)
This will automatically namespace keys with the current hour/day.
1 parent 7fa137e commit f477e8a

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

cmd/cachew/main.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type CLI struct {
2323

2424
URL string `help:"Remote cache server URL." default:"http://127.0.0.1:8080"`
2525
Platform bool `help:"Prefix keys with platform ($${os}-$${arch}-)."`
26+
Daily bool `help:"Prefix keys with date ($${YYYY}-$${MM}-$${DD}-). Mutually exclusive with --hourly." xor:"timeprefix"`
27+
Hourly bool `help:"Prefix keys with date and hour ($${YYYY}-$${MM}-$${DD}-$${HH}-). Mutually exclusive with --daily." xor:"timeprefix"`
2628

2729
Get GetCmd `cmd:"" help:"Download object from cache." group:"Operations:"`
2830
Stat StatCmd `cmd:"" help:"Show metadata for cached object." group:"Operations:"`
@@ -194,9 +196,25 @@ func (pk *PlatformKey) String() string {
194196
}
195197

196198
func (pk *PlatformKey) AfterApply(cli *CLI) error {
197-
if !cli.Platform {
198-
return nil
199+
prefixed := pk.raw
200+
201+
// Apply platform prefix if enabled
202+
if cli.Platform {
203+
prefixed = fmt.Sprintf("%s-%s-%s", runtime.GOOS, runtime.GOARCH, prefixed)
204+
}
205+
206+
// Apply time-based prefix if enabled (goes first in final order)
207+
now := time.Now()
208+
if cli.Hourly {
209+
prefixed = now.Format("2006-01-02-15-") + prefixed
210+
} else if cli.Daily {
211+
prefixed = now.Format("2006-01-02-") + prefixed
199212
}
200-
prefixed := fmt.Sprintf("%s-%s-%s", runtime.GOOS, runtime.GOARCH, pk.raw)
213+
214+
// Only print debug if we actually modified the key
215+
if prefixed != pk.raw {
216+
fmt.Fprintf(os.Stderr, "[DEBUG] Key transform: %s -> %s\n", pk.raw, prefixed) //nolint:forbidigo
217+
}
218+
201219
return errors.WithStack(pk.key.UnmarshalText([]byte(prefixed)))
202220
}

0 commit comments

Comments
 (0)