               version = +version
               plan.product = {prefix, category, item, version}
 
-              plan.lastUpdate = new Date()
+              plan.lastUpdate = (new Date()).getTime()
 
               plan.save ->
                 cb null, plan

            unless code.match /^([a-zA-Z0-9-]+_){3}[0-9]+$/
              cb()
            else
              if k not in mapCached
                plan = new JRecurlyPlan
                plan.code = code
              else
                plan = mapAll[k]

              plan.title = title
              plan.desc = desc

    recurlyPlan.lastUpdate = new Date()
    recurlyPlan.save -> callback

  @getPlans = secure (client, filter..., callback)->
    [prefix, category, item] = filter
    selector = {}
    selector["product.prefix"] = prefix  if prefix
    selector["product.category"] = category  if category
    selector["product.item"] = item  if item

    unless forceRefresh
      JRecurlyPlan.all selector, callback
    else
      JRecurlyPlan.one {}, (err, plan)=>
        callback err  if err
        unless plan
          @updateCache -> JRecurlyPlan.all selector, callback
        else
          plan.lastUpdate ?= 0
          now = (new Date()).getTime()
          if now - plan.lastUpdate > 1000 * 60 * 2
            @updateCache -> JRecurlyPlan.all selector, callback
          else
            JRecurlyPlan.all selector, callback

  # Recurly web hook will use this method to invalidate the cache.
  @updateCache = (callback)->
