Leveraging 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 round-trips by using the locally cached representation of the geodatabase schema. This topic discusses the best ways to use the schema cache in an ArcObjects application.


When to use the schema cache

It is common to use the schema cache when the geodatabase schema is accessed frequently. In ArcGIS, this occurs when opening a large number of datasets. For example, internally, ArcGIS uses the schema cache when opening all layers in a map document. The schema cache is beneficial when working with large static data models where geodatabase objects—such as tables, fields, domains, and relationships—are well defined and do not change.
To get the greatest benefit from the schema cache, fill the cache before opening any datasets. Once the schema cache is filled, calls to Open methods, including IName.Open, are satisfied by the active schema cache; consequently, 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 useful in an application when it isn't known what workspaces are being passed back.
The code examples in this topic assume the following variables have been previously declared, instantiated, and in the case of the property set, populated with connection properties:
See the following code example:
[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 open workspaces. See the following code example:
[C#]
IWorkspaceFactorySchemaCache workspaceFactorySchemaCache = (IWorkspaceFactorySchemaCache)workspaceFactory;
workspaceFactorySchemaCache.EnableAllSchemaCaches();
[VB.NET]
Dim workspaceFactorySchemaCache As IWorkspaceFactorySchemaCache = CType(workspaceFactory, IWorkspaceFactorySchemaCache)
workspaceFactorySchemaCache.EnableAllSchemaCaches()
Alternatively, the cache can be enabled for a specific open workspace. See the following code example:
[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; it is important to disable the schema cache when it is not being used. Since the schema cache satisfies queries to the geodatabase tables, if the underlying geodatabase schema changes, these changes are not reflected in the schema cache. To prevent the querying of a stale schema cache, you can refresh the schema cache.
The following code example determines if a refresh is necessary, then refresh is called on the cache. At this point, the IsSchemaCacheStale property returns 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 applies 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 not needed. This is done after all datasets used by an application have been opened. It is important to disable the schema cache before destroying any objects that might have used the cache. The following code example shows how to disable the schema cache:
[C#]
workspaceFactorySchemaCache.DisableSchemaCache(workspace);
[VB.NET]
workspaceFactorySchemaCache.DisableSchemaCache(workspace)

Using the schema cache

The following code is an example of using the schema cache:
[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 the 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 the application.

workspaceFactorySchemaCache.DisableSchemaCache(workspace)






To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcGIS for Desktop Basic ArcGIS for Desktop Basic
ArcGIS for Desktop Standard ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced ArcGIS for Desktop Advanced
Engine Developer Kit Engine