Skip to content

Commit

Permalink
chore: add request cache
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Sep 21, 2023
1 parent 41eea8b commit 4e3d727
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ linters-settings:
disabled: true
- name: early-return
disabled: true
- name: disable-stuttering-check
disabled: true
gocritic:
disabled-checks:
- ifElseChain
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ require (
github.com/h2non/filetype v1.1.3
github.com/improbable-eng/grpc-web v0.15.0
github.com/mssola/useragent v1.0.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
go.deanishe.net/favicon v0.1.0
go.uber.org/zap v1.21.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ github.com/openzipkin/zipkin-go v0.2.1/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnh
github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnhQw8ySjnjRyN4=
github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
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/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
Expand Down
24 changes: 24 additions & 0 deletions plugin/license/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package license

import (
"fmt"
"time"

"github.com/patrickmn/go-cache"
)

var (
licenseCache = cache.New(24*time.Hour, 24*time.Hour)
)

func SetLicenseCache(licenseKey, instanceName string, license LicenseKey) {
licenseCache.Set(fmt.Sprintf("%s-%s", licenseKey, instanceName), license, 24*time.Hour)
}

func GetLicenseCache(licenseKey, instanceName string) *LicenseKey {
cache, ok := licenseCache.Get(fmt.Sprintf("%s-%s", licenseKey, instanceName))
if !ok {
return nil
}
return cache.(*LicenseKey)
}
14 changes: 8 additions & 6 deletions plugin/license/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"net/http"
"time"

"github.com/pkg/errors"
)
Expand All @@ -21,15 +22,15 @@ const (
subscriptionProProductID = 98995
)

type licenseKey struct {
type LicenseKey struct {
ID int32 `json:"id"`
Status string `json:"status"`
Key string `json:"key"`
CreatedAt string `json:"created_at"`
ExpiresAt *string `json:"updated_at"`
}

type licenseKeyMeta struct {
type LicenseKeyMeta struct {
StoreID int32 `json:"store_id"`
OrderID int32 `json:"order_id"`
OrderItemID int32 `json:"order_item_id"`
Expand All @@ -45,15 +46,15 @@ type licenseKeyMeta struct {
type ValidateLicenseKeyResponse struct {
Valid bool `json:"valid"`
Error *string `json:"error"`
LicenseKey *licenseKey `json:"license_key"`
Meta *licenseKeyMeta `json:"meta"`
LicenseKey *LicenseKey `json:"license_key"`
Meta *LicenseKeyMeta `json:"meta"`
}

type ActiveLicenseKeyResponse struct {
Activated bool `json:"activated"`
Error *string `json:"error"`
LicenseKey *licenseKey `json:"license_key"`
Meta *licenseKeyMeta `json:"meta"`
LicenseKey *LicenseKey `json:"license_key"`
Meta *LicenseKeyMeta `json:"meta"`
}

func ValidateLicenseKey(licenseKey string, instanceName string) (*ValidateLicenseKeyResponse, error) {
Expand Down Expand Up @@ -97,6 +98,7 @@ func ValidateLicenseKey(licenseKey string, instanceName string) (*ValidateLicens
return nil, errors.New("invalid store or product id")
}
}
licenseCache.Set("key", "value", 24*time.Hour)
return &response, nil
}

Expand Down

0 comments on commit 4e3d727

Please sign in to comment.