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
...
}
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.”