Example
This example demonstrates the retrieval of a schema for a table in three situations:
*before a cache manager has been installed
*after a cache manager has been installed but schema data has not yet been cached
*after the schema data is in the global cache
A timer method, timeGetSchema(), creates a temporary table and then retrieves the schema for the table while capturing the time interval needed for the retrieval. The time interval for the retrieval is written to the console.
 
static void timeGetSchema(const RWDBDatabase& db, const RWCString& name,
const char* msg)
{
RWClockTimer timer;
timer.start();
 
// create a table
RWDBTable tbl = db.table(name);
if (tbl.exists()) {
// fetch the schema
tbl.describe(RWDBTable::ColumnList);
}
 
timer.stop();
 
outStream << msg << ": " << timer.elapsedTime() << std::endl;
}
The main program does some setup and then calls timeGetSchema() for each of the three situations.
 
int main(int argc, char** argv)
{
... // example setup
 
RWDBDatabase db = RWDBManager::database(serverType, serverName, userName,
password, databaseName, pstring);
 
timeGetSchema(db, customerTableName, " no cache"); //1
RWDBInMemoryCacheManager cacheManager;
RWDBCacheManager* oldCacheManager = db.cacheManager(&cacheManager); //2
timeGetSchema(db, customerTableName, "cache miss"); //3
 
timeGetSchema(db, customerTableName, " cache hit"); //4
 
db.cacheManager(oldCacheManager); //5
...
}
//1 At this call, there is no cache. The time interval represents the time needed to obtain the schema directly from the database.
//2 This line both sets the in-memory cache manager as the cache manager for the database, and creates a pointer to the previous cache manager, which is returned by the cacheManager() method. If there is no cache manager, NULL is returned.
//3 At this call, a cache exists, but the schema data is not yet present. The time interval represents the time needed to retrieve the data from the database client and to put the schema data into the global cache.
//4 At this call, the schema data is in the cache. The time interval represents just the time needed to obtain the schema data from the global cache.
//5 Restores the previous cache manager if one was present.
Here is output from running the example using the Microsoft SQL Server Native Client:
 
no cache: 0.027675
cache miss: 0.010535
cache hit: 0.00021
As the numbers indicate, caching can have a very positive effect on performance. Keep in mind, though, that the numbers you get from a given run of the example may vary a lot depending on database load and many other factors.
To examine the complete code, see the file <sourcepro_install>\examples\dbcore\memcache.cpp.
If you wish to run this example, be aware that the example follows the conventions of the DB Interface Module tutorials. Before you run the example, you need to run the executable tutinit, and when you are finished, you may want to run tutclean. For more information on the setup process, see Section 16.3, “Setting up the Tutorials.”