fs-cache provides a quick way to store and retrieve frequently accessed data, significantly enhancing your application performance and reducing database queries / API calls.
go get github.com/iqquee/fs-cache@latest
fscache "github.com/iqquee/fs-cache"
KeyStore gives you a Redis-like feature similarly as you would with a Redis database.
Set() adds a new data into the in-memory storage
fs := fscache.New()
// the third param is an optional param used to set the expiration time of the set data
if err := fs.KeyStore().Set("key1", "user1", 5*time.Minute); err != nil {
fmt.Println("error setting key1:", err)
}
Get() retrieves a data from the in-memory storage
fs := fscache.New()
result, err := fs.KeyStore().Get("key1")
if err != nil {
fmt.Println("error getting key 1:", err)
}
fmt.Println("key1:", result)
DataStore gives you an SQL/NoSQL-like feature.
Create is used to insert a new record into the storage.
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
fs := fscache.New()
var user User
user.Name = "jane doe"
user.Age = 20
if err := fs.DataStore().Namespace(User{}).Create(user); err != nil {
fmt.Println(err)
}
First is used when you expect a unique record and decodes it into the param object.
fs := fscache.New()
// filter out record of age 35
filter := map[string]interface{}{
"age": 35,
}
var response User
if err := fs.DataStore().Namespace(User{}).First(filter, &response); err != nil {
fmt.Println(err)
}
fmt.Println(response)
Find is used when you expect multiple records and decodes it into the param object.
fs := fscache.New()
// filter out records with of age 35
filter := map[string]interface{}{
"age": 35,
}
var response []User
if err := fs.DataStore().Namespace(User{}).Find(filter, &response); err != nil {
fmt.Println(err)
}
fmt.Println(response)
You can use the Sync method to synchronize the records in the cache to your live sql database.
fs := fscache.New()
ns := fs.DataStore().Namespace(User{})
db := gorm.Open(nil, &gorm.Config{})
ns.ConnectSQLDB(db).Sync(1 * time.Second)
Anyone can contribute to this library ;). So, feel free to improve on and add new features. I await your pull requests.