chore: add request cache

This commit is contained in:
Steven 2023-09-22 00:13:11 +08:00
parent 41eea8b571
commit 4e3d727b58
5 changed files with 37 additions and 6 deletions

View File

@ -51,6 +51,8 @@ linters-settings:
disabled: true disabled: true
- name: early-return - name: early-return
disabled: true disabled: true
- name: disable-stuttering-check
disabled: true
gocritic: gocritic:
disabled-checks: disabled-checks:
- ifElseChain - ifElseChain

1
go.mod
View File

@ -80,6 +80,7 @@ require (
github.com/h2non/filetype v1.1.3 github.com/h2non/filetype v1.1.3
github.com/improbable-eng/grpc-web v0.15.0 github.com/improbable-eng/grpc-web v0.15.0
github.com/mssola/useragent v1.0.0 github.com/mssola/useragent v1.0.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
go.deanishe.net/favicon v0.1.0 go.deanishe.net/favicon v0.1.0
go.uber.org/zap v1.21.0 go.uber.org/zap v1.21.0

2
go.sum
View File

@ -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/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/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/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/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 v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ= github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=

24
plugin/license/cache.go Normal file
View File

@ -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)
}

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"time"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -21,7 +22,7 @@ const (
subscriptionProProductID = 98995 subscriptionProProductID = 98995
) )
type licenseKey struct { type LicenseKey struct {
ID int32 `json:"id"` ID int32 `json:"id"`
Status string `json:"status"` Status string `json:"status"`
Key string `json:"key"` Key string `json:"key"`
@ -29,7 +30,7 @@ type licenseKey struct {
ExpiresAt *string `json:"updated_at"` ExpiresAt *string `json:"updated_at"`
} }
type licenseKeyMeta struct { type LicenseKeyMeta struct {
StoreID int32 `json:"store_id"` StoreID int32 `json:"store_id"`
OrderID int32 `json:"order_id"` OrderID int32 `json:"order_id"`
OrderItemID int32 `json:"order_item_id"` OrderItemID int32 `json:"order_item_id"`
@ -45,15 +46,15 @@ type licenseKeyMeta struct {
type ValidateLicenseKeyResponse struct { type ValidateLicenseKeyResponse struct {
Valid bool `json:"valid"` Valid bool `json:"valid"`
Error *string `json:"error"` Error *string `json:"error"`
LicenseKey *licenseKey `json:"license_key"` LicenseKey *LicenseKey `json:"license_key"`
Meta *licenseKeyMeta `json:"meta"` Meta *LicenseKeyMeta `json:"meta"`
} }
type ActiveLicenseKeyResponse struct { type ActiveLicenseKeyResponse struct {
Activated bool `json:"activated"` Activated bool `json:"activated"`
Error *string `json:"error"` Error *string `json:"error"`
LicenseKey *licenseKey `json:"license_key"` LicenseKey *LicenseKey `json:"license_key"`
Meta *licenseKeyMeta `json:"meta"` Meta *LicenseKeyMeta `json:"meta"`
} }
func ValidateLicenseKey(licenseKey string, instanceName string) (*ValidateLicenseKeyResponse, error) { func ValidateLicenseKey(licenseKey string, instanceName string) (*ValidateLicenseKeyResponse, error) {
@ -97,6 +98,7 @@ func ValidateLicenseKey(licenseKey string, instanceName string) (*ValidateLicens
return nil, errors.New("invalid store or product id") return nil, errors.New("invalid store or product id")
} }
} }
licenseCache.Set("key", "value", 24*time.Hour)
return &response, nil return &response, nil
} }