Caching Slow Database Queries

Why should I cache?

  • Speed up slow queries
  • Reduce the memory and CPU pressure on your database server
  • Add some functionality to your application so if your database goes down (or during planned maintenance), you can provide some limited functionality to your users

How do I cache?

Preliminary Decisions

In-Memory

  • Significantly faster
  • No shared state for horizontal scaling. Each cache is unique to the one running application.

Redis

  • If your application is composed of multiple instances running, they can all share the same cache
  • Provides powerful features such as filtering keys

Create a Key

CreateKeyStruct

type Key struct {
Search string
Page int `json:"page"`
}
var key string = remember.CreateKeyStruct(Key{"golang", 2})

CreateKey

key :=  remember.CreateKey(false, "-", "search-x-y", "search", "golang", 2)// Key will be "search-golang-2"

Initialize the Storage Driver

In-Memory

var ms = memory.NewMemoryStore(10 * time.Minute)

Redis

Create a SlowRetrieve Function

type SlowRetrieve func(ctx context.Context) (interface{}, error)
type Result struct {
Title string
}
slowQuery := func(ctx context.Context) (interface{}, error) { results := []Result{} stmt := `
SELECT title
FROM books
WHERE title LIKE ?
ORDER BY title
LIMIT ?, 20
`
rows, err := db.QueryContext(ctx, stmt, search, (page-1)*20)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var title string
if err := rows.Scan(&title); err != nil {
return nil, err
}
results = append(results, Result{title})
}
return results, nil
}

The Super Bowl

key := remember.CreateKeyStruct(Key{"golang", 2})
exp := 10*time.Minute
results, found, err := remember.Cache(ctx, ms, key, exp, slowQuery, remember.Options{GobRegister: false})return results.([]Result) // Type assert in order to use

Gob Register Errors

License

Project Location

Other Articles

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Learning Swift and iOS Development Part 11 : Inheritance

I will convert figma to html, psd to html, xd to html, sketch to html responsive

Berry Data and CCTIP Reached a Strategic Partnership

How to Build a Slack App to Keep a Slack Channel Topic Locked with Airtable and Standard Library

Why you should consider Graphql for your next Microservice

NestJS: Installation and creation of a monorepo project with CQRS architecture.

Improving your coding skills through Code Katas

July 2018 — F27D report (Happynodes, NEO-Watcher) — Proof of Learning

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
rocketlaunchr.cloud

rocketlaunchr.cloud

More from Medium

Learning Go in 3 minutes

In Golang Passing a Context To Function Performing I/O Can Save From DDOS Attack.

The main idea of ​​implementing a binary tree with Golang

JSON Marshal struct in Go