From 8992bb0e9d9a08390265fc685be26ce5658b3023 Mon Sep 17 00:00:00 2001 From: Fedorov Vladimir Date: Thu, 30 May 2024 04:32:28 +0700 Subject: [PATCH] add cache --- api/request.http | 8 +++++ go.mod | 1 + go.sum | 2 ++ .../modules/storage/storage_file/storage.go | 34 +++++++++++++++---- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/api/request.http b/api/request.http index e1938cd..5005d23 100644 --- a/api/request.http +++ b/api/request.http @@ -1,3 +1,11 @@ +GET http://localhost:8090/catalog + +### + +GET http://localhost:8090/products/1 + +### + POST http://localhost:8090/cart content-type: application/json diff --git a/go.mod b/go.mod index 0f5cc65..b24bf5f 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22 require ( 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/patrickmn/go-cache v2.1.0+incompatible google.golang.org/genproto/googleapis/api v0.0.0-20240513163218-0867130af1f8 google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.1 diff --git a/go.sum b/go.sum index 0d5590b..88d66e7 100644 --- a/go.sum +++ b/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/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/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/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= diff --git a/internal/modules/storage/storage_file/storage.go b/internal/modules/storage/storage_file/storage.go index 51e6611..377fe5f 100644 --- a/internal/modules/storage/storage_file/storage.go +++ b/internal/modules/storage/storage_file/storage.go @@ -6,8 +6,11 @@ import ( "context" "encoding/json" "errors" + "fmt" + "github.com/patrickmn/go-cache" "os" "strings" + "time" ) type Product struct { @@ -30,14 +33,18 @@ var ( ErrProductNotFound = errors.New("product not found") ) -type storageFile struct{} +type storageFile struct { + cache *cache.Cache +} 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) { - data, err := os.ReadFile("resources/catalog.json") + data, err := s.readFile("catalog") if err != nil { 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) { - data, err := os.ReadFile("resources/products.json") + data, err := s.readFile("products") if err != nil { 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) { - data, err := os.ReadFile("resources/products.json") + data, err := s.readFile("products") if err != nil { return nil, err } @@ -123,7 +130,7 @@ func (s *storageFile) GetBreadcrumbs(ctx context.Context, id int64) ([]*crm.Cate if err != nil { return nil, err } - data, err := os.ReadFile("resources/catalog.json") + data, err := s.readFile("catalog") if err != nil { 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) { - data, err := os.ReadFile("resources/products.json") + data, err := s.readFile("products") if err != nil { return nil, err } @@ -182,3 +189,16 @@ func (s *storageFile) GetPositionsByText(_ context.Context, text string) ([]*crm } 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 +}