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

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#
コードのコピー
const string NorthwindSampleUrl = @"Url = https://services.odata.org/V4/Northwind/Northwind.svc";

static String connectionString = $@"{NorthwindSampleUrl}; Use Cache = true; Cache Tolerance = 500; Cache Location = 'C:\temp\c1cache.db'";
static void DataReaderSelect()
{
    Console.WriteLine("Start Time: " + DateTime.Now);

    using (C1ODataConnection con = new C1ODataConnection(connectionString))
    {
        con.Open();

        var cmd = con.CreateCommand();
        cmd.CommandText = "SELECT ProductID, ProductName, UnitPrice FROM Products LIMIT 10";

        var rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            Console.WriteLine(String.Format("\t{0} --> \t\t{1}-->\t\t\t{2}", rdr["ProductID"], rdr["UnitPrice"], rdr["ProductName"]));
        }
    }

    Console.WriteLine("End Time: " + DateTime.Now);
}

External caching

The ADO.NET provider for OData 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 Odata 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#
コードのコピー
const string NorthwindSampleUrl = @"Url = https://services.odata.org/V4/Northwind/Northwind.svc";
// 明示的な MS SQL キャッシュ構成。
static String connectionString = $@"{NorthwindSampleUrl}; Use Cache = true; Cache Provider = 'Microsoft.Data.SqlClient'; 
Cache Connection = 'Server=yourserverid;Database=databasename;User Id=yourId;Password=yourpassword;'";

static void DataReaderSelect()
{
    Console.WriteLine("Start Time: " + DateTime.Now);                  

    using (var con = new C1ODataConnection(connectionString))
    {
        con.Open();

        var cmd = con.CreateCommand();
        cmd.CommandText = "SELECT ProductID, ProductName, UnitPrice FROM Products";

        var rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            Console.WriteLine(String.Format("\t{0} --> \t\t{1}-->\t\t\t{2}", rdr["ProductID"], rdr["UnitPrice"], rdr["ProductName"]));
        }
    }

    Console.WriteLine("End Time: " + DateTime.Now);
}
Note: With SQL Server, "Cache Provider = 'Microsoft.Data.SqlClient'" is mandatory and the user should have created own database, because the provider doesn’t create one.