https://github.com/baekhyunee7/recache
redis cache for database
https://github.com/baekhyunee7/recache
database go-redis redis
Last synced: 6 months ago
JSON representation
redis cache for database
- Host: GitHub
- URL: https://github.com/baekhyunee7/recache
- Owner: baekhyunee7
- Created: 2024-06-21T09:11:39.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-16T06:48:44.000Z (almost 2 years ago)
- Last Synced: 2024-09-17T09:16:28.938Z (almost 2 years ago)
- Topics: database, go-redis, redis
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# recache
redis cache for database
# example
```go
type s struct {
ID int64 `json:"id" gorm:"primaryKey"`
A int `json:"a"`
B string `json:"b"`
}
func main() {
db, _ := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
db.AutoMigrate(&s{})
redisServer, _ := miniredis.Run()
red := redis.NewClient(&redis.Options{
Addr: redisServer.Addr(),
})
cache := recache.NewCache(red, recache.WithStatInterval(time.Second))
cache.Exec(context.Background(), func() error {
return db.Create(&s{
ID: 1,
A: 100,
B: "b",
}).Error
}, "key1")
get(cache, db, 1)
get(cache, db, 1)
select {}
}
func get(cache *recache.Cache, db *gorm.DB, id int64) {
var model s
cache.Query(context.Background(), "key1", &model, func(v any) (bool, error) {
err := db.Model(&s{}).Where("id = ?", id).First(&v).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return false, nil
}
return false, err
}
return true, nil
})
fmt.Printf("%v\n", &model)
}
```
* `recache.NewCache` returns a `Cache` instance, optionally privided with options
* `Cache.Exec` do the write operation and then delete the redis data
* `Cache.Query` first search for data from redis, if not found, execute the incoming db query parameters. The db query function needs to return whether it is found
* `Cache.Get` query only from redis
* `Cache.Set` set only by redis
* `Cache.Del` delete from redis
* `Cache.SetWithExpire` set by redis with expiration
### Option
* `WithLogger` customize logger instance
* `WithExpire` customize the expiration time of redis data. The expiration time is really less than or equal to this value.
* `WithHystrixConfig` customize `Hystrix` configration
* `WithMetricsPort` open `Hystrix` metrics on configuration port
* `WithStatInterval` customize stat infomation interval