2017-05-22 6 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を呼び出す必要がありますか? –

関連する問題