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

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

  @getPlans = secure (client, prefix, callback)->
    unless forceRefresh
      JRecurlyPlan.all {'product.prefix': prefix}, callback
    else
      JRecurlyPlan.one {}, (err, plan)=>
        callback err  if err
        unless plan
          @updateCache -> JRecurlyPlan.all {'product.prefix': prefix}, callback
        else
          plan.lastUpdate ?= 0
          now = new Date()
          if now - plan.lastUpdate > 100 * 60 * 30
            @updateCache -> JRecurlyPlan.all {'product.prefix': prefix}, callback
          else
            JRecurlyPlan.all {'product.prefix': prefix}, callback

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

              version = +version
              plan.product = {prefix, category, item, version}

              plan.lastUpdate = (new Date()).getTime()

              plan.save ->
                cb null, plan
