 
 ;;;; Implementation
 
-(def ^:private lua-message-status
-  ;; Careful, logic here's quite subtle: see state diagram for assistance
-  "--
-   local now         = tonumber(_:now)
-   local lock_exp    = tonumber(redis.call('hget', _:qk-locks,    _:mid) or 0)
-   local backoff_exp = tonumber(redis.call('hget', _:qk-backoffs, _:mid) or 0)
-   local state       = nil
-   if redis.call('hexists', _:qk-messages, _:mid) == 1 then
-     if redis.call('sismember', _:qk-done, _:mid) == 1 then
-       if (now < backoff_exp) then
-         if redis.call('sismember', _:qk-requeue, _:mid) == 1 then
-           state = 'done-with-requeue'
+(def ^:private script-with-msg-status
+  (memoize
+   (fn [mid-arg? preamble conclusion]
+     (format "%s\n%s\n%s"
+      (or preamble "")
+      (str (when mid-arg? "local mid = _:mid\n")
+       ;; Careful, logic here's quite subtle: see state diagram for assistance
+       "--
+       local now         = tonumber(_:now)
+       local lock_exp    = tonumber(redis.call('hget', _:qk-locks,    mid) or 0)
+       local backoff_exp = tonumber(redis.call('hget', _:qk-backoffs, mid) or 0)
+       local state       = nil
+       if redis.call('hexists', _:qk-messages, mid) == 1 then
+         if redis.call('sismember', _:qk-done, mid) == 1 then
+           if (now < backoff_exp) then
+             if redis.call('sismember', _:qk-requeue, mid) == 1 then
+               state = 'done-with-requeue'
+             else
+               state = 'done-with-backoff'
+             end
+           else
+             state = 'done-awaiting-gc'
+           end
          else
-           state = 'done-with-backoff'
+           if (now < lock_exp) then
+             if redis.call('sismember', _:qk-requeue, mid) == 1 then
+               state = 'locked-with-requeue'
+             else
+               state = 'locked'
+             end
+             elseif (now < backoff_exp) then
+             state = 'queued-with-backoff'
+           else
+             state = 'queued'
+           end
          end
-       else
-         state = 'done-awaiting-gc'
-       end
-     else
-       if (now < lock_exp) then
-         if redis.call('sismember', _:qk-requeue, _:mid) == 1 then
-           state = 'locked-with-requeue'
-         else
-           state = 'locked'
-         end
-       elseif (now < backoff_exp) then
-         state = 'queued-with-backoff'
-       else
-         state = 'queued'
-       end
-     end
-   end")
+       end")
+      conclusion))))
+
+(comment (script-with-msg-status :mid-arg "preamble" "conclusion"))
 
 (defn message-status
   "Returns current message status, e/o:

    nil                   - Already GC'd or invalid message id."
  [qname mid]
  (car/parse-keyword
   (car/lua ; Careful, logic here's quite subtle: see state diagram for assistance
    (format "%s\n%s" lua-message-status "return state")
    {:qk-messages (qkey qname :messages)
     :qk-locks    (qkey qname :locks)
     :qk-backoffs (qkey qname :backoffs)

  [qname message & [unique-message-id allow-requeue?]]
  (->>
   (car/lua
    (script-with-msg-status :mid-arg nil
     "--
     if (_:allow-requeue? == 'true') and
        ((state == 'locked') or (state == 'done-with-backoff')) and
