HOW TO

Set cache duration for hosted feature layers in ArcGIS Online using ArcGIS API for Python

Last Published: November 1, 2024

Summary

By default, the cache duration for Cache Control is set to 30 seconds. The cache duration can be adjusted to manage resource usage in the browser when consuming a hosted feature layer on ArcGIS Online through the Settings page of the hosted feature layer or table. However, ArcGIS API for Python allows updating the cache duration without accessing ArcGIS Online, which is especially useful when multiple layers need to be edited and a list of their item IDs is available.

To do so, the FeatureLayerCollection object can be used to access the hosted feature layer or table's REST endpoint to allow users to edit the JSON definition for the cacheMaxAge parameter.

Procedure

  1. Import the necessary modules and specify the credentials.
  • For standalone script:
from arcgis.features import FeatureLayerCollection
gis = GIS("<ORGANIZATION_URL>", "username", "password")
  • For ArcGIS Notebook:
from arcgis.features import FeatureLayerCollection
gis = GIS("Home")
  1. Specify the items and create the FeatureLayerCollection object.
  • To specify the item based on the user's item list:
#To specify an item based on the user's content list:
items = gis.items()
item = items[0]
flc = FeatureLayerCollection.fromitem(item)
  • To specify a specific item:
#To specify a specific item:
item = gis.content.get("<Item_ID>")
flc = FeatureLayerCollection.fromitem(item)
  1. Call the updateDefinition function and specify the desired duration in seconds. The following example sets the cache duration to 600 seconds or 10 minutes.
flc.manager.update_definition({"cacheMaxAge":600})

The following is a sample of a working standalone script for a specific item.

from arcgis.features import FeatureLayerCollection
gis = GIS("www.arcgis.com", "username123", "password123")

item = gis.content.get("sdfn32rncwei8hfqwn")
flc = FeatureLayerCollection.fromitem(item)

flc.manager.update_definition({"cacheMaxAge":600})

Article ID: 000033831

Software:
  • ArcGIS Online
  • ArcGIS API for Python 1 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options