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

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"
"io"
"net/http"
"time"
"github.com/pkg/errors"
)
@ -21,7 +22,7 @@ const (
subscriptionProProductID = 98995
)
type licenseKey struct {
type LicenseKey struct {
ID int32 `json:"id"`
Status string `json:"status"`
Key string `json:"key"`
@ -29,7 +30,7 @@ type licenseKey struct {
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"`
@ -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) {
@ -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
}