-const pcre2_code * cre = regex_compile(req->re,
- req->caseless ? MCS_CASELESS | MCS_CACHEABLE : MCS_CACHEABLE,
- &errstr, pcre_gen_cmp_ctx);
+const pcre2_code * cre = NULL;
+
+if (regex_cachesize >= REGEX_CACHESIZE_LIMIT)
+ errstr = US"regex cache size limit reached";
+else if ((cre = regex_compile(req->re,
+ req->caseless ? MCS_CASELESS | MCS_CACHEABLE : MCS_CACHEABLE,
+ &errstr, pcre_gen_cmp_ctx)))
+ regex_cachesize++;