[mapserver-commits] r12430 - in trunk/mapserver/mapcache: . include
src
svn at osgeo.org
svn at osgeo.org
Fri Aug 26 07:25:09 EDT 2011
Author: tbonfort
Date: 2011-08-26 04:25:09 -0700 (Fri, 26 Aug 2011)
New Revision: 12430
Modified:
trunk/mapserver/mapcache/geocache.xml
trunk/mapserver/mapcache/include/geocache.h
trunk/mapserver/mapcache/src/cache_memcache.c
trunk/mapserver/mapcache/src/configuration.c
trunk/mapserver/mapcache/src/mod_geocache.c
trunk/mapserver/mapcache/src/tileset.c
Log:
make the xml parser be more lenient when extracting arrays: accept more separators (tabs, commas, newlines, ..)
thomas.bonfort | 2011-08-15 18:21:10 +0200 (Mon, 15 Aug 2011)
Modified: trunk/mapserver/mapcache/geocache.xml
===================================================================
--- trunk/mapserver/mapcache/geocache.xml 2011-08-26 11:25:03 UTC (rev 12429)
+++ trunk/mapserver/mapcache/geocache.xml 2011-08-26 11:25:09 UTC (rev 12430)
@@ -342,14 +342,12 @@
<!-- memcache cache
entry accepts multiple <server> entries
-->
- <!--
<cache name="memcache" type="memcache">
<server>
<host>localhost</host>
<port>11211</port>
</server>
</cache>
- -->
<!-- format
@@ -468,7 +466,7 @@
<params>
<FORMAT>image/png</FORMAT>
<LAYERS>default</LAYERS>
- <MAP>/Users/tbonfort/dev/mapserver-utils/osm.map</MAP>
+ <MAP>/gro1/mapserver-utils/osm.map</MAP>
</params>
</getmap>
</source>
@@ -588,8 +586,19 @@
<!-- expires
optional expiration value in seconds for a tile. this is expressed in a number of seconds
after the creation date of the tile
+ This is the value that will be set in the HTTP Expires and Cache-Control headers, and has
+ no effect on the actual expiration of tiles on the caches. See <auto_expire> for that.
-->
<expires>3600</expires>
+
+ <!-- auto_expire
+ automatically re-request tiles and update the cache once they are older than the given number
+ of seconds after their creation.
+ Note that this will only delete tiles form the cache when they are accessed, you cannot
+ use this configuration to limit the size of the created cache.
+ Note that if set, this value overrides the value given by <expires>
+ -->
+ <auto_expire>86400</auto_expire>
<!-- dimensions
optional dimensions that should be cached
@@ -656,7 +665,7 @@
<abstract>see http://mapserver-utils.googlecode.com</abstract>
</metadata>
<source>osm</source>
- <cache>disk</cache>
+ <cache>memcache</cache>
<format>PNG</format>
<grid>FXX</grid>
<grid>MILLER</grid>
@@ -664,6 +673,7 @@
<grid>g</grid>
<metatile>5 5</metatile>
<expires>10000</expires>
+ <auto_expire>86400</auto_expire>
<metabuffer>10</metabuffer>
</tileset>
Modified: trunk/mapserver/mapcache/include/geocache.h
===================================================================
--- trunk/mapserver/mapcache/include/geocache.h 2011-08-26 11:25:03 UTC (rev 12429)
+++ trunk/mapserver/mapcache/include/geocache.h 2011-08-26 11:25:09 UTC (rev 12430)
@@ -978,8 +978,18 @@
/**
* number of seconds that should be returned to the client in an Expires: header
+ *
+ * \sa auto_expire
*/
int expires;
+
+ /**
+ * number of seconds after which a tile will be regenerated from the source
+ *
+ * will take precedence over the #expires parameter.
+ * \sa expires
+ */
+ int auto_expire;
/**
* the cache in which the tiles should be stored
Modified: trunk/mapserver/mapcache/src/cache_memcache.c
===================================================================
--- trunk/mapserver/mapcache/src/cache_memcache.c 2011-08-26 11:25:03 UTC (rev 12429)
+++ trunk/mapserver/mapcache/src/cache_memcache.c 2011-08-26 11:25:09 UTC (rev 12430)
@@ -85,9 +85,8 @@
_geocache_cache_memcache_tile_key(ctx, tile, &key);
GC_CHECK_ERROR(ctx);
rv = apr_memcache_delete(cache->memcache,key,0);
- if(rv != APR_SUCCESS) {
+ if(rv != APR_SUCCESS && rv!= APR_NOTFOUND) {
int code = 500;
- if(rv == APR_NOTFOUND) code=404;
ctx->set_error(ctx,code,"memcache: failed to delete key %s: %s", key, apr_strerror(rv,errmsg,120));
}
}
@@ -116,7 +115,14 @@
ctx->set_error(ctx,500,"memcache cache returned 0-length data for tile %d %d %d\n",tile->x,tile->y,tile->z);
return GEOCACHE_FAILURE;
}
+ /* extract the tile modification time from the end of the data returned */
+ memcpy(
+ &tile->mtime,
+ &(tile->data->buf[tile->data->size-sizeof(apr_time_t)]),
+ sizeof(apr_time_t));
+ tile->data->buf[tile->data->size+sizeof(apr_time_t)]='\0';
tile->data->avail = tile->data->size;
+ tile->data->size -= sizeof(apr_time_t);
return GEOCACHE_SUCCESS;
}
@@ -132,12 +138,23 @@
static void _geocache_cache_memcache_set(geocache_context *ctx, geocache_tile *tile) {
char *key;
int rv;
- int expires = tile->tileset->expires?tile->tileset->expires:3600;
+ /* set expiration to one day if not configured */
+ int expires = 86400;
+ if(tile->tileset->auto_expire)
+ expires = tile->tileset->auto_expire;
geocache_cache_memcache *cache = (geocache_cache_memcache*)tile->tileset->cache;
_geocache_cache_memcache_tile_key(ctx, tile, &key);
GC_CHECK_ERROR(ctx);
+
+ /* concatenate the current time to the end of the memcache data so we can extract it out
+ * when we re-get the tile */
+ char *data = calloc(1,tile->data->size+sizeof(apr_time_t));
+ apr_time_t now = apr_time_now();
+ apr_pool_cleanup_register(ctx->pool, data, (void*)free, apr_pool_cleanup_null);
+ memcpy(data,tile->data->buf,tile->data->size);
+ memcpy(&(data[tile->data->size]),&now,sizeof(apr_time_t));
- rv = apr_memcache_set(cache->memcache,key,(char*)tile->data->buf,tile->data->size,expires,0);
+ rv = apr_memcache_set(cache->memcache,key,data,tile->data->size+sizeof(apr_time_t),expires,0);
if(rv != APR_SUCCESS) {
ctx->set_error(ctx,500,"failed to store tile %d %d %d to memcache cache %s",
tile->x,tile->y,tile->z,cache->cache.name);
Modified: trunk/mapserver/mapcache/src/configuration.c
===================================================================
--- trunk/mapserver/mapcache/src/configuration.c 2011-08-26 11:25:03 UTC (rev 12429)
+++ trunk/mapserver/mapcache/src/configuration.c 2011-08-26 11:25:09 UTC (rev 12430)
@@ -788,6 +788,17 @@
return;
}
}
+ if ((cur_node = ezxml_child(node,"auto_expire")) != NULL) {
+ char *endptr;
+ tileset->auto_expire = (int)strtol(cur_node->txt,&endptr,10);
+ if(*endptr != 0) {
+ ctx->set_error(ctx, 400, "failed to parse auto_expire %s."
+ "(expecting an integer, "
+ "eg <auto_expire>3600</auto_expire>",
+ cur_node->txt);
+ return;
+ }
+ }
if ((cur_node = ezxml_child(node,"metabuffer")) != NULL) {
char *endptr;
Modified: trunk/mapserver/mapcache/src/mod_geocache.c
===================================================================
--- trunk/mapserver/mapcache/src/mod_geocache.c 2011-08-26 11:25:03 UTC (rev 12429)
+++ trunk/mapserver/mapcache/src/mod_geocache.c 2011-08-26 11:25:09 UTC (rev 12430)
@@ -197,6 +197,7 @@
static int geocache_write_tile(geocache_context_apache_request *ctx, geocache_tile *tile) {
int rc;
+ char *timestr;
request_rec *r = ctx->request;
if(tile->expires) {
@@ -204,15 +205,20 @@
apr_time_t additional = apr_time_from_sec(tile->expires);
apr_time_t expires = now + additional;
apr_table_set(r->headers_out, "Cache-Control",apr_psprintf(r->pool, "max-age=%d", tile->expires));
- char *timestr = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
+ timestr = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
apr_rfc822_date(timestr, expires);
- apr_table_set(r->headers_out, "Expires", timestr);
+ apr_table_setn(r->headers_out, "Expires", timestr);
}
- ap_set_last_modified(r);
- ap_update_mtime(r, tile->mtime);
- if((rc = ap_meets_conditions(r)) != OK) {
- return rc;
+ if(tile->mtime) {
+ ap_update_mtime(r, tile->mtime);
+ if((rc = ap_meets_conditions(r)) != OK) {
+ return rc;
+ }
+ timestr = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
+ apr_rfc822_date(timestr, tile->mtime);
+ apr_table_setn(r->headers_out, "Last-Modified", timestr);
}
+
return geocache_write_image_buffer(ctx, tile->data, tile->tileset->format);
}
Modified: trunk/mapserver/mapcache/src/tileset.c
===================================================================
--- trunk/mapserver/mapcache/src/tileset.c 2011-08-26 11:25:03 UTC (rev 12429)
+++ trunk/mapserver/mapcache/src/tileset.c 2011-08-26 11:25:09 UTC (rev 12430)
@@ -271,6 +271,7 @@
tileset->metasize_x = tileset->metasize_y = 1;
tileset->metabuffer = 0;
tileset->expires = 0;
+ tileset->auto_expire = 0;
tileset->metadata = apr_table_make(ctx->pool,3);
tileset->dimensions = NULL;
tileset->format = NULL;
@@ -359,6 +360,18 @@
ret = tile->tileset->cache->tile_get(ctx, tile);
GC_CHECK_ERROR(ctx);
+ if(ret == GEOCACHE_SUCCESS && tile->tileset->auto_expire && tile->mtime) {
+ /* the cache is in auto-expire mode, and can return the tile modification date
+ * so we check to see if it is stale */
+ apr_time_t now = apr_time_now();
+ apr_time_t stale = tile->mtime + apr_time_from_sec(tile->tileset->auto_expire);
+ if(stale<now) {
+ geocache_tileset_tile_delete(ctx,tile);
+ GC_CHECK_ERROR(ctx);
+ ret = GEOCACHE_CACHE_MISS;
+ }
+ }
+
if(ret == GEOCACHE_CACHE_MISS) {
/* the tile does not exist, we must take action before re-asking for it */
@@ -419,6 +432,13 @@
ctx->set_error(ctx, 500, "tileset %s: failed to re-get tile %d %d %d from cache after set", tile->tileset->name,tile->x,tile->y,tile->z);
}
}
+ /* update the tile expiration time */
+ if(tile->tileset->auto_expire && tile->mtime) {
+ apr_time_t now = apr_time_now();
+ apr_time_t expire_time = tile->mtime + apr_time_from_sec(tile->tileset->auto_expire);
+ tile->expires = apr_time_sec(expire_time-now);
+
+ }
}
}
More information about the mapserver-commits
mailing list