tedious / Stash

The place to keep your cache.
http://www.stashphp.com
BSD 3-Clause "New" or "Revised" License
961 stars 133 forks source link

Changing expiration during saving. Why? #305

Closed seroga closed 8 years ago

seroga commented 8 years ago

Hi Guys,

I see that during the saving ( \Stash\Item :: executeSet ) you are changing the expiration time by dedacting the randim number between 0 and 15% of cacheTime .

 if ($cacheTime > 0) {

            $expirationDiff = rand(0, floor($cacheTime * .15));
            $expiration -= $expirationDiff;

        }

Could you please explain the reason why ?

Thanks, Kind Regards,

geekwright commented 8 years ago

This behavior is covered in the documentation, but it doesn't explain the implementation details.

The expiration time only sets a maximum time the Item can be considered fresh, not a minimum. Items can be invalidated from the cache before their expiration time for a number of reasons. Stash will also attempt to distribute cache misses to normalize system load. At no point will an Item be considered fresh after the expiration or ttl is reached.

Imagine that you are starting with a clean, empty cache. The first task has to build every cache item requested and specifies some preset ttl, such as 5 minutes. Every 5 minutes, there will be a big rebuild activity spike, as everything expires. Introducing a small variance leads to a more organic distribution of the cache misses over time, eliminating that potentially disruptive activity spike.

seroga commented 8 years ago

Clear , Thanks for explanation