# Same regex caching

**URL:** <https://www.ruby-forum.com/t/same-regex-caching/209739>\
**Category:** NGINX\
**Created:** [July 21, 2011, 4:19pm UTC](https://www.ruby-forum.com/t/same-regex-caching/209739 "2011-07-21T16:19:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [July 21, 2011, 4:19pm UTC](https://www.ruby-forum.com/t/same-regex-caching/209739/1 "2011-07-21T16:19:01Z")

</div>

Hi,

I wanted to ask if the same regex used in multiple conditions is the  
same  
cached-compiled version?  
Eg I need to validate the all the (sub)request params before sending it  
upstream:

location /foo {

if ($time !~ /(\d\d):(\d\d):(\d\d)/) { return 400; }  
if ($arg\_time !~ /(\d\d):(\d\d):(\d\d)/) { return 400; }

}

#note that in the real app, I’ll have lots of repeated regex for email,  
phone, used in every if directive

location /bar {

if ($arg\_time !~ /(\d\d):(\d\d):(\d\d)/) { return 400; }  
set\_form\_input $form\_time time\_finished;  
if ($form\_time !~ /(\d\d):(\d\d):(\d\d)/) { return 400; }

}

What I’m trying to do is send the params to redis with its lua scripting  
engine handling the _pre-validated_ request.  
Redis is single-process while nginx can have multiple workers … so its  
only fitting to let nginx do validation for redis 🙂

Cheers

---

<div class="post-metadata">

**Author:** ![David\_Yu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b77776/32.png) [@David\_Yu](https://www.ruby-forum.com/u/David_Yu)\
**Post date:** [July 21, 2011, 4:44pm UTC](https://www.ruby-forum.com/t/same-regex-caching/209739/2 "2011-07-21T16:44:27Z")

</div>

On Thu, Jul 21, 2011 at 10:18:06PM +0800, David Yu wrote:

> if ($arg\_time !~ /(\d\d):(\d\d):(\d\d)/) { return 400; }  
> if ($form\_time !~ /(\d\d):(\d\d):(\d\d)/) { return 400; }
> 
> }
> 
> What I’m trying to do is send the params to redis with its lua scripting  
> engine handling the _pre-validated_ request.  
> Redis is single-process while nginx can have multiple workers … so its  
> only fitting to let nginx do validation for redis 🙂

No, each regex is compiled separately.

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![David\_Yu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b77776/32.png) [@David\_Yu](https://www.ruby-forum.com/u/David_Yu)\
**Post date:** [July 21, 2011, 4:54pm UTC](https://www.ruby-forum.com/t/same-regex-caching/209739/3 "2011-07-21T16:54:24Z")

</div>

On Thu, Jul 21, 2011 at 10:43 PM, Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > if ($time !~ /(\d\d):(\d\d):(\d\d)/) { return 400; }  
> > set\_form\_input $form\_time time\_finished;

Thanks for the quick reply.  
I’m thinking maybe the nginx map can act as the regex cache for the same  
ones.

set $map\_regex\_time $arg\_time  
if ( !$map\_regex\_time) return { 400; }

set\_form\_input $map\_regex\_time time\_finished;  
if (! $map\_regex\_time) return { 400; }

Since I’ll literally have hundreds of fields using the same regex, this  
might help?

---

<div class="post-metadata">

**Author:** ![David\_Yu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b77776/32.png) [@David\_Yu](https://www.ruby-forum.com/u/David_Yu)\
**Post date:** [July 21, 2011, 8:10pm UTC](https://www.ruby-forum.com/t/same-regex-caching/209739/4 "2011-07-21T20:10:59Z")

</div>

On Thu, Jul 21, 2011 at 10:53:51PM +0800, David Yu wrote:

> > > location /foo {
> 
> if (! $map\_regex\_time) return { 400; }
> 
> Since I’ll literally have hundreds of fields using the same regex, this  
> might help?

I do not understand the issue.

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![David\_Yu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b77776/32.png) [@David\_Yu](https://www.ruby-forum.com/u/David_Yu)\
**Post date:** [July 21, 2011, 4:56pm UTC](https://www.ruby-forum.com/t/same-regex-caching/209739/5 "2011-07-21T16:56:16Z")

</div>

On Thu, Jul 21, 2011 at 10:53 PM, David Yu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > Eg I need to validate the all the (sub)request params before sending it  
> > > phone, used in every if directive  
> > > engine handling the _pre-validated_ request.  
> > > if (!$map\_regex\_time) { return 400; }
> 
> set\_form\_input $map\_regex\_time time\_finished;  
> if (!$map\_regex\_time) { return 400; }

Oops. Copy-paste err

---

<div class="post-metadata">

**Author:** ![David\_Yu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b77776/32.png) [@David\_Yu](https://www.ruby-forum.com/u/David_Yu)\
**Post date:** [July 22, 2011, 3:24am UTC](https://www.ruby-forum.com/t/same-regex-caching/209739/6 "2011-07-22T03:24:32Z")

</div>

On Fri, Jul 22, 2011 at 2:10 AM, Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> it  
> email,
> 
> > > > What I’m trying to do is send the params to redis with its lua  
> > > > ones.  
> > > > I do not understand the issue.

map $map\_arg\_time $map\_regex\_time {  
~/(\d\d):(\d\d):(\d\d)/ 1;  
}

location /foo {  
set $map\_arg\_time $arg\_time  
if ( !$map\_regex\_time ) return { 400; }

set\_form\_input $map\_arg\_time time\_finished;  
if ( !$map\_regex\_time ) return { 400; }

}

Since I’ll literally have hundreds of fields using the same regex for  
validation, this might help?  
If there are better ways to do validation, please do tell.

Cheers
