cacheKey returns a string that can be used as a cache key for a backend search job. if a valid key cannot be calculated it returns an empty string.
(prefix string, tenant string, queryHash uint64, start, end time.Time, meta *backend.BlockMeta, startPage, pagesToSearch int)
| 26 | // cacheKey returns a string that can be used as a cache key for a backend search job. if a valid key cannot be calculated |
| 27 | // it returns an empty string. |
| 28 | func cacheKey(prefix string, tenant string, queryHash uint64, start, end time.Time, meta *backend.BlockMeta, startPage, pagesToSearch int) string { |
| 29 | // if the query hash is 0 we can't cache. this may occur if the user is using the old search api |
| 30 | if queryHash == 0 { |
| 31 | return "" |
| 32 | } |
| 33 | |
| 34 | // unless the search range completely encapsulates the block range we can't cache. this is b/c different search ranges will return different results |
| 35 | // for a given block unless the search range covers the entire block |
| 36 | if !(start.Before(meta.StartTime) && // search start is before block start |
| 37 | end.After(meta.EndTime)) { // search end is after block end |
| 38 | return "" |
| 39 | } |
| 40 | |
| 41 | sb := strings.Builder{} |
| 42 | sb.Grow(len(prefix) + |
| 43 | len(tenant) + |
| 44 | 1 + // : |
| 45 | 20 + // query hash |
| 46 | 1 + // : |
| 47 | 36 + // block id |
| 48 | 1 + // : |
| 49 | 3 + // start page |
| 50 | 1 + // : |
| 51 | 2) // 2 for pages to search |
| 52 | sb.WriteString(prefix) |
| 53 | sb.WriteString(tenant) |
| 54 | sb.WriteString(":") |
| 55 | sb.WriteString(strconv.FormatUint(queryHash, 10)) |
| 56 | sb.WriteString(":") |
| 57 | sb.WriteString(meta.BlockID.String()) |
| 58 | sb.WriteString(":") |
| 59 | sb.WriteString(strconv.Itoa(startPage)) |
| 60 | sb.WriteString(":") |
| 61 | sb.WriteString(strconv.Itoa(pagesToSearch)) |
| 62 | |
| 63 | return sb.String() |
| 64 | } |