您如何设计 Hybris 与 SAP ERP 的 B2B 应用定价集成



如果ERP中有一个定价模块可以实时计算每个用户的价格,那么有什么方法可以在不牺牲性能的情况下获得它?

您可以维护缓存以避免多次调用ERP系统。

下面是一个示例代码,您可以尝试实现缓存 --

自定义缓存.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;
     }
}

*-弹簧.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系统并将结果更新到缓存。

最新更新