Changeset 31054 in osm for applications


Ignore:
Timestamp:
2015-03-06T10:05:11+01:00 (10 years ago)
Author:
bastik
Message:

see #josm11193 - save original expires time send by server and apply limit when reading it back in

Location:
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmFileCacheTileLoader.java

    r31053 r31054  
    4949    private static final Charset TAGS_CHARSET = Charset.forName("UTF-8");
    5050
    51     private static final long DEFAULT_EXPIRES_TIME = 1000 * 60 * 60 * 24 * 7;
    52 
     51    // Default expire time (i.e. maximum age of cached tile before refresh).
     52    // Used when the server does not send an expires or max-age value in the http header.
     53    protected static final long DEFAULT_EXPIRE_TIME = 1000 * 60 * 60 * 24 * 7; // 7 days
     54    // Limit for the max-age value send by the server.
     55    protected static final long EXPIRE_TIME_SERVER_LIMIT = 1000 * 60 * 60 * 24 * 28; // 4 weeks
     56    // Absolute expire time limit. Cached tiles that are older will not be used,
     57    // even if the refresh from the server fails.
     58    protected static final long ABSOLUTE_EXPIRE_TIME_LIMIT = Long.MAX_VALUE; // unlimited
    5359
    5460    protected String cacheDirBase;
     
    5662    protected final Map<TileSource, File> sourceCacheDirMap;
    5763
    58     protected long maxCacheFileAge = Long.MAX_VALUE;  // max. age not limited
    5964
    6065    public static File getDefaultCacheDir() throws SecurityException {
     
    200205            try {
    201206                URLConnection urlConn = loadTileFromOsm(tile);
    202                 if (fileMtime != null && now - fileMtime <= maxCacheFileAge) {
     207                if (fileMtime != null && now - fileMtime <= ABSOLUTE_EXPIRE_TIME_LIMIT) {
    203208                    switch (tile.getSource().getTileUpdate()) {
    204209                    case IfModifiedSince:
     
    236241                }
    237242                if (urlConn instanceof HttpURLConnection && ((HttpURLConnection)urlConn).getResponseCode() == 304) {
    238                     // If we are isModifiedSince or If-None-Match has been set
     243                    // If isModifiedSince or If-None-Match has been set
    239244                    // and the server answers with a HTTP 304 = "Not Modified"
    240245                    switch (tile.getSource().getTileUpdate()) {
     
    292297        protected boolean isCacheValid() {
    293298            Long expires = null;
     299            if (tileFile.exists()) {
     300                fileMtime = tileFile.lastModified();
     301            } else if (tagsFile.exists()) {
     302                fileMtime = tagsFile.lastModified();
     303            } else
     304                return false;
     305
    294306            try {
    295307                expires = Long.parseLong(tile.getValue("expires"));
    296308            } catch (NumberFormatException e) {}
    297309
    298             if(expires != null && !expires.equals(0L)) {
    299                 // check by expire date set by server
     310            // check by expire date set by server
     311            if (expires != null && !expires.equals(0L)) {
     312                // put a limit to the expire time (some servers send a value
     313                // that is too large)
     314                expires = Math.min(expires, fileMtime + EXPIRE_TIME_SERVER_LIMIT);
    300315                if (now > expires) {
    301316                    log.log(Level.FINE, "TMS - Tile has expired -> not valid {0}", tile);
     
    304319            } else {
    305320                // check by file modification date
    306                 if (tileFile.exists()) {
    307                     // check for modification date only when tile file exists
    308                     // so handle properly the case, when only tags file exists
    309                     fileMtime = tileFile.lastModified();
    310                 } else if (tagsFile.exists()) {
    311                     fileMtime = tagsFile.lastModified();
    312                 } else
    313                     return false;
    314 
    315                 if (now - fileMtime > DEFAULT_EXPIRES_TIME) {
     321                if (now - fileMtime > DEFAULT_EXPIRE_TIME) {
    316322                    log.log(Level.FINE, "TMS - Tile has expired, maximum file age reached {0}", tile);
    317323                    return false;
     
    480486    }
    481487
    482     public long getMaxFileAge() {
    483         return maxCacheFileAge;
    484     }
    485 
    486     /**
    487      * Sets the maximum age of the local cached tile in the file system. If a
    488      * local tile is older than the specified file age
    489      * {@link OsmFileCacheTileLoader} will connect to the tile server and check
    490      * if a newer tile is available using the mechanism specified for the
    491      * selected tile source/server.
    492      *
    493      * @param maxFileAge
    494      *            maximum age in milliseconds
    495      * @see #FILE_AGE_ONE_DAY
    496      * @see #FILE_AGE_ONE_WEEK
    497      * @see TileSource#getTileUpdate()
    498      */
    499     public void setCacheMaxFileAge(long maxFileAge) {
    500         this.maxCacheFileAge = maxFileAge;
    501     }
    502 
    503488    public String getCacheDirBase() {
    504489        return cacheDirBase;
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmTileLoader.java

    r31052 r31054  
    117117                    for (String token: str.split(",")) {
    118118                        if (token.startsWith("max-age=")) {
    119                             lng = Math.min(
    120                                     Long.parseLong(token.substring(8)),
    121                                     86400 * 31 // cap max-age at one month
    122                                     ) * 1000 +
     119                            lng = Long.parseLong(token.substring(8)) * 1000 +
    123120                                    System.currentTimeMillis();
    124121                        }
Note: See TracChangeset for help on using the changeset viewer.