Skip to content

Commit 883c607

Browse files
committed
Remove buffer copy during cached Scan
1 parent c728b81 commit 883c607

4 files changed

Lines changed: 13 additions & 13 deletions

File tree

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
[![codecov](https://codecov.io/gh/prashanthpai/sqlcache/branch/master/graph/badge.svg)](https://codecov.io/gh/prashanthpai/sqlcache)
77
[![MIT license](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://opensource.org/licenses/MIT)
88

9-
sqlcache is an **experimental** caching middleware for `database/sql`. It
10-
leverages APIs provided by the handy [sqlmw](https://github.com/ngrok/sqlmw)
9+
sqlcache is an **experimental** caching middleware for `database/sql`
10+
that enables existing Go programs to add caching in a declarative way.
11+
It leverages APIs provided by the handy [sqlmw](https://github.com/ngrok/sqlmw)
1112
project and is inspired from [slonik-interceptor-query-cache](https://github.com/gajus/slonik-interceptor-query-cache).
13+
1214
This liberates your Go program from maintaining imperative code that
13-
implements the cache-aside pattern. Your program will perceive the
14-
database client/driver as a read-through cache.
15+
repeatedly implements the cache-aside pattern. Your program will perceive
16+
the database client/driver as a read-through cache.
1517

16-
Tested with PostgreSQL database with [pgx](https://github.com/jackc/pgx/tree/master/stdlib) as the driver.
18+
Tested with PostgreSQL database with [pgx](https://github.com/jackc/pgx/tree/master/stdlib)
19+
as the underlying driver.
1720

1821
Cache backends supported:
1922

@@ -77,11 +80,6 @@ rows, err := db.QueryContext(context.TODO(), `
7780
7881
See [example/main.go](example/main.go) for a full working example.
7982
80-
## TODO
81-
82-
* Test against different postgres data types.
83-
* Check if deep copy of buffers can be removed.
84-
8583
### References
8684
8785
* A declarative way to cache PostgreSQL queries using Node.js: a [blog post](https://dev.to/gajus/a-declarative-way-to-cache-postgresql-queries-using-node-js-4fbo) by the author of [Slonik](https://github.com/gajus/slonik).

example/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func run() error {
9191
return fmt.Errorf("db.PingContext() failed: %w", err)
9292
}
9393

94-
for i := 0; i < 3; i++ {
94+
for i := 0; i < 15; i++ {
9595
start := time.Now()
9696
if err := doQuery(db); err != nil {
9797
return fmt.Errorf("doQuery() failed: %w", err)

recorder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (r *rowsRecorder) Next(dest []driver.Value) error {
6666
}
6767

6868
cpy := make([]driver.Value, len(dest))
69-
copy(cpy, dest) // TODO: deep copy required ?
69+
copy(cpy, dest)
7070
r.item.Rows = append(r.item.Rows, cpy)
7171

7272
return err

rows_cached.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ func (r *rowsCached) Next(dest []driver.Value) error {
2222
return io.EOF
2323
}
2424

25-
copy(dest, r.Item.Rows[r.ptr]) // copy required ?
25+
for i := range dest {
26+
dest[i] = r.Item.Rows[r.ptr][i]
27+
}
2628
r.ptr++
2729

2830
return nil

0 commit comments

Comments
 (0)