Use in-memory db for cache

This commit is contained in:
Jan-Lukas Else 2020-10-06 20:44:58 +02:00
parent d1ce3ed49c
commit 17d14ca56e
2 changed files with 36 additions and 10 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"database/sql"
"encoding/json" "encoding/json"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -11,9 +12,36 @@ import (
) )
var cacheMutexes map[string]*sync.Mutex var cacheMutexes map[string]*sync.Mutex
var cacheDb *sql.DB
var cacheDbWriteMutex = &sync.Mutex{}
func initCache() { func initCache() (err error) {
cacheMutexes = map[string]*sync.Mutex{} cacheMutexes = map[string]*sync.Mutex{}
cacheDb, err = sql.Open("sqlite3", ":memory:")
if err != nil {
return err
}
tx, err := cacheDb.Begin()
if err != nil {
return
}
_, err = tx.Exec("CREATE TABLE cache (path text not null primary key, time integer, header blob, body blob);")
if err != nil {
return
}
err = tx.Commit()
if err != nil {
return
}
return
}
func startWritingToCacheDb() {
cacheDbWriteMutex.Lock()
}
func finishWritingToCacheDb() {
cacheDbWriteMutex.Unlock()
} }
func cacheMiddleware(next http.Handler) http.Handler { func cacheMiddleware(next http.Handler) http.Handler {
@ -88,7 +116,7 @@ func setCacheHeaders(w http.ResponseWriter, cacheTimeString string, expiresTimeS
func getCache(context context.Context, path string) (creationTime int64, header map[string][]string, body []byte) { func getCache(context context.Context, path string) (creationTime int64, header map[string][]string, body []byte) {
var headerBytes []byte var headerBytes []byte
allowedTime := time.Now().Unix() - appConfig.Cache.Expiration allowedTime := time.Now().Unix() - appConfig.Cache.Expiration
row := appDb.QueryRowContext(context, "select COALESCE(time, 0), header, body from cache where path=? and time>=?", path, allowedTime) row := cacheDb.QueryRowContext(context, "select COALESCE(time, 0), header, body from cache where path=? and time>=?", path, allowedTime)
_ = row.Scan(&creationTime, &headerBytes, &body) _ = row.Scan(&creationTime, &headerBytes, &body)
header = make(map[string][]string) header = make(map[string][]string)
_ = json.Unmarshal(headerBytes, &header) _ = json.Unmarshal(headerBytes, &header)
@ -97,21 +125,20 @@ func getCache(context context.Context, path string) (creationTime int64, header
func saveCache(path string, now time.Time, header map[string][]string, body []byte) { func saveCache(path string, now time.Time, header map[string][]string, body []byte) {
headerBytes, _ := json.Marshal(header) headerBytes, _ := json.Marshal(header)
startWritingToDb() startWritingToCacheDb()
defer finishWritingToDb() defer finishWritingToCacheDb()
tx, err := appDb.Begin() tx, err := cacheDb.Begin()
if err != nil { if err != nil {
return return
} }
_, _ = tx.Exec("delete from cache where time<?;", now.Unix()-appConfig.Cache.Expiration)
_, _ = tx.Exec("insert or replace into cache (path, time, header, body) values (?, ?, ?, ?);", path, now.Unix(), headerBytes, body) _, _ = tx.Exec("insert or replace into cache (path, time, header, body) values (?, ?, ?, ?);", path, now.Unix(), headerBytes, body)
_ = tx.Commit() _ = tx.Commit()
} }
func purgeCache() { func purgeCache() {
startWritingToDb() startWritingToCacheDb()
defer finishWritingToDb() defer finishWritingToCacheDb()
tx, err := appDb.Begin() tx, err := cacheDb.Begin()
if err != nil { if err != nil {
return return
} }

View File

@ -19,7 +19,6 @@ func migrateDb() error {
CREATE TABLE post_parameters (id integer primary key autoincrement, path text not null, parameter text not null, value text); CREATE TABLE post_parameters (id integer primary key autoincrement, path text not null, parameter text not null, value text);
CREATE INDEX index_pp_path on post_parameters (path); CREATE INDEX index_pp_path on post_parameters (path);
CREATE TABLE redirects (fromPath text not null, toPath text not null, primary key (fromPath, toPath)); CREATE TABLE redirects (fromPath text not null, toPath text not null, primary key (fromPath, toPath));
CREATE TABLE cache (path text not null primary key, time integer, header blob, body blob);
`) `)
return err return err
}, },