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:
To enable the caching feature, necessary connection properties are available, such as:
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# |
Copy Code |
---|---|
static void LocalCache() { //Define connection string string connectionString = string.Format(@"Key File={0};View Id={1};Use Cache=true; Cache Tolerance=6; Cache Location='C:\temp\gacache.db';", KeyFile, ViewId); //Define command string sql = "SELECT Country, Sessions FROM Traffic WHERE Sessions > 500 AND StartDate = '14DaysAgo' AND EndDate = 'Today'"; //Fetch data using (var con = new C1GoogleAnalyticsConnection(connectionString)) { Console.WriteLine("Start Time " + DateTime.Now); con.Open(); var command = con.CreateCommand(); command.CommandText = sql; var reader = command.ExecuteReader(); while (reader.Read()) { Console.WriteLine(String.Format("\t{0} --> \t\t{1}", reader["Country"], reader["Sessions"])); } Console.WriteLine("End Time " + DateTime.Now); } } |
The ADO.NET provider for Google Analytics 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 Google Analytics 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# |
Copy Code |
---|---|
static void ExternalCache() { //Explicit MS SQL Cache Config string connectionString = string.Format(@"Key File={0};View Id={1};Use Cache=true; Cache provider='Microsoft.Data.SqlClient'; Cache connection='Server= yourserverid; Database= databasename; User Id= yourId; Password= yourpassword;'", keyFile, viewId); Console.WriteLine("Start Time " + DateTime.Now); using (C1GoogleAnalyticsConnection con = new C1GoogleAnalyticsConnection(connectionString)) { con.Open(); var command = con.CreateCommand(); command.CommandText = "SELECT Country, Sessions FROM Traffic WHERE Sessions > 500 AND StartDate = '14DaysAgo' AND EndDate = 'Today'"; var reader = command.ExecuteReader(); while (reader.Read()) { Console.WriteLine(String.Format("\t{0} --> \t\t{1}", reader["Country"], reader["Sessions"])); } Console.WriteLine("End Time " + DateTime.Now); } } |