Commit ee70bf4b authored by Timothy Richard's avatar Timothy Richard
Browse files

initial commit

parents
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.exe
*.test
*.prof
env
*.iml
.idea/
*.ipr
*.iws
out/
.idea_modules/
vendor/
\ No newline at end of file
# REDIS
\ No newline at end of file
package dbredis
import (
"git.edenfarm.id/cuxs/env"
"strconv"
)
var (
Config = loadConfig()
)
type config struct {
RedisHost string
RedisPassword string
RedisDB int
}
// loadConfig set config value from environment variable.
// If not exists, it will have a default values.
func loadConfig() *config {
c := new(config)
c.RedisHost = env.GetString("REDIS_HOST", "127.0.0.1:6379") // if using tcp, remove http (only IP and port)
c.RedisPassword = env.GetString("REDIS_PASSWORD", "")
redisDB := env.GetString("REDIS_DB", "0")
c.RedisDB, _ = strconv.Atoi(redisDB)
return c
}
This diff is collapsed.
package dbredis
import (
"context"
"github.com/go-redis/cache/v8"
"github.com/go-redis/redis/v8"
"github.com/labstack/echo/v4"
"net/http"
"os"
"time"
"github.com/ulule/limiter/drivers/store/redis"
"github.com/go-pkgz/expirable-cache"
"github.com/labstack/echo"
)
var (
Redis = new(RedisDB) // will insert from func redisStart()
ctx = context.TODO()
)
// Area model for area table.
type RedisDB struct {
Client *redis.Client
Cache *cache.Cache
}
func RedisStart() (rs *RedisDB, e error) {
rs = new(RedisDB)
rs.Client = redis.NewClient(&redis.Options{ // setup connection to redis
Addr: Config.RedisHost,
Network: "tcp",
Password: Config.RedisPassword,
DB: Config.RedisDB,
})
rs.Cache = cache.New(&cache.Options{ // setup cache redis
Redis: rs.Client,
})
e = rs.Client.Set(ctx, "key", "value", time.Minute).Err()
if e != nil {
defer rs.Client.Close()
panic(e)
} else {
Redis = rs
}
return rs, e
}
func (m *RedisDB) CheckExistKey(key string) bool { // check key exist or not (if true == exist key, false == there's no key)
return m.Cache.Exists(ctx, key)
}
func (m *RedisDB) SetCache(key string, value interface{}, expired time.Duration) (e error) {
if e = m.Cache.Set(&cache.Item{
Ctx: ctx,
Key: key,
Value: value,
TTL: expired,
}); e != nil {
e = echo.NewHTTPError(http.StatusUnprocessableEntity, "redis-cache set item error")
}
return
}
func (m *RedisDB) GetCache(key string, value interface{}) error {
return m.Cache.Get(ctx, key, &value)
}
func (m *RedisDB) DeleteCache(key string) error {
return m.Cache.Delete(ctx, key)
}
func (m *RedisDB) DeleteCacheWhereLike(key string) error { // add * in the last key
if len(os.Args) > 1 {
key = os.Args[1]
}
var foundedRecordCount int = 0
iter := m.Client.Scan(ctx, 0, key, 0).Iterator()
for iter.Next(ctx) {
m.Cache.Delete(ctx, iter.Val())
foundedRecordCount++
}
if err := iter.Err(); err != nil {
panic(err)
}
return iter.Err()
}
func (m *RedisDB) DeleteAllCache() error {
return m.Client.FlushDB(ctx).Err()
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment