Is there way to add new caching layer feature such as Redis cache as long query and also expiration of caching. ? Thank you.

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: jinzhu

func QueryFromCache(db *gorm.DB) {
   if !readFromCache(db) {
      callbacks.Query(db)
   }
}
db.Callback().Query().Replace("gorm:query", QueryFromCache)

or override the Conn like the prepared statments

Comment From: tabvn

@jinzhu thank you, if this is also including into developer document that could be great for other developers would like to integrate with caching layer with Gorm.

Comment From: mikarios

Hello, we have tried to add a caching layer in our system. What we have done is overwrite the gorm:query like so: db.Callback().Query().Replace("gorm:query", queryFromCache)

In queryFromCache what we do is:

_, err := cacheService.Get(key, db.Statement.Dest)
if err != nil {
    callbacks.Query(db)

    if db.Statement.Error != nil {
      return
    }

    value := db.Statement.Dest
    _ = cacheService.Set(key, value, config.GetInstance().REDIS.TTL)

    return
}

Problem is that if the result is found in cache then the AfterFind hook (and possibly other hooks as well) does not get executed. Is there a reason for this that we haven't found or it's a bug?

Comment From: mikarios

We found out that we need to set db.RowsAffected = 1 in order for the AfterFind to be executed... This is very counter intuitive and confusing. Is this the intended behaviour?

Comment From: kateile

Coming from Node JS world TypeORM have a really incredible caching mechanism which is integrated on ORM itself. For instance if you are finding a single document from database the result can be stored on redis/memory so later queries don't hit database until expire time has reached or results are manually invalidated. More can be seen here on docs.

@jinzhu I think it will be really helpful if caching functionality is implemented in gorm directly. Maybe you want to reopen this?

Comment From: rts-gordon

Support adding Redis and Cache layer for GORM.

Comment From: steinathan

https://github.com/Pacific73/gorm-cache

found this library, it pretty much works with redis

Comment From: goodylili

@jinzhu please reopen

Comment From: jinzhu

https://github.com/go-gorm/caches