mirror of https://github.com/jlelse/GoBlog
Use in-memory db for cache
This commit is contained in:
parent
d1ce3ed49c
commit
17d14ca56e
45
cache.go
45
cache.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
@ -11,9 +12,36 @@ import (
|
|||
)
|
||||
|
||||
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{}
|
||||
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 {
|
||||
|
@ -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) {
|
||||
var headerBytes []byte
|
||||
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)
|
||||
header = make(map[string][]string)
|
||||
_ = 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) {
|
||||
headerBytes, _ := json.Marshal(header)
|
||||
startWritingToDb()
|
||||
defer finishWritingToDb()
|
||||
tx, err := appDb.Begin()
|
||||
startWritingToCacheDb()
|
||||
defer finishWritingToCacheDb()
|
||||
tx, err := cacheDb.Begin()
|
||||
if err != nil {
|
||||
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.Commit()
|
||||
}
|
||||
|
||||
func purgeCache() {
|
||||
startWritingToDb()
|
||||
defer finishWritingToDb()
|
||||
tx, err := appDb.Begin()
|
||||
startWritingToCacheDb()
|
||||
defer finishWritingToCacheDb()
|
||||
tx, err := cacheDb.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -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 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 cache (path text not null primary key, time integer, header blob, body blob);
|
||||
`)
|
||||
return err
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue