-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
3,004 additions
and
823 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
default: help | ||
|
||
.PHONY: help | ||
help: # Show help for each of the Makefile recipes. | ||
@grep -E '^[a-zA-Z0-9 -]+:.*#' Makefile | sort | while read -r l; do printf "\033[1;32m$$(echo $$l | cut -f 1 -d':')\033[00m:$$(echo $$l | cut -f 2- -d'#')\n"; done | ||
|
||
|
||
.PHONY: install-dep | ||
install-dep: # Install dependencies | ||
# Install dependencies | ||
# -------------------- | ||
# install ifacemaker... | ||
@go install github.com/vburenin/ifacemaker@latest | ||
# install mockgen... | ||
@go install github.com/golang/mock/mockgen@latest | ||
# Install deps complete +++ | ||
|
||
.PHONY: interfaces | ||
interfaces: # Generate interfaces | ||
# Generate interfaces | ||
# ------------------- | ||
# BuntDBLayer interfaces... | ||
@ifacemaker -f buntdb/buntdb.go -s BuntDBLayer -i BuntDBLayerRepo -p buntdb -o buntdb/repository.go | ||
# GocacheLayer interfaces... | ||
@ifacemaker -f gocache/gocache.go -s GocacheLayer -i GocacheLayerRepo -p gocache -o gocache/repository.go | ||
# RedisLayer interfaces... | ||
@ifacemaker -f redis/redis.go -s RedisLayer -i RedisLayerRepo -p redis -o redis/repository.go | ||
# Cache interfaces... | ||
@ifacemaker -f cache.go -s Cache -i CacheRepo -p cache -o repository.go | ||
|
||
mocks: # Generate mocks | ||
# Generate mocks | ||
# -------------- | ||
# BuntDBLayer mocks... | ||
@mockgen -source=buntdb/repository.go -destination=buntdb/buntdb_mock.go -package=buntdb | ||
# MemoryLayer mocks... | ||
@mockgen -source=gocache/repository.go -destination=gocache/gocache_mock.go -package=gocache | ||
# RedisLayer mocks... | ||
@mockgen -source=redis/repository.go -destination=redis/redis_mock.go -package=redis | ||
# Cache mocks... | ||
@mockgen -source=repository.go -destination=cache_mock.go -package=cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package buntdb | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path" | ||
"time" | ||
|
||
"github.com/rs/zerolog" | ||
kind "github.com/thiagozs/go-cache/kind" | ||
"github.com/thiagozs/go-utils/files" | ||
"github.com/tidwall/buntdb" | ||
) | ||
|
||
type BuntDBLayer struct { | ||
params *BuntDBParams | ||
} | ||
|
||
func NewBuntDB(opts ...Options) (*BuntDBLayer, error) { | ||
params, err := newBuntDBParams(opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
zerolog.SetGlobalLevel(zerolog.InfoLevel) | ||
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix | ||
log := zerolog.New(os.Stderr).With().Timestamp().Logger() | ||
|
||
if params.GetLogDebug() { | ||
log.Info().Bool("debug", true).Msg("log debug") | ||
zerolog.SetGlobalLevel(zerolog.DebugLevel) | ||
} | ||
|
||
if params.GetLogDisable() { | ||
zerolog.SetGlobalLevel(zerolog.Disabled) | ||
} | ||
|
||
pathFile := fmt.Sprintf("%s/%s", params.GetFolder(), params.GetFile()) | ||
|
||
log.Info().Str("path_file", pathFile).Msg("file database") | ||
if !files.FileExists(pathFile) { | ||
if err := files.MkdirAll(path.Dir(pathFile)); err != nil { | ||
log.Info().Err(err).Msg("fail create a directory") | ||
return nil, err | ||
} | ||
} | ||
|
||
db, err := buntdb.Open(pathFile) | ||
if err != nil { | ||
log.Info().Err(err).Msg("could not open data file path") | ||
return nil, err | ||
} | ||
|
||
params.SetLogger(log) | ||
params.SetDB(db) | ||
|
||
return &BuntDBLayer{ | ||
params: params, | ||
}, nil | ||
} | ||
|
||
func (d *BuntDBLayer) GetVal(key string) (string, error) { | ||
var value string | ||
err := d.params.GetDB().View(func(tx *buntdb.Tx) error { | ||
val, err := tx.Get(key) | ||
if err != nil { | ||
d.params.log.Debug().Err(err).Msg("GetVal") | ||
return err | ||
} | ||
value = val | ||
return nil | ||
}) | ||
d.params.log.Debug().Str("method", "get"). | ||
Str("key", key). | ||
Str("value", value). | ||
Msg("GetVal") | ||
return value, err | ||
} | ||
|
||
func (d *BuntDBLayer) DeleteKey(key string) (string, error) { | ||
var value string | ||
err := d.params.GetDB().Update(func(tx *buntdb.Tx) error { | ||
val, err := tx.Delete(key) | ||
if err != nil { | ||
d.params.log.Debug().Err(err).Msg("DeleteKey") | ||
return err | ||
} | ||
value = val | ||
return nil | ||
}) | ||
d.params.log.Debug().Str("method", "delete"). | ||
Str("key", key). | ||
Str("value", value). | ||
Msg("DeleteKey") | ||
return value, err | ||
} | ||
|
||
func (d *BuntDBLayer) WriteKeyVal(key string, val string) error { | ||
err := d.params.GetDB().Update(func(tx *buntdb.Tx) error { | ||
_, _, err := tx.Set(key, val, nil) | ||
d.params.log.Debug().Err(err).Msg("WriteKeyVal") | ||
return err | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
d.params.log.Debug().Str("method", "write"). | ||
Str("key", key). | ||
Str("value", val). | ||
Msg("WriteKeyVal") | ||
return nil | ||
} | ||
|
||
func (d *BuntDBLayer) WriteKeyValTTL(key string, val string, insec int) error { | ||
if insec == 0 { | ||
return fmt.Errorf("ttl_seconds is zero") | ||
} | ||
err := d.params.GetDB().Update(func(tx *buntdb.Tx) error { | ||
_, _, err := tx.Set(key, val, &buntdb.SetOptions{Expires: true, TTL: time.Second * time.Duration(insec)}) | ||
d.params.log.Debug().Str("method", "update").Err(err).Msg("WriteKeyValTTL") | ||
return err | ||
}) | ||
if err != nil { | ||
d.params.log.Debug().Err(err).Msg("WriteKeyValTTL") | ||
return err | ||
} | ||
|
||
d.params.log.Debug().Str("method", "cache.Set"). | ||
Str("key", key). | ||
Str("value", val). | ||
Int64("ttl", int64(insec)). | ||
Msg("WriteKeyValTTL") | ||
return nil | ||
} | ||
|
||
func (d *BuntDBLayer) WriteKeyValAsJSON(key string, val any) error { | ||
valueAsJSON, err := json.Marshal(val) | ||
if err != nil { | ||
d.params.log.Debug().Str("method", "write").Err(err).Msg("WriteKeyValAsJSON") | ||
return err | ||
} | ||
return d.WriteKeyVal(key, string(valueAsJSON)) | ||
} | ||
|
||
func (d *BuntDBLayer) WriteKeyValAsJSONTTL(key string, val any, insec int) error { | ||
if insec == 0 { | ||
return fmt.Errorf("ttl_seconds is zero") | ||
} | ||
d.params.log.Debug().Int("ttl_seconds", insec).Msg("WriteKeyValTTL") | ||
|
||
valueAsJSON, err := json.Marshal(val) | ||
if err != nil { | ||
d.params.log.Debug().Str("method", "json.Marshal").Err(err).Msg("WriteKeyValAsJSONTTL") | ||
return err | ||
} | ||
return d.WriteKeyValTTL(key, string(valueAsJSON), insec) | ||
} | ||
|
||
func (d *BuntDBLayer) GetDriver() kind.Driver { | ||
return d.params.GetDriver() | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.