Skip to content

Commit

Permalink
Change cache for a memory if not utilized
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagozs committed Mar 18, 2022
1 parent b89baa8 commit 9456abd
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 31 deletions.
51 changes: 37 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,42 @@ Easy way to consume the public api informations from MercadoBitcoin

## API v4 (new - working in progress)

- [x] Authorization
- [ ] Accounts
- [x] - Get Accounts
- [x] - Balance List
- [ ] - Position List
- [ ] Trading
- [x] - Get Order
- [x] - Order Place
- [x] - Order Cancel
- [x] - Order List
- [x] - Order Cancel All
- [ ] Wallet
- [ ] Wallet Deposit
- [ ] Wallet Withdraw
- [ ] Wallet GetDraw
- [ ] Public data
- [x] - Get Ticker
- [ ] - Get Orderbook
- [ ] - Get Trades
- [ ] - Get Candles
- [ ] - Get Symbol


```golang
key := os.Getenv("MB_KEY")
secret := os.Getenv("MB_SECRET")

c, err := cache.NewCache()
optsc := []options.Options{
options.OptFolder("./settings"),
options.OptFileName("cache.db"),
options.OptTTL(3000),
options.OptLogDebug(true),
options.OptLogDisable(false),
}

c, err := cache.NewCache(kind.BUNTDB, optsc...)
if err != nil {
log.Fatal(err)
}
Expand All @@ -24,29 +55,21 @@ if err != nil {
fmt.Println(err)
}

// step 1 - get all authorization (mandatory)
if auth, err := a.AuthorizationToken(); err != nil {
auth, acc, err := a.Login()
if err != nil {
fmt.Println(err)
} else {
fmt.Println(auth)
return
}

// step 2 - run the account function to get all info (mandatory)
if acc, err := a.GetAccounts(); err != nil {
fmt.Println(err)
} else {
fmt.Println(acc)
}
fmt.Printf("%+v\n", auth)
fmt.Printf("%+v\n", acc)

// step 3 - run others methods, before that, you need
// run the function account to get all information
if balances, err := a.GetBalances(); err != nil {
fmt.Println(err)
} else {
fmt.Println(balances)
}

// step 4 - publics endpoints
if ticker, err := a.Tickers("BTC-BRL"); err != nil {
fmt.Println(err)
} else {
Expand Down
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ require github.com/hashicorp/go-retryablehttp v0.7.0

require (
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/thiagozs/go-cache v1.0.3
github.com/thiagozs/go-cache v1.0.5
)

require (
github.com/go-redis/redis/v7 v7.4.1 // indirect
github.com/google/go-querystring v1.1.0
github.com/rs/zerolog v1.26.1 // indirect
github.com/rs/zerolog v1.26.1
github.com/shopspring/decimal v1.3.1
github.com/thiagozs/go-utils v0.0.0-20211118150243-5cfe9a632a4b // indirect
github.com/tidwall/btree v0.6.1 // indirect
Expand All @@ -25,4 +25,7 @@ require (
github.com/tidwall/tinyqueue v0.1.1 // indirect
)

require github.com/pkg/errors v0.9.1 // indirect
require (
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand All @@ -34,6 +36,10 @@ github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFR
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/thiagozs/go-cache v1.0.3 h1:iZ3DQ9iq3ZOozxmJtZKKGb5JPXO8GBm9t5LNvI7ZA4o=
github.com/thiagozs/go-cache v1.0.3/go.mod h1:Wp8mRssm0HnkvY88Z/WihoKfNqLJSurir3aboSJfnfs=
github.com/thiagozs/go-cache v1.0.4 h1:DViMt1ob4HS/3wrYXmZNKUigLDNfVtFJ2u1PWmzL/0s=
github.com/thiagozs/go-cache v1.0.4/go.mod h1:nnbrPzqCKSk6mEOTF7HtgHP9/0/mN76G3aI2qafSKjI=
github.com/thiagozs/go-cache v1.0.5 h1:iiyfJtbG1KUM6WpoI4PBchnMyIo0j0g8TAyd3XajNcI=
github.com/thiagozs/go-cache v1.0.5/go.mod h1:nnbrPzqCKSk6mEOTF7HtgHP9/0/mN76G3aI2qafSKjI=
github.com/thiagozs/go-utils v0.0.0-20211118150243-5cfe9a632a4b h1:9Yxe9xzChXQdXxJMqTE3hHSENnBfd1RoUwJg+F3/zUs=
github.com/thiagozs/go-utils v0.0.0-20211118150243-5cfe9a632a4b/go.mod h1:oSPQMisOe4ySVivq0SOvHfWlBCHrk7iNqQphg7wHD3s=
github.com/tidwall/assert v0.1.0/go.mod h1:QLYtGyeqse53vuELQheYl9dngGCJQ+mTtlxcktb+Kj8=
Expand Down
16 changes: 14 additions & 2 deletions v4/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import (
"io/ioutil"
"net/http"
"os"
"time"

"github.com/rs/zerolog"
"github.com/rs/zerolog/pkgerrors"
"github.com/thiagozs/go-cache/v1/cache/drivers/kind"
"github.com/thiagozs/go-cache/v1/cache/options"
"github.com/thiagozs/go-mbsdk/v4/config"
"github.com/thiagozs/go-mbsdk/v4/models"
"github.com/thiagozs/go-mbsdk/v4/pkg/cache"
"github.com/thiagozs/go-mbsdk/v4/pkg/caller"
"github.com/thiagozs/go-mbsdk/v4/pkg/replacer"
)

func New(opts ...Options) (*Api, error) {

mts := &ApiCfg{}

for _, op := range opts {
err := op(mts)
if err != nil {
Expand All @@ -33,6 +35,16 @@ func New(opts ...Options) (*Api, error) {
Caller().
Timestamp().Logger()

if mts.cache == nil {
cache, err := cache.NewCache(kind.GOCACHE,
options.OptTimeCleanUpInt(time.Duration(60)*time.Second),
options.OptTimeExpiration(time.Duration(300)*time.Second))
if err != nil {
return &Api{}, err
}
mts.cache = cache
}

config.Config.Login = mts.key
config.Config.Password = mts.secret
config.Config.Cache = mts.cache
Expand Down
7 changes: 6 additions & 1 deletion v4/api/api_trading.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/shopspring/decimal"
"github.com/thiagozs/go-cache/v1/cache/drivers/kind"
"github.com/thiagozs/go-mbsdk/v4/config"
"github.com/thiagozs/go-mbsdk/v4/models"
"github.com/thiagozs/go-mbsdk/v4/pkg/caller"
Expand Down Expand Up @@ -308,7 +309,11 @@ func (a *Api) CancelOrder(symbol string, id string) error {
return nil
}

func (a *Api) CancelAllOrders(symbol string) error {
func (a *Api) CancelAllCachedOrders(symbol string) error {

if a.cache.GetDriver() == kind.GOCACHE {
return fmt.Errorf("sorry, this method is not supported for GOCACHE driver")
}

val, err := a.cache.GetKeyVal(config.ORDERS_INDEX.String())
if err != nil {
Expand Down
12 changes: 11 additions & 1 deletion v4/examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"log"
"os"

"github.com/thiagozs/go-cache/v1/cache/drivers/kind"
"github.com/thiagozs/go-cache/v1/cache/options"
"github.com/thiagozs/go-mbsdk/v4/api"
"github.com/thiagozs/go-mbsdk/v4/pkg/cache"
)
Expand All @@ -13,7 +15,15 @@ func main() {
key := os.Getenv("MB_KEY")
secret := os.Getenv("MB_SECRET")

c, err := cache.NewCache(false, true)
optsc := []options.Options{
options.OptFolder("./settings"),
options.OptFileName("cache.db"),
options.OptTTL(3000),
options.OptLogDebug(true),
options.OptLogDisable(false),
}

c, err := cache.NewCache(kind.BUNTDB, optsc...)
if err != nil {
log.Fatal(err)
}
Expand Down
17 changes: 7 additions & 10 deletions v4/pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,17 @@ package cache

import (
"github.com/thiagozs/go-cache/v1/cache"
"github.com/thiagozs/go-cache/v1/cache/drivers"
"github.com/thiagozs/go-cache/v1/cache/drivers/kind"
"github.com/thiagozs/go-cache/v1/cache/options"
)

type Cache struct {
cache cache.CachePort
}

func NewCache(logDebug, logDisable bool) (*Cache, error) {
opts := []options.Options{
options.OptFolder("./settings"),
options.OptFileName("cache.db"),
options.OptTTL(3000),
options.OptLogDebug(logDebug),
options.OptLogDisable(logDisable),
}
func NewCache(driver kind.Driver, opts ...options.Options) (*Cache, error) {

cache, err := cache.New(drivers.BUNTDB, opts...)
cache, err := cache.New(driver, opts...)
if err != nil {
return &Cache{}, err
}
Expand Down Expand Up @@ -49,3 +42,7 @@ func (c *Cache) SetKeyValTTLAsJSONTTL(key string, value interface{}, ttl int) er
func (c *Cache) SetKeyValAsJSON(key string, value interface{}) error {
return c.cache.WriteKeyValAsJSON(key, value)
}

func (c *Cache) GetDriver() kind.Driver {
return c.cache.GetDriver()
}

0 comments on commit 9456abd

Please sign in to comment.