From b0bd65d4f18fec88d1734a5a9a245c4e678bf28a Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Sun, 25 Jan 2026 15:40:53 +1100 Subject: [PATCH] feat: add --hourly and --daily flags to the client This will automatically namespace keys with the current hour/day. --- cmd/cachew/main.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/cmd/cachew/main.go b/cmd/cachew/main.go index b2bcfac..644f38a 100644 --- a/cmd/cachew/main.go +++ b/cmd/cachew/main.go @@ -23,6 +23,8 @@ type CLI struct { URL string `help:"Remote cache server URL." default:"http://127.0.0.1:8080"` Platform bool `help:"Prefix keys with platform ($${os}-$${arch}-)."` + Daily bool `help:"Prefix keys with date ($${YYYY}-$${MM}-$${DD}-). Mutually exclusive with --hourly." xor:"timeprefix"` + Hourly bool `help:"Prefix keys with date and hour ($${YYYY}-$${MM}-$${DD}-$${HH}-). Mutually exclusive with --daily." xor:"timeprefix"` Get GetCmd `cmd:"" help:"Download object from cache." group:"Operations:"` Stat StatCmd `cmd:"" help:"Show metadata for cached object." group:"Operations:"` @@ -194,9 +196,25 @@ func (pk *PlatformKey) String() string { } func (pk *PlatformKey) AfterApply(cli *CLI) error { - if !cli.Platform { - return nil + prefixed := pk.raw + + // Apply platform prefix if enabled + if cli.Platform { + prefixed = fmt.Sprintf("%s-%s-%s", runtime.GOOS, runtime.GOARCH, prefixed) + } + + // Apply time-based prefix if enabled (goes first in final order) + now := time.Now() + if cli.Hourly { + prefixed = now.Format("2006-01-02-15-") + prefixed + } else if cli.Daily { + prefixed = now.Format("2006-01-02-") + prefixed } - prefixed := fmt.Sprintf("%s-%s-%s", runtime.GOOS, runtime.GOARCH, pk.raw) + + // Only print debug if we actually modified the key + if prefixed != pk.raw { + fmt.Fprintf(os.Stderr, "[DEBUG] Key transform: %s -> %s\n", pk.raw, prefixed) //nolint:forbidigo + } + return errors.WithStack(pk.key.UnmarshalText([]byte(prefixed))) }