Download this file
| 1 | package assetcache |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/binary" |
| 6 | "encoding/hex" |
| 7 | ) |
| 8 | |
| 9 | // Key identifies one cached response: which app generation served it, on |
| 10 | // which host, for which path and query. |
| 11 | type Key struct { |
| 12 | Account, App, Gen, Host, Path, RawQuery string |
| 13 | } |
| 14 | |
| 15 | // ID is the hex sha256 over the six fields. Each field is written with its |
| 16 | // length first, so no two distinct keys hash the same input. |
| 17 | func (k Key) ID() string { |
| 18 | h := sha256.New() |
| 19 | var n [8]byte |
| 20 | for _, f := range [...]string{k.Account, k.App, k.Gen, k.Host, k.Path, k.RawQuery} { |
| 21 | binary.BigEndian.PutUint64(n[:], uint64(len(f))) |
| 22 | h.Write(n[:]) |
| 23 | h.Write([]byte(f)) |
| 24 | } |
| 25 | return hex.EncodeToString(h.Sum(nil)) |
| 26 | } |
| 27 | |
| 28 | // AppScope names the app this key belongs to, for per-app accounting. |
| 29 | func (k Key) AppScope() string { |
| 30 | return k.Account + "/" + k.App |
| 31 | } |
| 32 | |