Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created August 21, 2019 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennadel/3d9252c1a6f1bbbfa4dce74f317bf778 to your computer and use it in GitHub Desktop.
Save bennadel/3d9252c1a6f1bbbfa4dce74f317bf778 to your computer and use it in GitHub Desktop.
Calculating A Consistent Cache-Friendly Expiration Date For Signed-URLs In Lucee 5.3.2.77
<cfscript>
/**
* I return the "monthly" bucket for the given date (defaults to now). This helps with
* expiration dates on signed-URLs that will be consumed as the origin of a CDN pull-
* through.
*
* @input I am the date being used to calculate the bucket.
*/
public date function getMonthlyBucket( date input = now() )
cachedWithin = "request"
{
var inputYear = input.year();
var inputMonth = input.month();
var inputDay = input.day();
// To calculate the bucket for the input date, we need to roll back to the
// beginning of the month. And then, roll forward one month. This will place the
// bucket on the 1st of the next month.
var bucket = createDate( inputYear, inputMonth, 1 ).add( "m", 1 );
// Rolling forward to the next month will help with consistent in-app URLs, where
// the URLs are being GENERATED ON EACH REQUEST. However, not all URLs are
// consumed in-app. Some URLs are consumed externally, such as in an email.
// Because of this, we may want to roll-forward an ADDITIONAL MONTH if we are
// close to the end of the current bucket. This way, an email with embedded
// images that is sent at the END OF THE BUCKET will continue to work for at
// least two-weeks (roughly) before they expire.
if ( inputDay > 15 ) {
bucket = bucket.add( "m", 1 );
}
return( bucket );
}
// ------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------- //
fromDate = createDate( 2019, 8, 1 );
// Let's examine the bucket calculation over a 50-day period.
cfloop(
index = "i",
from = 0,
to = 50,
step = 1
) {
loopDate = fromDate.add( "d", i );
// Get the monthly bucket for the given date. Normally, this would just be
// assumed to be "now"; but, for the sake of this demo, we're going to provide
// an explicit test date.
bucketDate = getMonthlyBucket( loopDate );
echoLine(
loopDate.dateFormat( "short" ),
" => ",
bucketDate.dateFormat( "short" )
);
}
// ------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------- //
/**
* I echo each argument, in turn, and then add a line-break at the end.
*/
public void function echoLine() {
arguments.each(
( value ) => {
echo( value & " " );
}
);
echo( "<br />" );
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment