วิธีที่มีประสิทธิภาพในการรับรายการ EntityModel (Java, DXA 2.0)

Oct 28 2020

เรามีหน้าที่ค้นหาเอนทิตีด้วยเกณฑ์บางอย่าง

วิธีที่เรานำไปใช้คือการรับรายการรหัสเอนทิตีวนซ้ำ ID เพื่อดึงโมเดลเอนทิตีต่อ ID และเพิ่มลงในรายการ

รหัสหลอก:

Query query = new Query(...);
// Get list of ids from query
List<String> ids = Arrays.asList(query.executeQuery());

final WebComponentMetaFactory cmf = new WebComponentMetaFactoryImpl(brokerQuery.getPublicationId());

// Get component meta
List<ComponentMeta> componentMetas = ids.stream().map(cmf::getMeta)
            .collect(Collectors.toList());

// Get entity model per component and add it to the list
componentMetas.forEach(meta -> {
   EntityModel entityModel = contentProvider.getEntityModel(meta.getId() + templateId, webRequestContext.getLocalization())
   list.add(entityModel)
});

วิธีนี้ใช้งานได้อย่างไรก็ตามจากมุมมองด้านประสิทธิภาพนั้นช้ามากเพราะต้องเรียกcontentProvider.getEntityModel(...)หาทุก ID

มีทางออกที่ดีกว่านี้หรือไม่? บางทีอาจเป็น API จาก DXA ที่สร้างรายการ EntityModel?

คำตอบ

Velmurugan Nov 03 2020 at 06:06

ใช้สิ่งนี้ด้านล่าง CIL API เพื่อดึงเนื้อหา DCP หลายรายการโดยตัวอย่างการโทรเดียวที่ให้ไว้ด้านล่างนี้เป็นไปตาม API ที่คล้ายกันของ. NET ที่มีอยู่ใน java

string[] componentUris = query.ExecuteQuery();


            var components =
                componentUris
                    .Select(componentUri => { TcmUri uri = new TcmUri(componentUri); return (ComponentPresentation)new ComponentPresentationFactory(uri.PublicationId).FindAllComponentPresentations(componentUri)[0]; })
                    .Where(cp => cp != null)
                    .Select(cp => cp.Content)
                    .ToList();

ฉันหวังว่ามันจะช่วยได้