modulepackage
1.2.17
Repository: https://github.com/rookie-ninja/rk-cache.git
Documentation: pkg.go.dev
# README
rk-cache/redis
Init cache with go-redis/cache or local memory cache from YAML config.
This belongs to rk-boot family. We suggest use this lib from rk-boot.
Supported bootstrap
Bootstrap | Description |
---|---|
YAML based | Start go-redis/cache from YAML |
Code based | Start go-redis/cache from code |
Installation
- rk-boot: Bootstrapper base
- rk-gin: Bootstrapper for gin-gonic/gin Web Framework for API
- rk-cache/redis: Bootstrapper for go-redis/cache of cache
go get github.com/rookie-ninja/rk-boot/v2
go get github.com/rookie-ninja/rk-gin/v2
go get github.com/rookie-ninja/rk-cache/redis
Quick Start
In the bellow example, we will run Redis locally and implement API of Get/Set of K/V.
- GET /v1/get, get value
- POST /v1/set, set value
1.Create boot.yaml
- Create web server with Gin framework at port 8080
- Create Redis entry which connects Redis at localhost:6379 as cache
---
gin:
- name: cache-service
port: 8080
enabled: true
cache:
- name: redis-cache
enabled: true
local:
enabled: false
redis:
enabled: true
2.Create main.go
In the main() function, we implement bellow things.
- Register APIs into Gin router.
package main
import (
"context"
"github.com/gin-gonic/gin"
"github.com/rookie-ninja/rk-boot/v2"
"github.com/rookie-ninja/rk-cache/redis"
"github.com/rookie-ninja/rk-gin/v2/boot"
"net/http"
)
var cacheEntry *rkcache.CacheEntry
func main() {
boot := rkboot.NewBoot()
boot.Bootstrap(context.TODO())
// assign cache
cacheEntry = rkcache.GetCacheEntry("redis-cache")
// assign router
ginEntry := rkgin.GetGinEntry("cache-service")
ginEntry.Router.GET("/v1/get", Get)
ginEntry.Router.GET("/v1/set", Set)
boot.WaitForShutdownSig(context.TODO())
}
func Get(ctx *gin.Context) {
val := ""
resp := cacheEntry.GetFromCache(&rkcache.CacheReq{
Key: "demo-key",
Value: &val,
})
if resp.Error != nil || !resp.Success {
ctx.JSON(http.StatusInternalServerError, resp.Error)
return
}
ctx.JSON(http.StatusOK, map[string]string{
"value": val,
})
}
func Set(ctx *gin.Context) {
val, ok := ctx.GetQuery("value")
if !ok {
ctx.JSON(http.StatusBadRequest, "No value found")
}
cacheEntry.AddToCache(&rkcache.CacheReq{
Key: "demo-key",
Value: val,
})
ctx.JSON(http.StatusOK, map[string]string{
"value": val,
})
}
3.Start server
$ go run main.go
2022-03-09T00:10:50.325+0800 INFO redis/entry.go:163 Bootstrap CacheRedisEntry {"entryName": "redis-cache", "localCache": false, "redisCache": true}
2022-03-09T00:10:50.325+0800 INFO [email protected]/boot.go:253 Bootstrap redisEntry {"eventId": "2e47b54a-a46c-4c23-ae51-f105bdbc5836", "entryName": "redis-cache", "entryType": "RedisEntry", "clientType": "Single"}
2022-03-09T00:10:50.325+0800 INFO [email protected]/boot.go:261 Ping redis at [localhost:6379]
2022-03-09T00:10:50.330+0800 INFO [email protected]/boot.go:267 Ping redis at [localhost:6379] success
2022-03-09T00:10:50.330+0800 INFO boot/gin_entry.go:624 Bootstrap GinEntry {"eventId": "2e47b54a-a46c-4c23-ae51-f105bdbc5836", "entryName": "cache-service", "entryType": "GinEntry"}
------------------------------------------------------------------------
endTime=2022-03-09T00:10:50.33088+08:00
startTime=2022-03-09T00:10:50.330832+08:00
elapsedNano=47527
timezone=CST
ids={"eventId":"2e47b54a-a46c-4c23-ae51-f105bdbc5836"}
app={"appName":"","appVersion":"","entryName":"cache-service","entryType":"GinEntry"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"10.8.0.2","os":"darwin","realm":"*","region":"*"}
payloads={"ginPort":8080}
counters={}
pairs={}
timing={}
remoteAddr=localhost
operation=Bootstrap
resCode=OK
eventStatus=Ended
EOE
4.Validation
4.1 Set value
$ curl "localhost:8080/v1/set?value=my-value"
{"value":"my-value"}
4.2 Get value
$ curl localhost:8080/v1/get
{"value":"my-value"}
4.3 Validate Redis
The key of cache will be encoded with MD5 and value will be base64 encoded.
# Functions
GetCache get cache.Cache with entryName.
No description provided by the author
RegisterCacheEntry register with Option.
RegisterCacheEntryYAML create entry from config file.
WithDescription provide name.
WithLocalCache provide LocalCache.
WithLoggerEntry provide rkentry.LoggerEntry entry name.
WithName provide name.
WithRedisCache provide RedisEntry.
# Constants
No description provided by the author
# Structs
BootCache bootstrap entry from config.
No description provided by the author
CacheEntry implementation of rkentry.Entry.
No description provided by the author
No description provided by the author
# Type aliases
Option entry options.