change logic introduced in r3649:
*) now $uid_set is cacheable again
*) allow to see at any processing stage that uid cookie is remarked
diff --git a/src/http/modules/ngx_http_userid_filter_module.c b/src/http/modules/ngx_http_userid_filter_module.c
index 2f3f054..64811e4 100644
--- a/src/http/modules/ngx_http_userid_filter_module.c
+++ b/src/http/modules/ngx_http_userid_filter_module.c
@@ -211,21 +211,6 @@
         return NGX_ERROR;
     }
 
-    if (ctx->uid_got[3] != 0) {
-
-        if (conf->mark == '\0') {
-            return ngx_http_next_header_filter(r);
-
-        } else {
-            if (ctx->cookie.len > 23
-                && ctx->cookie.data[22] == conf->mark
-                && ctx->cookie.data[23] == '=')
-            {
-                return ngx_http_next_header_filter(r);
-            }
-        }
-    }
-
     if (ngx_http_userid_set_uid(r, ctx, conf) == NGX_OK) {
         return ngx_http_next_header_filter(r);
     }
@@ -284,16 +269,13 @@
         return NGX_ERROR;
     }
 
+    if (ngx_http_userid_create_uid(r->main, ctx, conf) != NGX_OK) {
+        return NGX_ERROR;
+    }
+
     if (ctx->uid_set[3] == 0) {
-
-        if (ctx->uid_got[3] != 0) {
-            v->not_found = 1;
-            return NGX_OK;
-        }
-
-        if (ngx_http_userid_create_uid(r->main, ctx, conf) != NGX_OK) {
-            return NGX_ERROR;
-        }
+        v->not_found = 1;
+        return NGX_OK;
     }
 
     return ngx_http_userid_variable(r->main, v, &conf->name, ctx->uid_set);
@@ -379,18 +361,12 @@
     ngx_str_t         src, dst;
     ngx_table_elt_t  *set_cookie, *p3p;
 
-    if (ctx->uid_set[3] == 0) {
-        if (ctx->uid_got[3] == 0) {
-            if (ngx_http_userid_create_uid(r, ctx, conf) != NGX_OK) {
-                return NGX_ERROR;
-            }
+    if (ngx_http_userid_create_uid(r, ctx, conf) != NGX_OK) {
+        return NGX_ERROR;
+    }
 
-        } else {
-            ctx->uid_set[0] = ctx->uid_got[0];
-            ctx->uid_set[1] = ctx->uid_got[1];
-            ctx->uid_set[2] = ctx->uid_got[2];
-            ctx->uid_set[3] = ctx->uid_got[3];
-        }
+    if (ctx->uid_set[3] == 0) {
+        return NGX_OK;
     }
 
     len = conf->name.len + 1 + ngx_base64_encoded_length(16) + conf->path.len;
@@ -483,6 +459,28 @@
     struct sockaddr_in6  *sin6;
 #endif
 
+    if (ctx->uid_set[3] != 0) {
+        return NGX_OK;
+    }
+
+    if (ctx->uid_got[3] != 0) {
+
+        if (conf->mark == '\0'
+            || (ctx->cookie.len > 23
+                && ctx->cookie.data[22] == conf->mark
+                && ctx->cookie.data[23] == '='))
+        {
+            return NGX_OK;
+        }
+
+        ctx->uid_set[0] = ctx->uid_got[0];
+        ctx->uid_set[1] = ctx->uid_got[1];
+        ctx->uid_set[2] = ctx->uid_got[2];
+        ctx->uid_set[3] = ctx->uid_got[3];
+
+        return NGX_OK;
+    }
+
     /*
      * TODO: in the threaded mode the sequencers should be in TLS and their
      * ranges should be divided between threads
@@ -579,8 +577,7 @@
 
     var->get_handler = ngx_http_userid_got_variable;
 
-    var = ngx_http_add_variable(cf, &ngx_http_userid_set,
-                                NGX_HTTP_VAR_NOCACHEABLE);
+    var = ngx_http_add_variable(cf, &ngx_http_userid_set, 0);
     if (var == NULL) {
         return NGX_ERROR;
     }