ソースを参照

db.clickhouse

- retrieve product tags
Alexey Kim 2 年 前
コミット
ebde7775f5
3 ファイル変更34 行追加0 行削除
  1. 30 0
      db/clickhouse.go
  2. 1 0
      db/database.go
  3. 3 0
      db/product.go

+ 30 - 0
db/clickhouse.go

@@ -378,6 +378,36 @@ func (db *clickhouse) ProductVariantOptions(ln model.LanguageCode, id string) ([
 	return options, nil
 }
 
+func (db *clickhouse) ProductTags() ([]model.ProductTag, error) {
+	var (
+		tags []model.ProductTag
+		key  = productTagKey("list")
+		l    = ttlcache.LoaderFunc[string, any](
+			func(ttl *ttlcache.Cache[string, any], _ string) *ttlcache.Item[string, any] {
+				var o []model.ProductTag
+				rows, err := db.session.
+					Select(key.Selection()...).
+					From(key.Table()).
+					OrderBy("count DESC").
+					Load(&o)
+				if rows < 1 || err != nil {
+					return nil
+				}
+
+				return ttl.Set(key.String(), o, key.TTL())
+			},
+		)
+	)
+
+	if p := db.cache.Get(key.String(), ttlcache.WithLoader[string, any](l)); p != nil {
+		for _, s := range p.Value().([]model.ProductTag) {
+			tags = append(tags, s)
+		}
+	}
+
+	return tags, nil
+}
+
 func (db *clickhouse) Ping() error {
 	return db.session.Ping()
 }

+ 1 - 0
db/database.go

@@ -21,6 +21,7 @@ type Database interface {
 	ProductOptions(ln model.LanguageCode, id string) ([]*generated.ProductOption, error)
 	ProductVariants(ctx *middleware.GShopifyContext, id string) ([]*generated.ProductVariant, error)
 	ProductVariantOptions(ln model.LanguageCode, id string) ([]*generated.SelectedOption, error)
+	ProductTags() ([]model.ProductTag, error)
 
 	Collection(ln model.LanguageCode, handle *string, id *string) (*generated.Collection, error)
 	Collections(ln model.LanguageCode) ([]*generated.Collection, error)

+ 3 - 0
db/product.go

@@ -94,4 +94,7 @@ var (
 			},
 			clause, args...)
 	}
+	productTagKey = func(clause string) *cache.SqlKey {
+		return cache.NewSQLKey("product_tags", time.Minute, []string{"id, tag, products, count"}, clause)
+	}
 )