Day 12 – Persistant Variables

Goal: Test using app level and entity/user level persistant variables.

ruleset a60x495 {
  meta {
    name "persistent-variables-example"
    description <<
    author "Mike Grace"
    logging on

  rule show_off_app_and_ent_persistant_variables {
    select when pageview ".*"
    pre {
      userRuns = ent:userRuns || 0;
      newUserRuns = userRuns + 1;
      userHasRunBefore = ent:hasRun;
      userRunAsBool = (userHasRunBefore == 0) => "false" | "true";

      appRuns = app:appRuns || 0;
      newAppRuns = appRuns + 1;
      appHasRunBefore = app:hasRun;
      appRunAsBool = (appHasRunBefore == 0) => "false" | "true";

      stats =<<
        <p>userRuns: #{newUserRuns}</p>
        <p>userHasRunBefore: #{userRunAsBool}</p>
        <p>appRuns: #{newAppRuns}</p>
        <p>appHasRunBefore: #{appRunAsBool}</p>
      notify("Status of app", stats) with sticky = true;
    fired {
      set ent:userRuns newUserRuns;
      set ent:hasRun;
      set app:appRuns newAppRuns;
      set app:hasRun;

  rule clear_user_persistance {
    select when pageview "\/\?clear"
      notify("Your persistance has been cleared",":)") with sticky = true;
    fired {
      clear ent:userRuns;
      clear ent:hasRun;
      // clear app:appRuns;
      // clear app:hasRun;
  • 14 get entity/user persistant variable and if not set yet, return 0;
  • 16 ent:hasRun will be set as a flag so it will return 0 for false and 1 for true;
  • 17 convert flag from 0/1 to false/true using conditional expression
  • 19 get app persistant variable for how many times the app has been run and return 0 if not previously set
  • 21 get flag to check if the app has run before
  • 22 convert 0/1 to false/true
  • 34-39 postlude block – set new values/update values of persistant variables
  • 35 sets entity variable ‘userRuns’ with value of ‘newUserRuns’
  • 36 sets entity variable ‘hasRun’ as a flag with the new value of 1 which means true
  • 42-53 rule to allow user to clear entity variables
  • 43 only selects on URL
  • 47-52 postlude block
  • 48 clears any value set in entity variable ‘userRuns’
  • 50-51 commented out to show that clearing app variables is the same as entity variables but I don’t want those lines to execute should this rule run

App run first time on with bookmarklet

App run second time on with bookmarklet

App run on with bookmarklet

App run on after running on and many others having run the app with bookmarklet

  • reading and writing of persistant variables is not locked meaning that if ‘Jim’ and ‘Pam’ are running the app at the same time causing the app to read an app persistant which later gets updated by a user action, the last one to write wins.
  • Persistant variables have a hard limit of about 1 million characters at the time of this posting. Don’t abuse it or you may be getting a phone call late at night by our sys admin.
  • Go and build awesomeness with this!

Get the bookmarklet to try it out yourself!

Gratuitous day 12 Grace face

This entry was posted in Kynetx and tagged . Bookmark the permalink.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s