Skip to content

Commit

Permalink
integrate bigcache package to banners, brands & product categories
Browse files Browse the repository at this point in the history
  • Loading branch information
Kervin Christianata committed Jun 17, 2022
1 parent 67fc3ba commit baa46b0
Show file tree
Hide file tree
Showing 28 changed files with 1,946 additions and 11 deletions.
1 change: 0 additions & 1 deletion .vscode/dryrun.log
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ make --dry-run --always-make --keep-going --print-directory
make: Entering directory `/Users/kervin/Documents/kin-api'
echo 'Usage:'
sed -n 's/^##//p' Makefile .envrc | column -t -s ':' | sed -e 's/^/ /'

make: Leaving directory `/Users/kervin/Documents/kin-api'

18 changes: 9 additions & 9 deletions .vscode/targets.log
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ make all --print-data-base --no-builtin-variables --no-builtin-rules --question
make: *** No rule to make target `all'. Stop.


# Make data base, printed on Tue Jun 14 19:35:41 2022
# Make data base, printed on Thu Jun 16 10:17:02 2022

# Variables

Expand Down Expand Up @@ -47,7 +47,7 @@ __CFBundleIdentifier = com.microsoft.VSCode
# environment
INFOPATH = /opt/homebrew/share/info:
# environment
VSCODE_IPC_HOOK_EXTHOST = /var/folders/48/vg8936dx17j9pqslg2hm5h_c0000gn/T/vscode-ipc-e2ea059a-d5ef-411e-b82a-6becfbb36b6f.sock
VSCODE_IPC_HOOK_EXTHOST = /var/folders/48/vg8936dx17j9pqslg2hm5h_c0000gn/T/vscode-ipc-93367032-61ed-46e7-a747-686d1f42b047.sock
# environment
VSCODE_CWD = /
# environment
Expand Down Expand Up @@ -89,7 +89,7 @@ HOME = /Users/kervin
# default
MAKEFILEPATH = $(shell /usr/bin/xcode-select -print-path 2>/dev/null || echo /Developer)/Makefiles
# environment
VSCODE_CODE_CACHE_PATH = /Users/kervin/Library/Application Support/Code/CachedData/c3511e6c69bb39013c4a4b7b9566ec1ca73fc4d5
VSCODE_CODE_CACHE_PATH = /Users/kervin/Library/Application Support/Code/CachedData/4af164ea3a06f701fe3e89a2bcbb421d2026b68f
# environment
LOGNAME = kervin
# environment
Expand Down Expand Up @@ -125,15 +125,15 @@ TMPDIR = /var/folders/48/vg8936dx17j9pqslg2hm5h_c0000gn/T/
# automatic
*F = $(notdir $*)
# environment
VSCODE_IPC_HOOK = /Users/kervin/Library/Application Support/Code/1.67.2-main.sock
VSCODE_IPC_HOOK = /Users/kervin/Library/Application Support/Code/1.68.0-main.sock
# makefile
MAKEFLAGS = Rrqp
# environment
MFLAGS = -Rrqp
# automatic
*D = $(patsubst %/,%,$(dir $*))
# environment
XPC_SERVICE_NAME = application.com.microsoft.VSCode.8184449.8184455
XPC_SERVICE_NAME = application.com.microsoft.VSCode.9477782.9477788
# environment
HOMEBREW_PREFIX = /opt/homebrew
# automatic
Expand Down Expand Up @@ -171,11 +171,10 @@ MAKELEVEL := 0
# environment
LANG = C
# environment
VSCODE_PID = 6564
VSCODE_PID = 86867
# makefile (from `.envrc', line 10)
XENDIT_CALLBACK_VERIFICATION_TOKEN = b8f54fc11fafd8b69cb9bb4f37c2be27307a7e30a91e8a477a3986fadd672967
# variable set hash-table stats:

# Load=80/1024=8%, Rehash=0, Collisions=4/106=4%

# Pattern-specific Variable Values
Expand Down Expand Up @@ -301,6 +300,7 @@ db/psql:
# Implicit rule search has not been done.
# File does not exist.
# File has not been updated.

# commands to execute (from `Makefile', line 29):
psql ${DB_DSN}

Expand Down Expand Up @@ -340,7 +340,6 @@ run/api:
# File does not exist.
# File has not been updated.
# commands to execute (from `Makefile', line 24):

go run ./cmd/api -db-dsn=${DB_DSN}


Expand All @@ -354,6 +353,7 @@ vendor:
go mod tidy
go mod verify
@echo 'Vendoring dependencies...'

go mod vendor


Expand All @@ -370,6 +370,6 @@ vendor:
# strcache size: total = 4096 / max = 4096 / min = 4096 / avg = 4096
# strcache free: total = 4080 / max = 4080 / min = 4080 / avg = 4080

# Finished Make data base on Tue Jun 14 19:35:41 2022
# Finished Make data base on Thu Jun 16 10:17:02 2022


22 changes: 22 additions & 0 deletions cmd/api/banners.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -202,12 +203,33 @@ func (app *application) deleteBannerHandler(w http.ResponseWriter, r *http.Reque
// ====================================================================================

func (app *application) getBannersHandler(w http.ResponseWriter, r *http.Request) {
entry, _ := app.cache.Get("GET_BANNERS_API")
if entry != nil {
var e any
err := json.Unmarshal(entry, &e)
if err != nil {
if err != nil {
app.serverErrorResponse(w, r, err)
return
}
}

err = app.writeJSON(w, http.StatusOK, http.StatusText(http.StatusOK), e, nil)
if err != nil {
app.serverErrorResponse(w, r, err)
}
return
}

banners, err := app.models.Banners.GetAPI()
if err != nil {
app.serverErrorResponse(w, r, err)
return
}

b, _ := json.Marshal(banners)
app.cache.Set("GET_BANNERS_API", b)

err = app.writeJSON(w, http.StatusOK, http.StatusText(http.StatusOK), banners, nil)
if err != nil {
app.serverErrorResponse(w, r, err)
Expand Down
23 changes: 22 additions & 1 deletion cmd/api/brands.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -221,13 +222,33 @@ func (app *application) deleteBrandHandler(w http.ResponseWriter, r *http.Reques
// ====================================================================================

func (app *application) getBrandsHandler(w http.ResponseWriter, r *http.Request) {
brands, err := app.gorm.Brands.GetAPI()
entry, _ := app.cache.Get("GET_BRANDS_API")
if entry != nil {
var e any
err := json.Unmarshal(entry, &e)
if err != nil {
if err != nil {
app.serverErrorResponse(w, r, err)
return
}
}

err = app.writeJSON(w, http.StatusOK, http.StatusText(http.StatusOK), e, nil)
if err != nil {
app.serverErrorResponse(w, r, err)
}
return
}

brands, err := app.gorm.Brands.GetAPI()
if err != nil {
app.serverErrorResponse(w, r, err)
return
}

b, _ := json.Marshal(brands)
app.cache.Set("GET_BRANDS_API", b)

err = app.writeJSON(w, http.StatusOK, http.StatusText(http.StatusOK), brands, nil)
if err != nil {
app.serverErrorResponse(w, r, err)
Expand Down
5 changes: 5 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"gorm.io/driver/postgres"
"gorm.io/gorm"

"github.com/allegro/bigcache/v3"
"github.com/kervinch/internal/data"
"github.com/kervinch/internal/jsonlog"
"github.com/kervinch/internal/mailer"
Expand Down Expand Up @@ -69,6 +70,7 @@ type application struct {
wg sync.WaitGroup
s3 s3.S3
xendit xendit.Xendit
cache bigcache.BigCache
}

func main() {
Expand Down Expand Up @@ -131,6 +133,8 @@ func main() {
return time.Now().Unix()
}))

bigcache, _ := bigcache.NewBigCache(bigcache.DefaultConfig(5 * time.Hour))

// Declare an instance of the application struct, containing the config struct and the logger.
app := &application{
config: cfg,
Expand All @@ -140,6 +144,7 @@ func main() {
mailer: mailer.New(cfg.smtp.host, cfg.smtp.port, cfg.smtp.username, cfg.smtp.password, cfg.smtp.sender),
s3: s3.New("kin-public"),
xendit: xendit.New(os.Getenv("XENDIT_SECRET_KEY")),
cache: *bigcache,
}

err = app.serve()
Expand Down
22 changes: 22 additions & 0 deletions cmd/api/product_categories.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -237,12 +238,33 @@ func (app *application) deleteProductCategoryHandler(w http.ResponseWriter, r *h
// ====================================================================================

func (app *application) getProductCategoriesHandler(w http.ResponseWriter, r *http.Request) {
entry, _ := app.cache.Get("GET_PRODUCT_CATEGORIES_API")
if entry != nil {
var e any
err := json.Unmarshal(entry, &e)
if err != nil {
if err != nil {
app.serverErrorResponse(w, r, err)
return
}
}

err = app.writeJSON(w, http.StatusOK, http.StatusText(http.StatusOK), e, nil)
if err != nil {
app.serverErrorResponse(w, r, err)
}
return
}

productCategories, err := app.gorm.ProductCategories.GetAPI()
if err != nil {
app.serverErrorResponse(w, r, err)
return
}

pc, _ := json.Marshal(productCategories)
app.cache.Set("GET_PRODUCT_CATEGORIES_API", pc)

err = app.writeJSON(w, http.StatusOK, http.StatusText(http.StatusOK), productCategories, nil)
if err != nil {
app.serverErrorResponse(w, r, err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/kervinch
go 1.18

require (
github.com/allegro/bigcache/v3 v3.0.2
github.com/aws/aws-sdk-go v1.43.24
github.com/felixge/httpsnoop v1.0.2
github.com/go-mail/mail/v2 v2.3.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/allegro/bigcache/v3 v3.0.2 h1:AKZCw+5eAaVyNTBmI2fgyPVJhHkdWder3O9IrprcQfI=
github.com/allegro/bigcache/v3 v3.0.2/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I=
github.com/aws/aws-sdk-go v1.43.24 h1:7c2PniJ0wpmWsIA6OtYBw6wS7DF0IjbhvPq+0ZQYNXw=
github.com/aws/aws-sdk-go v1.43.24/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
Expand Down
28 changes: 28 additions & 0 deletions vendor/github.com/allegro/bigcache/v3/.codecov.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions vendor/github.com/allegro/bigcache/v3/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit baa46b0

Please sign in to comment.