Skip to main content

Handling Abandoned Orders

If a user abandons an order, then the user's browser will stop sending requests to Caché. After a certain amount of time elapses between requests the session "times out". That is, the system automatically ends the session. We should provide a method that "cleans up" after a session ends. To do this we need to perform two tasks: specify where Caché can find the clean-up method and then supply the method.

We accomplish the first task by adding a line of code to the AddShow method specifying a value for the EventClass property of the %session object. In general, the value of the EventClass property tells Caché where to find methods that need to be executed when %session related events, timeouts for example, occur.

ClassMethod AddShow(ShowID As %String)
    // Get an order object
    If $data(%session.Data("Order")) {
        Set ord = ##class(Cinema.TicketOrder).%OpenId(%session.Data("Order"))
    Else {
        Set ord=##class(Cinema.TicketOrder).%New()

        // Set up a event class
        Set %session.EventClass = "Cinema.Utils"

    // ...

When the session ends, in this case due to a timeout, Caché invokes the OnEndSession method of some class. Which class is determined by the EventClass property, which we've set to “Cinema.Utils”. Note that we set the value of the EventClass property here because this Else block executes the first time that a user invokes AddShow. That is, this Else block executes the first time that a user begins the order creation process.

Our remaining task, therefore, is to add an OnEndSession method to the Utils class.

FeedbackOpens in a new tab