Monday, July 31, 2017

Creating a New Custom Cache Region in Hybris


  • Create a new cahce region bean in core-cache.xml spring file. The below code is to cache only the products
   <bean name="productCacheRegion" class="de.hybris.platform.regioncache.region.impl.EHCacheRegion">
        <constructor-arg name="name" value="productCacheRegion" />
        <constructor-arg name="maxEntries" value="50000" />
        <constructor-arg name="evictionPolicy" value="LFU" />
        <property name="handledTypes">
            <array>
                <value>1</value>
            </array>
        </property>
 </bean>
    

  • Access caching by using the DefaultCacheController
Reference

Webserver based media serving in Hybris


  • Create an extension called staticmedia with a new class named ApacheMediaUrlStrategy that implements MediaURLStrategy interface. By default Hybris uses a base-64 encoded context request parameter that will not work properly in Apache. The ApacheMediaUrlStrategy uses a folder stategy so that apache can directly point to that directory. The below code in the class returns back //hostName/static/media/ in witch mediaHostUrl is an instance variable in ApacheMediaUrlStrategy class that contains hostName/static/media
return new StringBuilder(mediaHostUrl.length() + location.length() + 3).append("//").append(mediaHostUrl).append('/').append(location).toString();
  • Add configuration properties to the local.properties to specify the mediaHostUrlwith an appropriate URL that points to you media serving apache layer and change the mediaurlstrategy
media.apachemedia.url=
media.folder.images.url.strategy=apacheMediaURLStrategy
  • Add the new strategy to the staticmedia-spring.xml file
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    <bean id="apacheMediaURLStrategy" class="com.package.staticmedia.media.url.impl.ApacheMediaUrlStrategy">
        <property name="mediaHostUrl" value="#{configurationService.configuration.getProperty('media.apachemedia.url')}" />
    </bean>
</beans>
  • Create ysynch to copy the image contents to the media.apachemedia.url
  • Modify httpd.conf file to hanlde the media url inside the VirtualDirectory with 7 days of  cache expiry for images and 10 minutes for the _ui directory
SetEnvIf Request_URI "^/images/*" no-jk
Alias /images /opt/hybris/media/sys_master/images "/opt/hybris/media/sys_master/images">
Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        Allow from all
        Header append Cache-Control public,max-age=604800

    SetEnvIf Request_URI "^/_ui/*" no-jk
    Alias /_ui /opt/hybris/storefront/web/webroot/_ui    "/opt/hybris/storefront/web/webroot/_ui">
        Options FollowSymLinks MultiViews Indexes 
   AllowOverride None
        Order allow,deny
        Allow from all
        Header append Cache-Control public,max-age=600
Reference
Hybris Wiki Static Contents from Apache
Wiki Versioned Static content