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.:
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# |
コードのコピー
|
---|---|
//キャッシュを有効にするには、Use Cache を True に設定し、キャッシュの場所を有効な場所に設定します。 static string connectionString = $@"{MagentoConnectionString}; Use Cache = true; Cache Tolerance = 500; Cache Location = 'C:\Windows\Temp\c1cache.db'"; static void LocalCache() { Console.WriteLine("Start Time " + DateTime.Now); using (C1MagentoConnection con = new C1MagentoConnection(connectionString)) { con.Open(); var cmd = con.CreateCommand(); cmd.CommandText = "SELECT Id,Name,Price from Products limit 10"; DbDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { Console.WriteLine(String.Format("\t{0} --> \t\t{1}-->\t\t\t{2}", rdr["Id"], rdr["Name"], rdr["Price"])); } Console.WriteLine("End Time " + DateTime.Now); } } |
The ADO.NET provider for Magento 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 Magento 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.
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 = $@"{MagentoConnectionString}; Use Cache = true; Cache provider='Microsoft.Data.SqlClient'; Cache connection='Server= yourserverid;Database= databasename; User Id= yourId;Password= yourpassword;'"; static void ExternalCache() { Console.WriteLine("Start Time " + DateTime.Now); using (C1MagentoConnection con = new C1MagentoConnection(connectionString)) { con.Open(); var cmd = con.CreateCommand(); cmd.CommandText = "SELECT Id,Name,Price from Products limit 10"; DbDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { Console.WriteLine(String.Format("\t{0} --> \t\t{1}-->\t\t\t{2}", rdr["Id"], rdr["Name"], rdr["Price"])); } Console.WriteLine("End Time " + DateTime.Now); } } |