add cache
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
8be5d0b831
commit
8992bb0e9d
|
@ -1,3 +1,11 @@
|
||||||
|
GET http://localhost:8090/catalog
|
||||||
|
|
||||||
|
###
|
||||||
|
|
||||||
|
GET http://localhost:8090/products/1
|
||||||
|
|
||||||
|
###
|
||||||
|
|
||||||
POST http://localhost:8090/cart
|
POST http://localhost:8090/cart
|
||||||
content-type: application/json
|
content-type: application/json
|
||||||
|
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -5,6 +5,7 @@ go 1.22
|
||||||
require (
|
require (
|
||||||
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
|
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
|
||||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0
|
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0
|
||||||
|
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||||
google.golang.org/genproto/googleapis/api v0.0.0-20240513163218-0867130af1f8
|
google.golang.org/genproto/googleapis/api v0.0.0-20240513163218-0867130af1f8
|
||||||
google.golang.org/grpc v1.64.0
|
google.golang.org/grpc v1.64.0
|
||||||
google.golang.org/protobuf v1.34.1
|
google.golang.org/protobuf v1.34.1
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -4,6 +4,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0=
|
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0=
|
||||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k=
|
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k=
|
||||||
|
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=
|
||||||
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
|
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
|
||||||
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
||||||
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
||||||
|
|
|
@ -6,8 +6,11 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"github.com/patrickmn/go-cache"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Product struct {
|
type Product struct {
|
||||||
|
@ -30,14 +33,18 @@ var (
|
||||||
ErrProductNotFound = errors.New("product not found")
|
ErrProductNotFound = errors.New("product not found")
|
||||||
)
|
)
|
||||||
|
|
||||||
type storageFile struct{}
|
type storageFile struct {
|
||||||
|
cache *cache.Cache
|
||||||
|
}
|
||||||
|
|
||||||
func NewStorageFile() storage.IStorage {
|
func NewStorageFile() storage.IStorage {
|
||||||
return &storageFile{}
|
return &storageFile{
|
||||||
|
cache: cache.New(24*time.Hour, time.Hour),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storageFile) GetCatalog(_ context.Context) ([]*crm.Category, error) {
|
func (s *storageFile) GetCatalog(_ context.Context) ([]*crm.Category, error) {
|
||||||
data, err := os.ReadFile("resources/catalog.json")
|
data, err := s.readFile("catalog")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -49,7 +56,7 @@ func (s *storageFile) GetCatalog(_ context.Context) ([]*crm.Category, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storageFile) GetPositions(_ context.Context, id int64) ([]*crm.Product, error) {
|
func (s *storageFile) GetPositions(_ context.Context, id int64) ([]*crm.Product, error) {
|
||||||
data, err := os.ReadFile("resources/products.json")
|
data, err := s.readFile("products")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -68,7 +75,7 @@ func (s *storageFile) GetPositions(_ context.Context, id int64) ([]*crm.Product,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storageFile) GetProduct(_ context.Context, id int64) (*crm.Product, error) {
|
func (s *storageFile) GetProduct(_ context.Context, id int64) (*crm.Product, error) {
|
||||||
data, err := os.ReadFile("resources/products.json")
|
data, err := s.readFile("products")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -123,7 +130,7 @@ func (s *storageFile) GetBreadcrumbs(ctx context.Context, id int64) ([]*crm.Cate
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
data, err := os.ReadFile("resources/catalog.json")
|
data, err := s.readFile("catalog")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -163,7 +170,7 @@ func getBreadcrumbs(categories []*crm.Category, id int64) []*crm.Category {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storageFile) GetPositionsByText(_ context.Context, text string) ([]*crm.Product, error) {
|
func (s *storageFile) GetPositionsByText(_ context.Context, text string) ([]*crm.Product, error) {
|
||||||
data, err := os.ReadFile("resources/products.json")
|
data, err := s.readFile("products")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -182,3 +189,16 @@ func (s *storageFile) GetPositionsByText(_ context.Context, text string) ([]*crm
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *storageFile) readFile(name string) ([]byte, error) {
|
||||||
|
cacheData, found := s.cache.Get(name)
|
||||||
|
if found {
|
||||||
|
return cacheData.([]byte), nil
|
||||||
|
}
|
||||||
|
data, err := os.ReadFile(fmt.Sprintf("resources/%s.json", name))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s.cache.Set(name, data, cache.DefaultExpiration)
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue