Sqlcache
Caching middleware for database/sql
Install / Use
/learn @prashanthpai/SqlcacheREADME
sqlcache
sqlcache is a caching middleware for database/sql
that enables existing Go programs to add caching in a declarative way.
It leverages APIs provided by the handy sqlmw
project and is inspired from slonik-interceptor-query-cache.
This liberates your Go program from maintaining imperative code that repeatedly implements the cache-aside pattern. Your program will perceive the database client/driver as a read-through cache.
Tested with PostgreSQL database with pgx as the underlying driver.
Cache backends supported:
It's easy to add other caching backends by implementing the cache.Cacher
interface.
Usage
Create a backend cache instance and install the interceptor:
import (
"database/sql"
"github.com/redis/go-redis/v9"
"github.com/jackc/pgx/v4/stdlib"
"github.com/prashanthpai/sqlcache"
)
func main() {
...
rc := redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: []string{"127.0.0.1:6379"},
})
// create a sqlcache.Interceptor instance with the desired backend
interceptor, err := sqlcache.NewInterceptor(&sqlcache.Config{
Cache: sqlcache.NewRedis(rc, "sqc"),
})
...
// wrap pgx driver with cache interceptor and register it
sql.Register("pgx-with-cache", interceptor.Driver(stdlib.GetDefaultDriver()))
// open the database using the wrapped driver
db, err := sql.Open("pgx-with-cache", dsn)
...
Caching is controlled using cache attributes which are SQL comments starting
with @cache- prefix. Only queries with cache attributes are cached.
Cache attributes:
|Cache attribute|Description|Required?|Default|
|---|---|---|---|
|@cache-ttl|Number (in seconds) to cache the query for.|Yes|N/A|
|@cache-max-rows|Don't cache if number of rows in query response exceeds this limit.|Yes|N/A|
Example query:
rows, err := db.QueryContext(context.TODO(), `
-- @cache-ttl 30
-- @cache-max-rows 10
SELECT name, pages FROM books WHERE pages > $1`, 100)
See example/main.go for a full working example.
References
Related Skills
oracle
343.3kBest practices for using the oracle CLI (prompt + file bundling, engines, sessions, and file attachment patterns).
xurl
343.3kA CLI tool for making authenticated requests to the X (Twitter) API. Use this skill when you need to post tweets, reply, quote, search, read posts, manage followers, send DMs, upload media, or interact with any X API v2 endpoint.
prose
343.3kOpenProse VM skill pack. Activate on any `prose` command, .prose files, or OpenProse mentions; orchestrates multi-agent workflows.
Command Development
92.1kThis skill should be used when the user asks to "create a slash command", "add a command", "write a custom command", "define command arguments", "use command frontmatter", "organize commands", "create command with file references", "interactive command", "use AskUserQuestion in command", or needs guidance on slash command structure, YAML frontmatter fields, dynamic arguments, bash execution in commands, user interaction patterns, or command development best practices for Claude Code.
