pg icon indicating copy to clipboard operation
pg copied to clipboard

Is it can cache query data at memory ?

Open AbooJan opened this issue 8 years ago • 5 comments

Can pg ORM setup to cache the query data at memory or other ?

AbooJan avatar Sep 11 '17 08:09 AbooJan

No, nothing like that at the moment.

vmihailenco avatar Sep 11 '17 09:09 vmihailenco

It would be really interesting to "redis" Cache(id, object, interval) upon getter result success. Having it baked into go-pg would give the library a unique advantage.

jmheidly avatar Sep 13 '17 08:09 jmheidly

I tried to play with the DB.Query() to achieve this but there is currently no way to modify Query.model. Here is an example:

package main

import (
	"time"
	"github.com/go-pg/pg"
	"github.com/go-pg/pg/orm"
)

type PgDb struct {
	*pg.DB
}

type cacheResult struct {
	result orm.Result
	models []interface{}
	query  interface{}
}

var (
	queryCache = make(map[string]cacheResult)
)

func (db *PgDb) Query(model, query interface{}, params ...interface{}) (res orm.Result, err error) {
	q, ok := query.(interface{ AppendQuery(b []byte) ([]byte, error) })
	if !ok {
		return db.DB.Query(model, query, params...)
	}

	var formattedQuery []byte
	formattedQuery, err = q.AppendQuery(nil)
	if err != nil {
		return
	}
	hash := string(formattedQuery)
	if cache, exists := queryCache[hash]; exists {
		//@todo need to get the models pointers (params in this case) and switch it to cache.models
		return cache.result, nil
	}
	res, err = db.DB.Query(model, query, params...)
	if err != nil {
		return
	}
	queryCache[hash] = cacheResult{result: res, models: params, query: query}

	return
}

func (db *PgDb) Model(model ...interface{}) *orm.Query {
	return orm.NewQuery(db, model...)
}

func (db *PgDb) Select(model interface{}) error {
	return orm.Select(db, model)
}

gadelkareem avatar Mar 13 '18 10:03 gadelkareem

I suggest to look at https://github.com/dgraph-io/ristretto which is used by dgraph database for caching.

anjmao avatar Nov 05 '19 11:11 anjmao

Any more thoughts/progress on this? I think ristretto is a promising avenue...

jdbranham avatar Jun 02 '21 18:06 jdbranham