Skip to content

Commit 459370b

Browse files
committed
Add noop hash func
1 parent 883c607 commit 459370b

3 files changed

Lines changed: 83 additions & 18 deletions

File tree

hash.go

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

hash_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package sqlcache
2+
3+
import (
4+
"database/sql/driver"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestNoopHash(t *testing.T) {
11+
assert := require.New(t)
12+
13+
tcs := []struct {
14+
query string
15+
args []driver.NamedValue
16+
expected string
17+
}{
18+
{
19+
query: `
20+
-- @cache-ttl 5
21+
-- @cache-max-rows 10
22+
SELECT name, pages FROM books WHERE pages > $1
23+
`,
24+
args: []driver.NamedValue{
25+
{
26+
Ordinal: 1,
27+
Value: 10,
28+
},
29+
},
30+
expected: "--@cache-ttl5--@cache-max-rows10SELECTname,pagesFROMbooksWHEREpages>$1:[{ 1 10}]",
31+
},
32+
}
33+
34+
for _, tc := range tcs {
35+
h, err := NoopHash(tc.query, tc.args)
36+
assert.Nil(err)
37+
assert.Equal(tc.expected, h)
38+
}
39+
}

interceptor.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import (
44
"context"
55
"database/sql/driver"
66
"fmt"
7-
"strconv"
87
"sync/atomic"
98
"time"
109

11-
"github.com/mitchellh/hashstructure"
1210
"github.com/ngrok/sqlmw"
1311
"github.com/prashanthpai/sqlcache/cache"
1412
)
@@ -26,6 +24,7 @@ type Config struct {
2624
OnError func(error)
2725
// HashFunc can be optionally set to provide a custom hashing function. By
2826
// default sqlcache uses mitchellh/hashstructure which internally uses FNV.
27+
// If hash collision is a concern to you, consider using NoopHash.
2928
HashFunc func(query string, args []driver.NamedValue) (string, error)
3029
}
3130

@@ -185,22 +184,6 @@ func (i *Interceptor) checkCache(hash string) driver.Rows {
185184
}
186185
}
187186

188-
func defaultHashFunc(query string, args []driver.NamedValue) (string, error) {
189-
u64, err := hashstructure.Hash(struct {
190-
Query string
191-
Args []driver.NamedValue
192-
}{
193-
Query: query,
194-
Args: args,
195-
}, nil)
196-
if err != nil {
197-
return "", err
198-
}
199-
200-
key := fmt.Sprintf("q%da%dh%s", len(query), len(args), strconv.FormatUint(u64, 10))
201-
return key, nil
202-
}
203-
204187
// Stats contains sqlcache statistics.
205188
type Stats struct {
206189
Hits uint64

0 commit comments

Comments
 (0)