Re: Why cannot the same path for cache be used several times?

On Wed, Aug 6, 2014 at 8:47 AM, itpp2012 [email protected] wrote:

keys_zone is memory, the other a ‘file’ path, I could imagine a ‘cachehash’
used could overwrite the other.

​I thought of that​, but learning how variables are resolved at request
time, and knowing that configuration is parsed on HUP signal, I though
nginx would handle cache requests from different servers/locations the
same
way it is dealing with them when they are coming from the same server.

I have not digged into the code, and I do not know if there is such
thing
sa a ‘cache manager’ which would centralize cache management and respond
to
cache events.

keys_zone are normally enough to avoid overwriting cache entries from
other
zones.
Failing to do so, it would mean several keys_zone being used in the same
cache are not isolated and can lead to collision… that would be
catastrophic…
From the assumption that keys_zone are collision-free, it would seem
reasonable to share the same cache file over several calls.

IMHO, the only thing that shall fail is the use of the same keys_zone on
the same file more than once.
Does not that sound reasonable?
​—
B. R.

Hello!

On Wed, Aug 06, 2014 at 06:48:29PM +0200, B.R. wrote:

reasonable to share the same cache file over several calls.

IMHO, the only thing that shall fail is the use of the same keys_zone on
the same file more than once.
Does not that sound reasonable?

To specify a cache, you have to define:

  1. Unique name for the cache, to be used in proxy_cache
    directives. This also the name of shared memory zone used to
    store the cache in-memory data.

  2. Path to a directory to store cache files.

Each cache is completely isolated from other caches (if any), and
knows nothing about them. Therefore, the directory used to store
cache files must be unique - if not, cache entries with identical
keys will be mapped to the same file, and will overwrite each
other. On the other hand, identical keys in different caches
doesn’t mean that resources match, keys are expected to be unique
only within a given cache.

In most cases, you’ll need only one cache; just use it multiple
times as appropriate with the proxy_cache directive. If you need
more than one cache for some reason, you’ll have to specify both
unique name and unique storage path to avoid possible collisions.


Maxim D.
http://nginx.org/

Thanks for your input Maxim.

However either I still do not get the purpose of the zone key or I am
missing something in the whole reasoning…
Isn’t it the zone key there to isolate requests from different sources
and
thus avoid collisions in the same cache path?
The only case favorable for collision is, as you explained, when the
same
cache path and the same zone key are being used.

If I am correct, why not allowing the same cache path to be used,
provided
the zone key is always different on each call of *_cache_path?

B. R.

Hi,

The key_zone is just the name of a shared memory area where some
metadata
about the cache entries will be saved.
The files stored on the cache_path does not use the key_zone.

They are different configurations to two different purposes.

To do what you are imagining, the file stored on cache_path must have
the
key_zone on its name, something like

/tmp/cache/zone1/0/00/9fc611e1a487c7a86585d509730f6000
/tmp/cache/zone2/0/00/9fc611e1a487c7a86585d509730f6000

Two different files saved on the same cache path, only using different
key_zones.

Since the key_zone is just a string on your configuration, if you now
want
to rename them like
zone1 -> zoneA and zone2 -> zoneB
and restart your server, all the content on your cache would be lost,
because the paths /tmp/cache/zoneA and /tmp/cache/zoneB will be empty.

You can use the key_zone name on different locations, and all objects
will
be saved on the same path.
To have different key_zone values you have to specify different
cache_path.

I hope this hypothetical example helps to understand :slight_smile:

Regards