DataConnector
キャッシング
ADO.NET provider for QuickBooks Online > キャッシング

Caching data offers several advantages that can improve overall process performance, including reduced API requests and faster data access.

The DataConnector features an easy-to-use caching procedure, which can also be shared by multiple connections. This article is going to demonstrate the two types of caches that are currently supported:

To enable the caching feature, necessary connection properties are available, e.g.:

Internal caching

The following code demonstrates how to enable caching for a table by setting the UseCache property to True (by default, it is set to False). The cached data is stored in the file specified by the CacheLocation property in the connection string. For more information on Incremental Caching, refer to this topic.

C#
コードのコピー
static string connectionString = $@"Company Id={CompanyId};Use SandBox=true;OAuth Client Secret={OAuthClientSecret};OAuth Client Id={OAuthClientId};OAuth Access Token={OAuthAccessToken};
                  OAuth Refresh Token={OAuthRefreshToken}; OAuth Token Endpoint={OAuthTokenEndpoint};Minor Version={MinorVersion}; 
                  Use Cache=true; Cache Tolerance=500; Cache Location='C:\temp\c1cache.db';";
static void LocalCache()
{
    using (C1QuickBooksOnlineConnection conn = new C1QuickBooksOnlineConnection(connectionString))
    {
        conn.OAuthTokenRefreshed += OAuthTokenRefreshed;

        Console.WriteLine("Start Time " + DateTime.Now);
        conn.Open();
        C1QuickBooksOnlineCommand cmd = new C1QuickBooksOnlineCommand(conn, "Select * from Attachables'");
        var rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            Console.WriteLine("Read and cached the row with Id " + rdr["Id"]);
        }
        Console.WriteLine("End Time " + DateTime.Now);
    }
}

External Caching

The ADO.NET provider for QuickBooks Online support external caching, allowing users to store cached data in a separate database. This external database serves as a constant repository and can be accessed by multiple connection objects simultaneously. It includes all tables exposed by the QuickBooks Online provider and offers configuration options specific to the cache provider.

To utilize external caching, the provider supports a specified cache, such as SQL Server. You can specify the connection string using the UseCache, CacheProvider, and CacheConnection keys to enable external caching.

Note: With SQL Server, Cache Provider = ‘Microsoft.Data.SqlClient’ is mandatory so you must create your own database as our cache doesn’t create new Database.

The following code example implements external caching, by setting Use Cache to True, and using 'Microsoft.Data.SqlClient' as the cache provider.

C#
コードのコピー
static string connectionString = $@"Url=http://****/****/****; Use Cache=true; Cache provider='Microsoft.Data.SqlClient'; 
                              Cache connection='Server= yourserverid; Database= databasename; User Id= yourId; Password= yourpassword;' Company Id={CompanyId};
                              Use SandBox=true;OAuth Client Secret={OAuthClientSecret};OAuth Client Id={OAuthClientId};OAuth Access Token={OAuthAccessToken};
                              OAuth Refresh Token={OAuthRefreshToken}; OAuth Token Endpoint={OAuthTokenEndpoint};Minor Version={MinorVersion}";

static void ExternalCache()
{
    Console.WriteLine("Start Time " + DateTime.Now);
    using (C1QuickBooksOnlineConnection con = new C1QuickBooksOnlineConnection(connectionString))
    {
        con.Open();
        C1QuickBooksOnlineCommand cmd = new C1QuickBooksOnlineCommand(conn, "Select * from Attachables'");
        var rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
           Console.WriteLine("Read and cached the row with Id " + rdr["Id"]);
                }
        Console.WriteLine("End Time " + DateTime.Now);
    }
}