Storing Personalization Calculation Results

The CxService provides the calculateAndStorePersonalization method, which in its default implementation stores the results in the database.

The method is used in the case of asynchronous calculation of results. Results can be identified by their key, which is generated based on user, catalog version and session information.

Tasks Executed after Closing the Session

Once the user session is closed, there might be a need to execute additional tasks, such as cleaning the calculation result. To easily handle this need, we have provided a session close listener. The listener can schedule a task either for anonymous or registered user. For more details on the task service refer to The Task Service.

You will find the listener configuration in the personalizationservices-spring.xml file.
...
    <bean id="cxBeforeSessionCloseListener" class="de.hybris.platform.personalizationservices.events.CxBeforeSessionCloseListener"
            parent="abstractEventListener">
            <property name="taskService" ref="taskService"/>
            <property name="modelService" ref="modelService"/>
            <property name="userService" ref="userService"/>
            <property name="defaultSessionTokenService" ref="defaultSessionTokenService" />
            <property name="cxConfigurationService" ref="cxConfigurationService"/>
            <property name="cxUserSegmentSessionService" ref="cxUserSegmentSessionService"/>
            <property name="anonymousUserTaskRunnerName" value="cleanCxSessionResultsTaskRunner"/>
            <property name="registeredUserTaskRunnerName" value="cxRegisteredUserSessionCloseTaskRunner"/>
    </bean>
 
    <bean id="cleanCxSessionResultsTaskRunner" class="de.hybris.platform.personalizationservices.task.CleanCxSessionResultsTaskRunner" >
            <property name="actionResultDao" ref="cxActionResultDao"/>
            <property name="modelService" ref="modelService"/>
    </bean>
 
    <bean id="cxRegisteredUserSessionCloseTaskRunner" class="de.hybris.platform.personalizationservices.task.CxRegisteredUserSessionCloseTaskRunner" >
            <property name="modelService" ref="modelService"/>
            <property name="userService" ref="userService"/>
            <property name="cxUserSegmentService" ref="cxUserSegmentService"/>
            <property name="cxUserSegmentConversionHelper" ref="cxUserSegmentConversionHelper"/>
    </bean>
...

Anonymous User Task

A task for an anonymous user is defined to avoid storing large amount of results for anonymous users in the database. It cleans the already calculated personalization results, which are no longer needed once the session for anonymous user is closed.

Registered User Task

The current implementation of a task triggered for registered user is responsible for storing user segments in the database. It is used when the registered user segments are set to be stored in session. You can define this setting in your personalization configuration. For details, see Set Up a Personalization Configuration. User segments are stored for the current base site.

Cron Job Mechanism for Cleaning Results

Additionally, you can use a cronJob to remove old personalization calculation results. The cronJob is using the maintenance cron job mechanism described in The Cronjob Service.

  • Cronjob: The CxResultsCleaningCronJob item type is defined in the personalizationservices-items.xml file.
    There are two attributes defining which results should be removed:
    • resultMaxAge: Results older than a specified number of seconds is removed. The default value is 172800 (2 days).
    • anonymous: Defines what type of results should be removed (anonymous or regular customer results). The default value is true.
  • CxResultsCleanupStrategy: Finds results based on given parameters and removes them. Results are cleanup in bundles (batches). Batch size is configured by setting personalizationservices.calculation.results.cleanup.pageSize property in local.properties file. Default value is 1000.
    ...
      <bean id="cleanupCxResultsJobPerformable" parent="abstractGenericMaintenanceJobPerformable">
    <property name="pageSize" value="#{configurationService.configuration.getProperty('personalizationservices.calculation.results.cleanup.pageSize')}"/>
          <property name="maintenanceCleanupStrategy" ref="cxResultsCleanupStrategy"/>
      </bean>
    
      <alias name="defaultCxResultsCleanupStrategy" alias="cxResultsCleanupStrategy"/>
      <bean id="defaultCxResultsCleanupStrategy" class="de.hybris.platform.personalizationservices.strategies.impl.CxResultsCleanupStrategy">
          <property name="timeService" ref="timeService"/>
          <property name="modelService" ref="modelService"/>
      </bean>
    ... 
  • Trigger: The default CxResultsCleaningCronJob is created during the init/update process. The trigger is set to 4:05: AM daily. The script located in the /personalizationservices/resources/impex directory provides an example of adding a cronjob. You can configure it or create new job using the Backoffice Administration Cockpit.
    INSERT_UPDATE CxResultsCleaningCronJob; code[unique=true];job(code);singleExecutable;sessionLanguage(isocode);anonymous;maxResultsAge;
    ;cleanupCxResultsCronJob;cleanupCxResultsJobPerformable;false;de;true;172800;
     
    INSERT_UPDATE Trigger;cronJob(code)[unique=true];second;minute;hour;day;month;year;relative;active;maxAcceptableDelay
    # Run the cleanupCxResultsCronJob at 4:05 AM every day
    ;cleanupCxResultsCronJob;0;5;4;-1;-1;-1;false;true;-1