[]
        
(Showing Draft Content)

Caching

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

The ADO.NET provider features an easy-to-use caching procedure that can also be shared by multiple connections. This article demonstrates the two types of caches that are currently supported:

  • Default cache: An internally managed database (in SQLite).
  • Specified cache: An externally managed database (for now, in SQL Server). For details, see External Caching.

To enable the caching feature, necessary connection properties are available, such as:

  • Cache Tolerance: The duration during which the cached data is considered as not needing to be updated with a more recent version from the source database (default is 600 seconds).
  • Cache Location: The location of the cache in the underlying file system (default is %AppData%/C1DataConnector).

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.

static void LocalCache() 
{
    string csvConnectionString = $"Uri='sampleCSV.csv';Use Pool= false; Use Cache = true; 
               Cache Location = 'C:/Temp/Cache.db'";

    Console.WriteLine("Query all Accounts...");
    using (var con = new C1CSVConnection(csvConnectionString))
    {
       con.Open();
       var table = con.GetSchema("columns", new string[] { "sampleCSV" });
       ShowDataTable(table);
       var cmd = con.CreateCommand();
       cmd.CommandText = "Select * From sampleCSV";
       var reader = cmd.ExecuteReader();
    }
}
static void ShowDataTable(DataTable table, int length = 25)
{
    foreach (DataColumn col in table.Columns)
    {
        Console.Write("{0,-" + length + "}", col.ColumnName);
    }
    Console.WriteLine();
    foreach (DataRow row in table.Rows)
    {
        foreach (DataColumn col in table.Columns)
        {
            if (col.DataType.Equals(typeof(DateTime)))
                Console.Write("{0,-" + length + ":d}", row[col]);
            else if (col.DataType.Equals(typeof(decimal)))
                Console.Write("{0,-" + length + ":C}", row[col]);
            else
                Console.Write("{0,-" + length + "}", row[col]);
        }
        Console.WriteLine();
    }
}       

External Caching

The ADO.NET provider for CSV supports 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 CSV 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.

type=note

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

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

//Explicit MS SQL Cache Config
static void ExternalCache() 
{
    string csvConnectionString = $"Uri='sampleCSV.csv';Use Pool= false; Use Cache = true;
           Cache provider='Microsoft.Data.SqlClient'; Cache connection='Server= yourserverid;
           Database= databasename; User Id= yourId; Password= yourpassword'";

   Console.WriteLine("Query all Accounts...");
   using (var con = new C1CSVConnection(csvConnectionString))
   {
        con.Open();
        var table = con.GetSchema("columns", new string[] { "sampleCSV" });
        ShowDataTable(table);
        var cmd = con.CreateCommand();
        cmd.CommandText = "Select * From sampleCSV";
        var reader = cmd.ExecuteReader();
    }
}
static void ShowDataTable(DataTable table, int length = 25)
{
    foreach (DataColumn col in table.Columns)
    {
        Console.Write("{0,-" + length + "}", col.ColumnName);
    }
    Console.WriteLine();
    foreach (DataRow row in table.Rows)
    {
        foreach (DataColumn col in table.Columns)
        {
            if (col.DataType.Equals(typeof(DateTime)))
                Console.Write("{0,-" + length + ":d}", row[col]);
            else if (col.DataType.Equals(typeof(decimal)))
                Console.Write("{0,-" + length + ":C}", row[col]);
            else
                Console.Write("{0,-" + length + "}", row[col]);
        }
        Console.WriteLine();
    }
}