# Merging equivalent cache keys?

**URL:** <https://www.ruby-forum.com/t/merging-equivalent-cache-keys/234918>\
**Category:** NGINX\
**Created:** [May 18, 2013, 2:52pm UTC](https://www.ruby-forum.com/t/merging-equivalent-cache-keys/234918 "2013-05-18T14:52:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [May 18, 2013, 2:52pm UTC](https://www.ruby-forum.com/t/merging-equivalent-cache-keys/234918/1 "2013-05-18T14:52:31Z")

</div>

We’ve got a route which is used to retrieve multiple objects in  
parallel.  
The client does a GET on /api/1/station/multi?id=123&id=456&id=789. We  
cache these in our nginx config:

```
   location ~ /api/[^/]+/station/multi {
          proxy_pass http://localhost:8000;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-Host $host;
          proxy_set_header X-Forwarded-Server $host;
          proxy_set_header X-Forwarded-For 

```

$proxy\_add\_x\_forwarded\_for;

```
          proxy_cache api;
          proxy_cache_use_stale updating;
   }

```

The problem is, clients present the ids in random order. For example,  
one  
client might ask for id=1&id=2, and another ask for id=2&id=1. Both  
should  
return exactly the same response, but map to different cache keys. For  
two  
ids, it’s not that bad, but many of the calls are for large numbers of  
ids  
and the combinatorics quickly spin out of control.

Is there any way to rewrite the keys in nginx to canonicalize them?  
Sorting  
all the ids in numerical order would do it.

Posted at Nginx Forum:

> **[Merging equivalent cache keys?](https://forum.nginx.org/read.php?2,239315,239315#msg-239315)**
>
> We've got a route which is used to retrieve multiple objects in parallel. The client does a GET on /api/1/station/multi?id=123&id=456&id=789. We cache these in our nginx config: location ~ /api/\[^/\]+/station/multi { proxy\_pass...

---

<div class="post-metadata">

**Author:** ![roysmith649](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f04885/32.png) [@roysmith649](https://www.ruby-forum.com/u/roysmith649)\
**Post date:** [May 18, 2013, 3:45pm UTC](https://www.ruby-forum.com/t/merging-equivalent-cache-keys/234918/2 "2013-05-18T15:45:42Z")

</div>

Using the embedded Lua module you can add the three IDs as integers and  
reduce it to a single number.  
That way you’ll get a single number by virtue if the commutativity of  
real  
number addition. AFAIK there  
are no arithmetic operators available on the Nginx config language.

----appa
