Skip to content

Commit a67a674

Browse files
committed
open source
0 parents  commit a67a674

16 files changed

Lines changed: 1490 additions & 0 deletions

.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# ignore vendor directory
2+
vendor/
3+
4+
# ignore patches
5+
*.patch
6+
7+
# ctags
8+
tags
9+
10+
# vim/vi
11+
*.swp
12+
*.swo
13+
*~
14+
15+
# Files generated by JetBrains IDEs, e.g. IntelliJ IDEA
16+
.idea/
17+
*.iml
18+
19+
# Binary
20+
example/example

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Prashanth Pai
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# sqlcache
2+
3+
[![Go.Dev reference](https://img.shields.io/badge/go.dev-reference-blue?logo=go&logoColor=white)](https://pkg.go.dev/prashanthpai/sqlcache?tab=doc)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/prashanthpai/sqlcache)](https://goreportcard.com/report/github.com/prashanthpai/sqlcache)
5+
[![MIT license](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://opensource.org/licenses/MIT)
6+
7+
sqlcache is an **experimental** caching middleware for `database/sql`. It
8+
leverages APIs provided by the handy [sqlmw](https://github.com/ngrok/sqlmw)
9+
project and is inspired from [slonik-interceptor-query-cache](https://github.com/gajus/slonik-interceptor-query-cache).
10+
This liberates your Go program from maintaining imperative code that
11+
implements the cache-aside pattern. Your program will perceive the
12+
database client/driver as a read-through cache.
13+
14+
Tested with PostgreSQL database with [pgx](https://github.com/jackc/pgx/tree/master/stdlib) as the driver.
15+
16+
Cache backends supported:
17+
18+
* [ristretto](https://github.com/dgraph-io/ristretto) (in-memory)
19+
* [redis](https://github.com/go-redis/redis)
20+
21+
It's easy to add other caching backends by implementing the `cache.Cacher`
22+
interface.
23+
24+
## Usage
25+
26+
Create a backend cache instance and install the interceptor:
27+
28+
```go
29+
import (
30+
"database/sql"
31+
32+
redis "github.com/go-redis/redis/v7"
33+
"github.com/ngrok/sqlmw"
34+
"github.com/prashanthpai/sqlcache"
35+
)
36+
37+
func main() {
38+
...
39+
rc := redis.NewUniversalClient(&redis.UniversalOptions{
40+
Addrs: []string{"127.0.0.1:6379"},
41+
})
42+
43+
// create a sqlcache.Interceptor instance with the desired backend
44+
interceptor, err := sqlcache.NewInterceptor(&sqlcache.Config{
45+
Cache: sqlcache.NewRedis(rc, "sqc"),
46+
})
47+
...
48+
49+
// wrap pgx driver with the interceptor and register it
50+
sql.Register("pgx-with-cache", sqlmw.Driver(stdlib.GetDefaultDriver(), interceptor))
51+
52+
// open the database using the wrapped driver
53+
db, err := sql.Open("pgx-with-cache", dsn)
54+
...
55+
```
56+
57+
Caching is controlled using cache attributes which are SQL comments starting
58+
with `@cache-` prefix. Only queries with cache attributes are cached.
59+
60+
**Cache attributes:**
61+
62+
|Cache attribute|Description|Required?|Default|
63+
|---|---|---|---|
64+
|`@cache-ttl`|Number (in seconds) to cache the query for.|Yes|N/A|
65+
|`@cache-max-rows`|Don't cache if number of rows in query response exceeds this limit.|Yes|N/A|
66+
67+
Example query:
68+
69+
```go
70+
rows, err := db.QueryContext(context.TODO(), `
71+
-- @cache-ttl 30
72+
-- @cache-max-rows 10
73+
SELECT name, pages FROM books WHERE pages > $1`, 100)
74+
```
75+
76+
See [example/main.go](example/main.go) for a full working example.
77+
78+
## TODO
79+
80+
* Test against different postgres data types.
81+
* Check if deep copy of buffers can be removed.
82+
83+
### References
84+
85+
* 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).
86+
* Declarative Caching with Postgres and Redis: Kyle Davis's [talk](https://youtu.be/IID2LQVztIM?t=1170) on Slonik + Redis.

attr.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+
"regexp"
5+
"strconv"
6+
)
7+
8+
var (
9+
attrRegexp = regexp.MustCompile(`(@cache-ttl|@cache-max-rows) (\d+)`)
10+
)
11+
12+
type attributes struct {
13+
ttl int
14+
maxRows int
15+
}
16+
17+
func getAttrs(query string) *attributes {
18+
matches := attrRegexp.FindAllStringSubmatch(query, 2)
19+
if len(matches) != 2 {
20+
return nil
21+
}
22+
23+
var attrs attributes
24+
for _, match := range matches {
25+
if len(match) != 3 {
26+
return nil
27+
}
28+
switch match[1] {
29+
case "@cache-ttl":
30+
ttl, _ := strconv.Atoi(match[2])
31+
attrs.ttl = ttl
32+
case "@cache-max-rows":
33+
maxRows, _ := strconv.Atoi(match[2])
34+
attrs.maxRows = maxRows
35+
}
36+
}
37+
38+
return &attrs
39+
}

cache/cache.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cache
2+
3+
import (
4+
"database/sql/driver"
5+
"time"
6+
)
7+
8+
// Item represents a single item in cache and will contain the results of a
9+
// single SQL query.
10+
type Item struct {
11+
Cols []string
12+
Rows [][]driver.Value
13+
}
14+
15+
// Cacher represents a backend cache that can be used by sqlcache package.
16+
type Cacher interface {
17+
// Get must return a pointer to the item, a boolean representing whether
18+
// item is present or not, and an error (must be nil when key is not
19+
// present).
20+
Get(key string) (*Item, bool, error)
21+
// Set sets the item into cache with the given TTL.
22+
Set(key string, item *Item, ttl time.Duration) error
23+
}

cache_redis.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package sqlcache
2+
3+
import (
4+
"time"
5+
6+
"github.com/prashanthpai/sqlcache/cache"
7+
8+
redis "github.com/go-redis/redis/v7"
9+
msgpack "github.com/vmihailenco/msgpack/v4"
10+
)
11+
12+
// Redis implements cache.Cacher interface to use redis as backend with
13+
// go-redis as the redis client library.
14+
type Redis struct {
15+
c redis.UniversalClient
16+
keyPrefix string
17+
}
18+
19+
// Get gets a cache item from redis. Returns pointer to the item, a boolean
20+
// which represents whether key exists or not and an error.
21+
func (r *Redis) Get(key string) (*cache.Item, bool, error) {
22+
b, err := r.c.Get(r.keyPrefix + key).Bytes()
23+
switch err {
24+
case nil:
25+
var item cache.Item
26+
if err := msgpack.Unmarshal(b, &item); err != nil {
27+
return nil, true, err
28+
}
29+
return &item, true, nil
30+
case redis.Nil:
31+
return nil, false, nil
32+
default:
33+
return nil, false, err
34+
}
35+
}
36+
37+
// Set sets the given item into redis with provided TTL duration.
38+
func (r *Redis) Set(key string, item *cache.Item, ttl time.Duration) error {
39+
b, err := msgpack.Marshal(item)
40+
if err != nil {
41+
return err
42+
}
43+
44+
_, err = r.c.Set(r.keyPrefix+key, b, ttl).Result()
45+
return err
46+
}
47+
48+
// NewRedis creates a new instance of redis backend using go-redis client.
49+
// All keys created in redis by sqlcache will have start with prefix.
50+
func NewRedis(c redis.UniversalClient, keyPrefix string) *Redis {
51+
return &Redis{
52+
c: c,
53+
keyPrefix: keyPrefix,
54+
}
55+
}

cache_ristretto.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package sqlcache
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/prashanthpai/sqlcache/cache"
8+
9+
"github.com/dgraph-io/ristretto"
10+
)
11+
12+
// Ristretto implements cache.Cacher interface to use ristretto as backend with
13+
// go-redis as the redis client library.
14+
type Ristretto struct {
15+
c *ristretto.Cache
16+
}
17+
18+
// Get gets a cache item from ristretto. Returns pointer to the item, a boolean
19+
// which represents whether key exists or not and an error.
20+
func (r *Ristretto) Get(key string) (*cache.Item, bool, error) {
21+
i, ok := r.c.Get(key)
22+
if !ok {
23+
return nil, false, nil
24+
}
25+
26+
item, ok := i.(*cache.Item)
27+
if !ok {
28+
return nil, false, fmt.Errorf("Ristretto.Get(): i.(*cache.Item) failed")
29+
}
30+
31+
return item, ok, nil
32+
}
33+
34+
// Set sets the given item into ristretto with provided TTL duration.
35+
func (r *Ristretto) Set(key string, item *cache.Item, ttl time.Duration) error {
36+
// using # of rows as cost
37+
_ = r.c.SetWithTTL(key, item, int64(len(item.Rows)), ttl)
38+
return nil
39+
}
40+
41+
// NewRistretto creates a new instance of ristretto backend wrapping the
42+
// provided *ristretto.Cache instance. While creating the ristretto
43+
// instance, please note that number of rows will be used as "cost"
44+
// (in ristretto's terminology) for each cache item.
45+
func NewRistretto(c *ristretto.Cache) *Ristretto {
46+
return &Ristretto{
47+
c: c,
48+
}
49+
}

doc.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Package sqlcache provides an experimental caching middleware for database/sql
3+
users. This liberates your Go program from maintaining imperative code that
4+
implements the cache-aside pattern. Your program will perceive the database
5+
client/driver as a read-through cache.
6+
7+
Usage:
8+
9+
import (
10+
"database/sql"
11+
12+
redis "github.com/go-redis/redis/v7"
13+
"github.com/prashanthpai/sqlcache"
14+
"github.com/ngrok/sqlmw"
15+
)
16+
17+
func main() {
18+
...
19+
rc := redis.NewUniversalClient(&redis.UniversalOptions{
20+
Addrs: []string{"127.0.0.1:6379"},
21+
})
22+
23+
// create a sqlcache.Interceptor instance with the desired backend
24+
interceptor, err := sqlcache.NewInterceptor(&sqlcache.Config{
25+
Cache: sqlcache.NewRedis(rc, "sqc"),
26+
})
27+
...
28+
29+
// wrap pgx driver with the interceptor and register it
30+
sql.Register("pgx-with-cache", sqlmw.Driver(stdlib.GetDefaultDriver(), interceptor))
31+
32+
// open the database using the wrapped driver
33+
db, err := sql.Open("pgx-with-cache", dsn)
34+
...
35+
}
36+
37+
Caching is controlled using cache attributes which are SQL comments starting
38+
with `@cache-` prefix. Only queries with cache attributes are cached.
39+
40+
Example query:
41+
42+
rows, err := db.QueryContext(context.TODO(), `
43+
-- @cache-ttl 30
44+
-- @cache-max-rows 10
45+
SELECT name, pages FROM books WHERE pages > $1`, 100)
46+
*/
47+
package sqlcache

0 commit comments

Comments
 (0)