Skip to content

Added jitter function to the cache #618

Closed
wants to merge 1 commit into from

5 participants

@Jeraimee

No description provided.

@AD7six
CakePHP member
AD7six commented Apr 20, 2012

"No description given." click edit please

@markstory markstory commented on the diff Apr 20, 2012
lib/Cake/Cache/Cache.php
@@ -239,6 +239,11 @@ public static function set($settings = array(), $value = null, $config = 'defaul
if (isset($settings['duration']) && !is_numeric($settings['duration'])) {
$settings['duration'] = strtotime($settings['duration']) - time();
}
+ if (isset($settings['duration']) && isset($settings['jitter'])) {
+ $jitter = rand(0, $settings['jitter']);
+ $plus_or_minus = (bool) rand(0, 1);
@markstory
CakePHP member
markstory added a note Apr 20, 2012

Should be $plusOrMinus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@markstory markstory commented on the diff Apr 21, 2012
app/Config/core.php
@@ -250,6 +250,16 @@
$duration = '+10 seconds';
}
+/**
+ * The number of seconds (if any) to add or subtract from the cache duration.
+ * In systems with large caches this can lessen the thundering hurd effect.
+ * In development mode, use no jitter
+ */
+$jitter = 300;
@markstory
CakePHP member
markstory added a note Apr 21, 2012

5 minutes seems like a long time for a default jitter value. Wouldn't 1 minute work just as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@markstory markstory commented on the diff Apr 21, 2012
lib/Cake/Cache/Cache.php
@@ -239,6 +239,11 @@ public static function set($settings = array(), $value = null, $config = 'defaul
if (isset($settings['duration']) && !is_numeric($settings['duration'])) {
$settings['duration'] = strtotime($settings['duration']) - time();
}
+ if (isset($settings['duration']) && isset($settings['jitter'])) {
+ $jitter = rand(0, $settings['jitter']);
+ $plus_or_minus = (bool) rand(0, 1);
+ $settings['duration'] = ($plus_or_minus) ? ($settings['duration'] + $jitter) : ($settings['duration'] - $jitter);
@markstory
CakePHP member
markstory added a note Apr 21, 2012

You don't safeguard against the duration becoming 0. If for example the duration and jitter are the same, or the jitter is greater than the duration, you could end up with 0 or fewer seconds as the duration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@ADmad ADmad commented on the diff May 20, 2012
app/Config/core.php
@@ -250,6 +250,16 @@
$duration = '+10 seconds';
}
+/**
+ * The number of seconds (if any) to add or subtract from the cache duration.
+ * In systems with large caches this can lessen the thundering hurd effect.
@ADmad
CakePHP member
ADmad added a note May 20, 2012

s/hurd/herd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@markstory markstory was assigned May 21, 2012
@lorenzo
CakePHP member
lorenzo commented Dec 22, 2012

There was no follow up on this PR after 8 months. APC already implement this type of feature so I don't think we should duplicate it anyway.

@lorenzo lorenzo closed this Dec 22, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.