Returns whether or not the given cache file can be used or is stale. @param query The query to serve. @param end_time The end time on the query (32-bit unsigned int, seconds). @param max_age The maximum time (in seconds) we wanna allow clients to cache the result in case of a cache hit. If the file
(final HttpQuery query,
final long end_time,
final long max_age,
final File cachedfile)
| 521 | * @param cachedfile The file to check for staleness. |
| 522 | */ |
| 523 | private static boolean staleCacheFile(final HttpQuery query, |
| 524 | final long end_time, |
| 525 | final long max_age, |
| 526 | final File cachedfile) { |
| 527 | final long mtime = cachedfile.lastModified() / 1000; |
| 528 | if (mtime <= 0) { |
| 529 | return true; // File doesn't exist, or can't be read. |
| 530 | } |
| 531 | |
| 532 | final long now = System.currentTimeMillis() / 1000; |
| 533 | // How old is the cached file, in seconds? |
| 534 | final long staleness = now - mtime; |
| 535 | if (staleness < 0) { // Can happen if the mtime is "in the future". |
| 536 | logWarn(query, "Not using file @ " + cachedfile + " with weird" |
| 537 | + " mtime in the future: " + mtime); |
| 538 | return true; // Play it safe, pretend we can't use this file. |
| 539 | } |
| 540 | |
| 541 | // Case 1: The end time is an absolute point in the past. |
| 542 | // We might be able to re-use the cached file. |
| 543 | if (0 < end_time && end_time < now) { |
| 544 | // If the file was created prior to the end time, maybe we first |
| 545 | // executed this query while the result was uncacheable. We can |
| 546 | // tell by looking at the mtime on the file. If the file was created |
| 547 | // before the query end time, then it contains partial results that |
| 548 | // shouldn't be served again. |
| 549 | return mtime < end_time; |
| 550 | } |
| 551 | |
| 552 | // Case 2: The end time of the query is now or in the future. |
| 553 | // The cached file contains partial data and can only be re-used if it's |
| 554 | // not too old. |
| 555 | if (staleness > max_age) { |
| 556 | logInfo(query, "Cached file @ " + cachedfile.getPath() + " is " |
| 557 | + staleness + "s stale, which is more than its limit of " |
| 558 | + max_age + "s, and needs to be regenerated."); |
| 559 | return true; |
| 560 | } |
| 561 | return false; |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Writes the given byte array into a file. |
no test coverage detected