Cache
Goffee uses Redis for cache and its disabled by default, you can enable it in the file config/cache.go by setting the attribute EnableCache to true, then add Redis connection information to the .env if you are using it. otherwise, you can use an external tool to inject these variables into the environment
Here is a sample of the Redis connection information in the .env` file:
#######################################
###### CACHE ######
#######################################
CACHE_DRIVER=redis
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_DB=0
Set values in the cache
package controllers
import (
"git.smarteching.com/goffee/core/v2"
)
func Login(c *core.Context) *core.Response {
err := c.GetCache().Set("userID", "12345")
}
Note: cache values are stored as string, so convert other types before setting them
(for example with c.CastToString(...)).
Set values in the cache with an expiration date
package controllers
import (
"time"
"git.smarteching.com/goffee/core/v2"
)
func Login(c *core.Context) *core.Response {
hours24 := time.Hour * 24 // 24 hours duration
err := c.GetCache().SetWithExpiration("userID", "12345", hours24) // expires after 24 hours
}
Where the value passed to the cache is a string and the expiration is a time.Duration.
Get values from cache
Here is how you can get values from cache
package controllers
import (
"git.smarteching.com/goffee/core/v2"
)
func Login(c *core.Context) *core.Response {
userID, err := c.GetCache().Get("userID")
}
Cache Delete
Here is how you can delete something from the cache
package controllers
import (
"git.smarteching.com/goffee/core/v2"
)
func Login(c *core.Context) *core.Response {
err := c.GetCache().Delete("userID")
}