Welcome to ESRI Blogs

Updating your map caches automatically: The key to caching dynamic data

For the fastest performance of your ArcGIS Server map services, we recommend that you use map caching wherever it’s appropriate. A map cache is a picture of your map at one point in time. Map caches are obviously appropriate for maps that don’t change frequently, such as street maps and imagery. But thanks to the update tools included with ArcGIS Server, you may still be able to use caching even if your data changes more often.

The Update Map Server Cache geoprocessing tool helps you update an existing map cache. You can write a script that uses this tool, then schedule the update to run on a regular basis. Even if you’ve never written a script or scheduled a task before, it’s worth the effort to learn how because of the performance improvements you can get from using a cached map. This post walks you through the process.

Deciding if you should cache your changing data

To understand whether your frequently-changing map can still be cached, it helps to ask these questions:

  • How up-to-date does my map need to be?

    If the data you see on the map needs to be live, with no time delay acceptable, then caching is not appropriate. However, if a short delay is acceptable and the cache updates can be performed within that time window, you can still use caching.

  • How big is my cache?

    A larger cache takes more time to update. But if you can isolate the areas of change, you may be able to shorten the update time.

    For example, Jeremy Bartley wrote a script that updates areas based on feature extents, instead of updating the entire cache. If you’re editing data in national parks, but you need a map of the entire country, you use this script to update your cache in the national parks only. This can save an enormous amount of time and server resources.

    When you use this script, remember that if data and symbology are frequently changing throughout your map, the update will take longer than if the changes were confined to a few specific areas.

To summarize, if the update can keep up with the changes in an acceptable amount of time, the map is appropriate for caching.

Scenarios

The following scenarios might give you an idea of what types of maps are appropriate or inappropriate for caching.

  • National and regional weather maps

    Probably appropriate for caching. A map viewed at a national or regional scale does not require many tiles and can be updated before the weather drastically changes.

  • Cadastral maps of a city that change on a daily basis

    Probably appropriate for caching because a city is not a large area and the cache can run overnight. However if no delay is acceptable, the map might not be appropriate for caching.

  • Maps that update vehicle or ship positions every 10 minutes

    May be appropriate if the cache can be updated in less than 10 minutes. This depends on the size of the map, the cache scales, and available server resources.

  • Maps for 911 dispatchers that display incidents and responders in real time

    Probably not appropriate for caching because no time delay is acceptable.

Preparing to write the script

When you initially create the map cache, note how long it takes. This will give you an idea of how long it would take to update the entire cache.

After the cache generation tool runs, you may want to copy the text from the geoprocessing message box and paste it into Notepad. This isn’t required; it’s just for convenience when writing your update script. You can copy and paste some of the parameters from the message box into your script.

Writing a script to update your cache

To automate cache updates, you can create a Python script that runs the Update Map Server Cache tool. You can write the Python inside of Notepad, or you can use a development environment such as IDLE or PythonWin.

The script does not have to be complex. It just needs to define the parameters for the tool and run it. The following Python script runs the Update Map Server Cache tool to re-create an entire cache:

# This script updates all tiles in an ArcGIS Server 9.2 map cache

# Access the geoprocessing tools
import arcgisscripting
gp = arcgisscripting.create()

# Set up all of the variables for the update tool
server_name = "myserver1"
object_name = "Precipitation"
data_frame = "Layers"
layers = ""
constraining_extent = ""
scales = "32000000;16000000;8000000;4000000;2000000"
update_mode = "Recreate All Tiles"
thread_count = "2"
antialiasing = "NONE"

# Run the Update Map Server Cache tool
try:
print 'Starting Cache Update'
gp.UpdateMapServerCache(server_name, object_name, data_frame, layers, constraining_extent, scales, update_mode, thread_count, antialiasing)
print 'Finished Cache Update'

# Get the error messages if the tool fails
except:
gp.AddMessage(gp.GetMessages(2))
print gp.GetMessages(2)

If you’re just getting started with scripting you can just copy the above into Notepad, substitute your own variables, and save it with a .py extension. If you need help understanding the variables, see the Geoprocessing tool reference for Update Map Server Cache.

Scheduling the update to run automatically

Once you’ve saved your Python script, you can run it on any machine that has ArcGIS Server and Python installed by double-clicking the .py file in Windows Explorer. (Python is included with ArcGIS Desktop so you may already have it installed. Otherwise visit www.python.org to get Python.) Running the script manually in this way can be useful for testing, but in most cases you'll want to schedule the script to run on a regular basis.

Your operating system contains utilities that help you schedule tasks such as running a script. You'll need to provide the location of the script file, how often you want it to run, and the name and password that the task will run as.

The Scheduled Task Wizard in Windows walks you through the process of setting up the task. Open the wizard by clicking Start > All Programs > Accessories > System Tools > Scheduled Tasks > Add Scheduled Task.

The Scheduled Task Wizard 

If you want a few more options than the wizard provides, you can schedule the task from the command prompt. Just open Start > All Programs > Accessories > Command Prompt and enter your command: like the following, which schedules a Python script to run every 30 minutes:

schtasks /create /sc minute /mo 30 /tn "Update My Cache" /tr C:\92CachingScripts\Update.py

If you need help creating a command like this, see the Microsoft documentation for the schtasks command.

Dedicating server resources to the update

At this point you may wonder if your server can handle the cache updates and fill user requests at the same time. Updating a cache definitely uses server resources because the server is required to continually draw map tiles during the update. However, the update doesn't have to tie up your entire server.

Each service has a maximum number of instances of the service that are allowed to run, which you can adjust in the Service Properties. You can also use the caching tools to specify how many of those instances will be dedicated to caching (this is the “thread_count” variable in the script above). If you're short on server resources, you can lower the number of instances dedicated to caching so that others can continue to use your service while the update occurs.

In summary, if you want others to continue using your service while you are updating, make sure that the number of instances dedicated to caching is less than the maximum allowable number of instances.

Setting the maximum number of instances 

It’s also important to note that running Update Map Server Cache forces two restarts of the service – once before the update starts and once after the update finishes. Your services may be momentarily unavailable during the restarts.

Scheduling your update to run on nights or weekends can also lessen the chance of the update interfering with regular server traffic. Many organizations have nightly backup or update processes, into which you could incorporate your cache update script.

-Sterling Quinn

Published Friday, January 04, 2008 3:40 PM by sterlingdq

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# MapReduce for Large Geodatasets « The Memory Leak

Thursday, January 10, 2008 2:45 PM by MapReduce for Large Geodatasets « The Memory Leak

# re: Updating your map caches automatically: The key to caching dynamic data

If you want to run more than one map cache update can you just duplicate the code in the same file?
Friday, January 11, 2008 9:30 AM by Greg Matthews

# re: Updating your map caches automatically: The key to caching dynamic data

Greg-  Yes, you can run multiple updates in the script, in either sequential or looped form.

Sterling

Monday, January 14, 2008 8:54 AM by sterlingdq

# re: Updating your map caches automatically: The key to caching dynamic data

This script is not working for me, I've tried this on 2 servers and my desktop (AGS 9.2 SP3) It reports: C:\ARCGIS~1\scripts>c:\python24\python.exe update.py Starting Cache Update Failed to generate cache. All server contexts failed to cache map. Failed to execute (UpdateMapServerCache). Failed to generate cache. All server contexts failed to cache map. Failed to execute (UpdateMapServerCache). Any thoughts? # This script updates all tiles in an ArcGIS Server 9.2 map cache # Access the geoprocessing tools import arcgisscripting gp = arcgisscripting.create() # Set up all of the variables for the update tool server_name = "L087XPx359" object_name = "test" data_frame = "Layers" layers = "parcels" constraining_extent = "" scales = "32000000;16000000;8000000;4000000;2000000" update_mode = "Recreate All Tiles" thread_count = "2" antialiasing = "NONE" # Run the Update Map Server Cache tool try: print 'Starting Cache Update' gp.UpdateMapServerCache(server_name, object_name, data_frame, layers, constraining_extent, scales, update_mode, thread_count, antialiasing) print 'Finished Cache Update' # Get the error messages if the tool fails except: gp.AddMessage(gp.GetMessages(2)) print gp.GetMessages(2)
Thursday, January 17, 2008 1:40 PM by Chris

# re: Updating your map caches automatically: The key to caching dynamic data

Chris- Some questions that might help troubleshoot:

-Are there any relevant messages in the server log files after the tool fails?

-Does the tool try to do something for a while then fail, or does it fail immediately?

-Do any tiles get updated?

-Can you successfully run UpdateMapServerCache from ArcToolbox with the same parameters?

Sterling

Thursday, January 17, 2008 2:42 PM by sterlingdq

# re: Updating your map caches automatically: The key to caching dynamic data

Sterling, thanks for your help! -Can you successfully run UpdateMapServerCache from ArcToolbox with the same parameters? Yes. -Are there any relevant messages in the server log files after the tool fails? Not really. In fact, I've compared the logs from my run with ArcCatalog with the script, both log outputs are the same, but the tools reports a failure and does not update any tiles. One of the really strange things that I have found after setting logging to level 5 is that if I specify a map services that does not exists, it seems to default to the first one and tries to update the cache for it (but also fails). -Does the tool try to do something for a while then fail, or does it fail immediately? It takes about 20-30 seconds and then fails. -Do any tiles get updated? No. Sterling
Friday, January 18, 2008 12:51 PM by Chris

# re: Updating your map caches automatically: The key to caching dynamic data

Chris- I took a closer look at your script and was wondering the following:

- Does it work if you use layers = ""  Leaving empty quotes should cache all layers. By the way, is this a fused or a multilayer cache?

- Are the scales in your script the same ones at which you originally built your cache? I ask because I noticed they are the same scales I used in the sample script. You should use your own scales.

Sterling

Friday, January 18, 2008 1:16 PM by sterlingdq

# re: Updating your map caches automatically: The key to caching dynamic data

Sterling,
Thanks for the hint on the scales, I've been using the ones that ArcCatalog uses when I update it via the GUI.
Good news! I got the update to work, thanks much for your help. However, the only combo that I could get to work is multilayer cache with out specifying the layers to update. I can recreate all empty tiles or all tiles - no problem. When I specify a layer name, it reports the same problem
- Failed to generate cache. All server contexts failed to cache map. Failed to execute (UpdateMapServerCache).
Not being able to create the fused cache is not really a big problem, but it would be nice to specify which layers should be updated in the multilayer cache.
Do you think this is possible an issue with AGS 9.2 SP3 and I should try an upgrade or is there anything else you could suggest?
Thanks very much for your help, Chris
Sunday, January 20, 2008 6:27 AM by Chris

# re: Updating your map caches automatically:one or more contexts failed

The script worked successfully when tesing small areas using few polycons. But we got failed error "Updating envelope: 38 .......... Completed with error. One or more server contexts failed." during creating real data. Any idea? Thank you for your halp.
Saturday, February 02, 2008 2:04 PM by Tom

# re: Updating your map caches automatically: The key to caching dynamic data

OK, to get this working from an script, you will need to do a couple of things (or at least, this is what I had to do):
1) Change the default "Maximum time a client will wait for a service to something much higher than the default (Tom, I think that might fix your problem)
2) With my configuration (Windows 2003/XP & AGS 9.2 SP3) I had to supply an extent and that fixed my problem. I think the take home lesson here is that if you are having problems with your python script and you can get it working interactively with ArcCatalog, experiment with the values that ArcCatalog defaults to and use that as your starting template.
-Chris
Saturday, February 09, 2008 3:36 PM by Chris

# Starting and Restarting of MapService

I had developed a program in Python that does the task of reading extents record by record and for every record it updates the cache. This is scheduled to run every night. But i found the starting of mapservices before and after the call to gp.UpdateMapCaching to be extremely annoying. With no direct support for updating the cache in 9.2,I fail to understand as how ESRI folks designed the api to force a restart everytime a call is made to the UpdateMapServerCache. Imagine if i have to update a cache for 100 different extents. I hereby sincerely request ESRI to change the library so that the map service need not be started everytime.
Friday, March 20, 2009 10:52 AM by Govindarajan

Leave a Comment

(required) 
required 
(required)