-
Notifications
You must be signed in to change notification settings - Fork 11
/
hash.go
43 lines (37 loc) · 938 Bytes
/
hash.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package sqlcache
import (
"database/sql/driver"
"fmt"
"strconv"
"strings"
"unicode"
"github.com/mitchellh/hashstructure/v2"
)
func defaultHashFunc(query string, args []driver.NamedValue) (string, error) {
u64, err := hashstructure.Hash(struct {
Query string
Args []driver.NamedValue
}{
Query: query,
Args: args,
}, hashstructure.FormatV2, nil)
if err != nil {
return "", err
}
key := fmt.Sprintf("q%da%dh%s", len(query), len(args), strconv.FormatUint(u64, 10))
return key, nil
}
// NoopHash returns a string representation of the query and args. Whitespaces
// in the query string is stripped off.
func NoopHash(query string, args []driver.NamedValue) (string, error) {
var b strings.Builder
b.Grow(len(query) + len(args)*10) // arbitrary
for _, ch := range query {
if !unicode.IsSpace(ch) {
b.WriteRune(ch)
}
}
b.WriteRune(':')
b.WriteString(fmt.Sprintf("%v", args))
return b.String(), nil
}