package assetcache import ( "crypto/sha256" "encoding/binary" "encoding/hex" ) // Key identifies one cached response: which app generation served it, on // which host, for which path and query. type Key struct { Account, App, Gen, Host, Path, RawQuery string } // ID is the hex sha256 over the six fields. Each field is written with its // length first, so no two distinct keys hash the same input. func (k Key) ID() string { h := sha256.New() var n [8]byte for _, f := range [...]string{k.Account, k.App, k.Gen, k.Host, k.Path, k.RawQuery} { binary.BigEndian.PutUint64(n[:], uint64(len(f))) h.Write(n[:]) h.Write([]byte(f)) } return hex.EncodeToString(h.Sum(nil)) } // AppScope names the app this key belongs to, for per-app accounting. func (k Key) AppScope() string { return k.Account + "/" + k.App }