Skip to content
This repository has been archived by the owner on Jul 27, 2018. It is now read-only.

Commit

Permalink
JAV-254 avoided querying db in a loop
Browse files Browse the repository at this point in the history
  • Loading branch information
seanyinx committed Aug 27, 2017
1 parent 1a2ce68 commit 1a1381c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
package io.servicecomb.poc.demo.seckill.repositories;

import io.servicecomb.poc.demo.seckill.Promotion;
import java.util.Collection;
import java.util.List;
import org.springframework.data.repository.PagingAndSortingRepository;

public interface PromotionRepository extends PagingAndSortingRepository<Promotion, Integer> {
List<Promotion> findByIdGreaterThan(int id);

Promotion findTopByPromotionId(String promotionId);

List<Promotion> findByPromotionIdIn(Collection<String> promotionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.servicecomb.poc.demo.seckill.repositories.PromotionRepository;
import io.servicecomb.poc.demo.seckill.repositories.SpringBasedPromotionEventRepository;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
Expand Down Expand Up @@ -73,7 +74,7 @@ public Collection<Promotion> getActivePromotions() {
}

private void populatePromotionEvents(List<PromotionEvent<T>> promotionEvents) {
Set<String> newActivePromotionIds = ConcurrentHashMap.newKeySet();
Set<String> newActivePromotionIds = new HashSet<>();
for (PromotionEvent<T> promotionEvent : promotionEvents) {
if (PromotionEventType.Grab.equals(promotionEvent.getType())) {
customerCoupons.computeIfAbsent(promotionEvent.getCustomerId(), id -> new ConcurrentLinkedQueue<>())
Expand All @@ -94,11 +95,9 @@ private void populatePromotionEvents(List<PromotionEvent<T>> promotionEvents) {
}

//add new active promotion to cache together
if (newActivePromotionIds.size() != 0) {
for (String activePromotionId : newActivePromotionIds) {
Promotion activePromotion = promotionRepository.findTopByPromotionId(activePromotionId);
activePromotions.put(activePromotion.getPromotionId(), activePromotion);
}
if (!newActivePromotionIds.isEmpty()) {
promotionRepository.findByPromotionIdIn(newActivePromotionIds)
.forEach(promotion -> activePromotions.put(promotion.getPromotionId(), promotion));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import io.servicecomb.poc.demo.seckill.repositories.SpringBasedPromotionEventRepository;
import java.time.ZonedDateTime;
import java.util.Date;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -63,8 +63,8 @@ public class SecKillQueryServiceApplicationTest {
@Autowired
private MockMvc mockMvc;

@After
public void tearDown() throws Exception {
@Before
public void setUp() throws Exception {
promotionEventRepository.deleteAll();
promotionRepository.deleteAll();
}
Expand All @@ -74,7 +74,7 @@ public void grabbedCouponsCanBeQueried() throws Exception {
addCouponToCustomer(customerId, promotion1);
addCouponToCustomer("unknown", promotion2);

Thread.sleep(1000);
Thread.sleep(300);

mockMvc.perform(get("/query/coupons/{customerId}", customerId).contentType(APPLICATION_JSON))
.andExpect(status().isOk())
Expand All @@ -87,7 +87,7 @@ public void grabbedCouponsCanBeQueried() throws Exception {

addCouponToCustomer(customerId, promotion3);

Thread.sleep(1000);
Thread.sleep(300);

mockMvc.perform(get("/query/coupons/{customerId}", customerId).contentType(APPLICATION_JSON))
.andExpect(status().isOk())
Expand Down

0 comments on commit 1a1381c

Please sign in to comment.