Day 17 – Detecting Recent Run of Kynetx App Within Timeframe

For an app I have been working on recently, I wanted a way to let the user know if the data they were being shown was likely to be a cached version of the data or a non cached version. Because the API I was using for the app doesn’t return a timestamp of when the API call was made, I needed a different way to “guess” when the cache expired. I thought of taking a hash of the data returned from the last call and the current API call and comparing them but that would only let me know when the API returns something different and not that it isn’t a cached version of the API call. Thus I came up with the following…

ruleset a60x507 {
  meta {
    name "ran-recently-example"
    description <<
      ran-recently-example
    >>
    author "Mike Grace"
    logging on
  }

  rule check_for_recent_run {
    select when pageview ".*"
    pre {
      msg =<<
        Because we cache our API calls for
        this app for 1 minute, you are most
        likely seeing cached data from the
        last time you ran this.
      >>;
    }
    if ent:recentlyRan == true within 1 minutes then {
      notify("Notice", msg) with sticky = true;
    }
    notfired {
      raise explicit event fresh_run
    }
  }

  rule fresh_run {
    select when explicit fresh_run
    pre {
      msg =<<
        Wohoo! Looks like the cached data
        expired so you should be seeing
        new data if there is any provided
        by the API. : )
      >>;
    }
    {
      notify("Notice",msg) with sticky = true;
    }
    fired {
      set ent:recentlyRan;
    }
  }
}
  • 12 selects on pageview event
  • 21 if the entity flag ‘recentlyRan’ is set to true within the last minute then execute the action block
  • 24 postlude block raises explicit event of ‘fresh_run’ if the action block did not fire
  • 30 rule selects on the ‘fresh_run’ explicit event
  • 42 set the entity flag ‘recentlyRan’ to true

App run on example.com several times over a 2 minute period with bookmarklet

This pattern is just a best guess solution that would be best solved by the API providing a timestamp of when the API call was made.

Get the bookmarklet to try it out yourself!

Gratuitous day 16 Grace face

About these ads
This entry was posted in Kynetx. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s