How to leverage the schema cache


Summary The schema cache is a cached snapshot of the geodatabase system tables (often referred to as the geodatabase schema). In an enterprise environment the geodatabase system tables (geodatabase schema) are in constant use by ArcGIS. Caching this schema locally can reduce database roundtrips by using the locally cached representation of the geodatabase schema. The following article is a discussion on how to best use the schema cache within an ArcObjects application.

Development licensing Deployment licensing
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime

To use the code in this article, the following namespace must be referenced via the using (C#) or Imports (VB.NET) statements. It is also necessary to add the corresponding reference to the project in order to gain access to the API.

 

In this topic

 
 
When to use the schema cache
 
It is common to use the schema cache when the geodatabase schema is being frequently accessed. In ArcGIS this occurs when opening a large number of datasets. For instance, internally ArcGIS uses the schema cache when opening all the layers in a map document. The schema cache is beneficial when working with large static data models where geodatabase objects like tables, fields, domains and relationships are well defined and do not change.
 
To get the greatest benefit from the schema cache the cache should be filled prior to opening any datasets. Once the schema cache is filled any calls to Open methods including IName.Open will be satisfied by the active schema cache and therefore all round trips to the database to query the geodatabase objects are avoided.
 
Enabling Schema Caching
 
Application developers are responsible for defining the scope of the schema cache. It is possible to enable schema caching on all workspaces passed back from a workspace factory. This is sometimes useful if within an application if it isn't known what workspaces are going to be passed back.
 
The code fragments in this document assume that the following variables and their types have been previously declared:

[C#]
IWorkspaceFactorySchemaCache workspaceFactorySchemaCache = 
  (IWorkspaceFactorySchemaCache)workspaceFactory;
workspaceFactorySchemaCache.EnableSchemaCaching();

[VB.NET]
Dim workspaceFactorySchemaCache As IWorkspaceFactorySchemaCache = CType(workspaceFactory, IWorkspaceFactorySchemaCache)
workspaceFactorySchemaCache.EnableSchemaCaching()
It is also possible to enable caching on all currently open workspaces.

[C#]
IWorkspaceFactorySchemaCache workspaceFactorySchemaCache = 
  (IWorkspaceFactorySchemaCache)workspaceFactory;
workspaceFactorySchemaCache.EnableAllSchemaCaches();

[VB.NET]
Dim workspaceFactorySchemaCache As IWorkspaceFactorySchemaCache = CType(workspaceFactory, IWorkspaceFactorySchemaCache)
workspaceFactorySchemaCache.EnableAllSchemaCaches()
Alternatively, the cache be enabled on just one specific open workspace.

[C#]
IWorkspaceFactorySchemaCache workspaceFactorySchemaCache = 
  (IWorkspaceFactorySchemaCache)workspaceFactory;
IWorkspace workspace = workspaceFactory.Open(propertySet, 0);
workspaceFactorySchemaCache.EnableSchemaCache(workspace);

[VB.NET]
Dim workspaceFactorySchemaCache As IWorkspaceFactorySchemaCache = CType(workspaceFactory, IWorkspaceFactorySchemaCache)
Dim workspace As IWorkspace = workspaceFactory.Open(propertySet, 0)
workspaceFactorySchemaCache.EnableSchemaCache(workspace)
 
Refreshing the Schema Cache
The schema cache is not a write through cache so it is important to disable the schema cache when it is not being used. Since the schema cache satisfies any queries to the geodatabase tables, if the underlying geodatabase schema changes these changes will not be reflected in the schema cache. To prevent the querying of a stale schema cache it is possible to refresh the schema cache. In the code example below we first determine if a refresh is necessary and then call refresh on the cache; at this point the IsSchemaCacheStale property will return false.
 

[C#]
//Spatial Cache Refresh if stale 
if (workspaceFactorySchemaCache.IsSchemaCacheStale(workspace))
{
  workspaceFactorySchemaCache.RefreshSchemaCache(workspace);
}

[VB.NET]
'Spatial Cache Refresh if stale
If workspaceFactorySchemaCache.IsSchemaCacheStale(workspace) Then
    workspaceFactorySchemaCache.RefreshSchemaCache(workspace)
End If
 
Refreshing the schema cache will pick up any changes made to the geodatabase schema after the schema cache was first enabled.
 
Disabling the Schema Cache
Application developers are responsible for disabling the schema cache when it is no longer needed. It is a good idea to do this after all the datasets used by an application have been opened. It is very important to disable the schema cache prior to destroying any objects that might have used the cache. An example of disabling the schema cache is given below.
 

[C#]
workspaceFactorySchemaCache.DisableSchemaCache(workspace);

[VB.NET]
workspaceFactorySchemaCache.DisableSchemaCache(workspace)
 
A complete code sample of recommended use of the schema cache if found below:

[C#]
IWorkspaceFactorySchemaCache workspaceFactorySchemaCache = 
  (IWorkspaceFactorySchemaCache)workspaceFactory;

IWorkspace workspace = workspaceFactory.Open(propertySet, 0);

workspaceFactorySchemaCache.EnableSchemaCache(workspace);

IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("ParcelClass");

// ...open all feature classes used in application

workspaceFactorySchemaCache.DisableSchemaCache(workspace);

[VB.NET]
Dim workspaceFactorySchemaCache As IWorkspaceFactorySchemaCache = CType(workspaceFactory, IWorkspaceFactorySchemaCache)

Dim workspace As IWorkspace = workspaceFactory.Open(propertySet, 0)

workspaceFactorySchemaCache.EnableSchemaCache(workspace)

Dim featureWorkspace As IFeatureWorkspace = CType(workspace, IFeatureWorkspace)
Dim featureClass As IFeatureClass = featureWorkspace.OpenFeatureClass("ParcelClass")

' ...open all feature classes used in application

workspaceFactorySchemaCache.DisableSchemaCache(workspace)