2017-05-22 44 views

回答

0

您可以維護緩存以避免多次調用ERP系統。

這裏是一個示例代碼,您可以嘗試實現緩存 -

CustomCache.java

public class CustomCache 
{ 

    @Resource(name = customCacheRegion) 
    protected CacheAccess customCacheAccess; 

    //Fetch result from cache 
    public ResultData readCachedData(final B2BUnitModel customer, final Date date) 
    { 
     return (ResultData) customCacheAccess.get(createCacheKey(customer, date)); 
    } 

    //Update result to cache 
    public void cacheResult(final B2BUnitModel customer, final Date date, 
     final ResultData resultData) 
    { 

     try 
     { 
      customCacheAccess.put(createCacheKey(customer, date), resultData); 

     } 
     catch (final SAPHybrisCacheException e) 
     { 
     //error 
     } 
    } 

    protected CustomCacheKey createCacheKey(final B2BUnitModel customer, final Date date) 
    { 
     return new CustomCacheKey(customer, date); 
    } 

} 

緩存鍵 -

public class CustomCacheKey extends AbstractCacheKey 
    { 
     private final B2BUnitModel customer; 

     private final Date date; 

     @Override 
     public int hashCode() 
     { 
      final int prime = 31; 
      int result = super.hashCode(); 
      result = prime * result + ((customer == null) ? 0 : customer.hashCode()); 
      return result; 
     } 

     @Override 
     public boolean equals(final Object obj) 
     { 
      if (obj == null) 
      { 
       return false; 
      } 

      if (!super.equals(obj)) 
      { 
       return false; 
      } 

      final CustomCacheKey customCacheKey = (CustomCacheKey) obj; 
      if (customer == null) 
      { 
       if (customCacheKey.customer != null) 
       { 
        return false; 
       } 
      } 
      else if (!customer.equals(customCacheKey.customer)) 
      { 
       return false; 
      } 

      if (date == null) 
      { 
       if (customCacheKey.date != null) 
       { 
       return false; 
      } 
     } 
     else if (!DateUtils.isSameDay(date, customCacheKey.date)) 
     { 
      return false; 
     } 

     return true; 
    } 
} 

* -spring.xml -

<bean id="customCacheRegion" parent="sapCoreCacheRegion"> 
    <constructor-arg name="name" 
     value="customCacheRegion" /> 
    <constructor-arg name="maxEntries" value="10000" /> 
    <constructor-arg name="evictionPolicy" value="FIFO" /> 
    <constructor-arg name="statsEnabled" value="true" /> 
    <constructor-arg name="exclusiveComputation" value="false" /> 
    <constructor-arg name="ttlSeconds" value="300" /> 
</bean> 

<bean 
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> 
    <property name="targetObject" ref="cacheRegionsList" /> 
    <property name="targetMethod" value="add" /> 
    <property name="arguments"> 
     <ref bean="customCacheRegion" /> 
    </property> 
</bean> 

所以緩存是一種地圖,您可以定義鍵值對,並獲取從密鑰本身的緩存值。

最後在您的服務層,在調用ERP系統之前,只需檢查是否針對特定客戶(或您的情況中的其他條件),數據是否在緩存中可用。如果可用,則直接從緩存中獲取它,否則調用ERP系統並將結果更新到緩存。

+0

謝謝Shreshtt Bhatt。它真的幫助我。 – racha11

+0

@ racha11如果它可行,請您接受這個答案:) –

+0

如果同一個用戶會更改條目/產品,那麼應該如何區分是否應該返回上一個緩存價格或者應該調用ERP? –