新增功能
This commit is contained in:
Vendored
+44
@@ -0,0 +1,44 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/luxsin/app-api/internal/model"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
const (
|
||||
brandAllKey = "brand:all"
|
||||
brandTTL = 30 * time.Minute
|
||||
)
|
||||
|
||||
type BrandCache struct {
|
||||
rdb *redis.Client
|
||||
}
|
||||
|
||||
func NewBrandCache(rdb *redis.Client) *BrandCache {
|
||||
return &BrandCache{rdb: rdb}
|
||||
}
|
||||
|
||||
func (c *BrandCache) GetAll(ctx context.Context) ([]model.Brand, error) {
|
||||
data, err := c.rdb.Get(ctx, brandAllKey).Bytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var list []model.Brand
|
||||
if err := json.Unmarshal(data, &list); err != nil {
|
||||
return nil, fmt.Errorf("unmarshal brands: %w", err)
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (c *BrandCache) SetAll(ctx context.Context, list []model.Brand) error {
|
||||
data, err := json.Marshal(list)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal brands: %w", err)
|
||||
}
|
||||
return c.rdb.Set(ctx, brandAllKey, data, brandTTL).Err()
|
||||
}
|
||||
Vendored
+69
@@ -0,0 +1,69 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/luxsin/app-api/internal/model"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
const (
|
||||
modelBrandPrefix = "model:brand:"
|
||||
modelAllKey = "model:all"
|
||||
modelTTL = 30 * time.Minute
|
||||
)
|
||||
|
||||
type ModelCache struct {
|
||||
rdb *redis.Client
|
||||
}
|
||||
|
||||
func NewModelCache(rdb *redis.Client) *ModelCache {
|
||||
return &ModelCache{rdb: rdb}
|
||||
}
|
||||
|
||||
func modelBrandKey(brandName string) string {
|
||||
return modelBrandPrefix + brandName
|
||||
}
|
||||
|
||||
func (c *ModelCache) GetByBrand(ctx context.Context, brandName string) ([]model.Model, error) {
|
||||
data, err := c.rdb.Get(ctx, modelBrandKey(brandName)).Bytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var list []model.Model
|
||||
if err := json.Unmarshal(data, &list); err != nil {
|
||||
return nil, fmt.Errorf("unmarshal models: %w", err)
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (c *ModelCache) SetByBrand(ctx context.Context, brandName string, list []model.Model) error {
|
||||
data, err := json.Marshal(list)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal models: %w", err)
|
||||
}
|
||||
return c.rdb.Set(ctx, modelBrandKey(brandName), data, modelTTL).Err()
|
||||
}
|
||||
|
||||
func (c *ModelCache) GetAll(ctx context.Context) ([]model.Model, error) {
|
||||
data, err := c.rdb.Get(ctx, modelAllKey).Bytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var list []model.Model
|
||||
if err := json.Unmarshal(data, &list); err != nil {
|
||||
return nil, fmt.Errorf("unmarshal models: %w", err)
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (c *ModelCache) SetAll(ctx context.Context, list []model.Model) error {
|
||||
data, err := json.Marshal(list)
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal models: %w", err)
|
||||
}
|
||||
return c.rdb.Set(ctx, modelAllKey, data, modelTTL).Err()
|
||||
}
|
||||
Reference in New Issue
Block a user