Entity Framework は、データ指向のソフトウェア アプリケーションの開発をサポートする ADO.NET の一連のテクノロジです。 Entity Framework を使用すると、開発者はデータを扱うときにより高い抽象レベルで作業でき、従来のアプリケーションよりも少ないコードでデータ指向のアプリケーションを作成および保守できます。
NCache Entity Framework とデータ ソースの間で機能するキャッシュ プロバイダーを紹介します。 その主な理由は、 EF キャッシュ プロバイダー これは、データベースのトリップ (アプリケーションのパフォーマンスを低下させる) を減らし、キャッシュからクエリ結果を提供することです。 プロバイダーは、ADO.NET エンティティ フレームワークと元のデータ ソースの間で機能します。 したがって、現在のコードを変更/コンパイルすることなく、キャッシュ プロバイダーをプラグインできます。
NCache Entity Frameworkキャッシングプロバイダーは、XNUMXつのモードで動作します。 「キャッシング「または」で分析" モード。キャッシュ モードでは、選択したクエリの結果セットをキャッシュできます。分析モードはパススルー モードで動作し、どのクエリがどのような頻度で呼び出されているかを示すレポートを生成することで、キャッシュする必要があるクエリを見つけるのに役立ちます。 統合モードの詳細については、
NCache Entity Framework プロバイダーは、キャッシュ内のデータが常にデータベースと同期していることも保証します。 したがって NCache .NETを使用します SqlCacheDependency これにより、SQL クエリが SQL Server に登録されるため、このクエリによって表されるデータセット内の行がデータベース内で変更されると、SQL Server はイベント通知をスローします。 NCache. NCache 次に、対応する結果セットをキャッシュから削除します。
1. Entity Framework アプリケーションが必要です。 (EFCaching のサンプル アプリケーションを次から使用することもできます) NCache サンプル アプリケーションは次のパスから入手できます。
「インストールディレクトリ:/ProgramFiles/NCache/samples/clr20/EntityDataModelIntegrationDemo"
すべての Entity Framework アプリケーションには、ADO.NET Entity Data Model がすでに追加されており、次の XNUMX つのファイルが自動的に生成されます。
2. Entity Framework 2010 および 3.5 の場合は Microsoft Visual Studio 4.0、Entity Framework 2012 および 2013 の場合は Microsoft Visual Studio 6.0/6.1
3. データベース ツール (MS SQL SERVER 2008、ORACLE など)
Add Alachisoft.Integrations.EntityFramework.CachingProvider
Entity Framework アプリケーションへの参照。 この .dll ファイルは次のパスに配置されます。
"インストールディレクトリ:/ProgramFiles/NCache/integration/MSEntityFramework"
参照を追加した後、3 つの異なるファイルを変更する必要があります。
Entity Framework アプリケーションに ADO.NET Entity Data Model を追加すると生成される SSDL ファイルには、次の変更が必要です。
以下は、SQL 2008 データベースの変更例です。
SSDL では、プロバイダー名は、 以下に示すような要素:
<Schema Namespace = "NorthwindModel.Store" Alias = "Self" Provider = "System.Data.SqlClient" ProviderManifestToken = "2005"
xmlns:store ="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator"
xmlns ="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
私たちのものを注射するために NCache Entity Framework プロバイダーをプラグインするには、上記で強調表示された属性をオーバーライドする必要があります。 SSDL では、新しいプロバイダーの名前を Provider 属性に入力し、前のプロバイダーとプロバイダー マニフェスト トークンを連結します。 プロバイダマニフェストトークン 以下に示すように、フィールド:
<Schema Namespace = "NorthwindModel.Store" Alias = "Self"
Provider = "Alachisoft.Integrations.EntityFramework.CachingProvider"
ProviderManifestToken = "System.Data.SqlClient;2005"
xmlns:store = "http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator"
xmlns = "http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
Entity Framework アプリケーションに ADO.NET Entity Data Model を追加すると生成されるアプリケーション (または Web) 構成ファイルには、次の変更が必要です。
プロバイダー不変名は、アプリケーションまたはWeb構成ファイルの接続文字列で指定されます。
<connectionStrings>
<add name="NorthwindEntities"
connectionString="metadata=res://*/NorthwindCustomerModel.csdl
|res://*/NorthwindCustomerModel.ssdl
|res://*/NorthwindCustomerModel.msl;
provider=System.Data.EntityClient;
provider connection string="
Data Source=localhost;Initial Catalog=Northwind;user id= userid;
password=password;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient"/>
</connectionStrings>
プロバイダー名をに変更します NCache Entity Framework プロバイダーを追加し、次に示すように接続文字列にプロバイダー ラッパー情報も追加します。
<connectionStrings>
<add name="NorthwindEntities"
connectionString="metadata=res://*/NorthwindCustomerModel.csdl
|res://*/NorthwindCustomerModel.ssdl
|res://*/NorthwindCustomerModel.msl;
provider=Alachisoft.Integrations.EntityFramework.CachingProvider;
provider connection string="
wrappedProvider=System.Data.SqlClient;
Data Source=localhost;Initial Catalog=Northwind;user id= userid;
password=password;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient"/>
</connectionStrings>
また、初期化のためのプロバイダーファクトリを追加します NCache エンティティフレームワークプロバイダー。 不変属性は、接続文字列のプロバイダー名と同じである必要があります。
<DbProviderFactories>
<add name="EF Caching Data Provider"
invariant="Alachisoft.Integrations.EntityFramework.
CachingProvider"
description="Caching Provider Wrapper"
type="Alachisoft.NCache.Integrations.
EntityFramework.EFCachingProviderFactory,
Alachisoft.Integrations.EntityFramework.
CachingProvider,
Version=1.0.0.0,
Culture=neutral,
PublicKeyToken=cff5926ed6a53769"/>
</DbProviderFactories>
プロバイダー不変名は、アプリケーション(またはWeb)構成ファイルでも指定されます。
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient"
type="System.Data.Entity.SqlServer.SqlProviderServices,
EntityFramework.SqlServer" />
</providers>
</entityFramework>
インバリアント名を次のように変更します EFCachingProviderServices
そして次のように入力します Alachisoft.NCache.Integrations.EntityFramework.EFCachingProviderServices, Alachisoft.Integrations.EntityFramework.CachingProvider
以下のように:
<entityFramework>
<providers>
<provider invariantName="EFCachingProviderServices"
type="Alachisoft.NCache.Integrations.EntityFramework.EFCachingProviderServices,
Alachisoft.Integrations.EntityFramework.CachingProvider" />
</providers>
</entityFramework>
アプリケーション (または Web) 構成ファイルのプロバイダー セクションにインターセプターを追加します。
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient"
type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
<interceptors>
<interceptor type="Alachisoft.NCache.Integrations.EntityFramework.Caching.EFCommandInterceptor,
Alachisoft.Integrations.EntityFramework.CachingProvider" />
</interceptors>
</entityFramework>
次の情報を appSettings に追加する必要があります。 app.config/web.config
:
<appSettings> <add key = "app-id" value = "PersonNameApp2"/> <add key = "logging-level" value = "Debug"/> </appSettings>
注意: efcaching.ncconf のアプリ ID はアプリケーション (app.config) と同じである必要があります。そうでない場合はエラーが表示されます)
app.config / web.configで接続文字列を指定する以外に、作成時に接続を指定することもできます ObjectContext
or EntityConnection
。 接続文字列をプログラムで作成する場合、ラッパー情報を接続文字列に含める必要があります。 接続文字列を作成する方法と、 ObjectContext
or EntityConnection
初期化できます。
try
{
SqlConnectionStringBuilder sqlConnBuilder = new SqlConnectionStringBuilder();
sqlConnBuilder.DataSource = "localhost";
sqlConnBuilder.InitialCatalog = "EFTestDB";
sqlConnBuilder.IntegratedSecurity = true;
string conString = sqlConnBuilder.ToString();
EntityConnectionStringBuilder efConnBuilder = new EntityConnectionStringBuilder();
efConnBuilder.Provider = "EFCachingProvider";
efConnBuilder.ProviderConnectionString = @"wrappedProvider=System.Data.SqlClient;" + conString;
efConnBuilder.Metadata = "res:// /NorthwindCustomerModel.csdl|res:" + "// /NorthwindCustomerModel.ssdl|res:" +"// /NorthwindCustomerModel.msl;";
EntityConnection connection = new EntityConnection(efConnBuilder.ToString());
}
catch (Exception ex)
{
// handle exception
}
app.config/web.config でプロバイダー インバリアントを指定する以外に、構成 (DbConfiguration を継承するクラス) の初期化中にプロバイダー インバリアントを指定できます。 プロバイダーの不変条件を指定する方法は次のとおりです。
try
{
this.SetProviderServices("EFCachingProviderServices", EFCachingProviderServices.Instance);
}
catch (Exception ex)
{
// handle exception
}
app.config/web.config でデータベース インターセプターを指定する以外に、構成 (DbConfiguration を継承するクラス) の初期化中にデータベース インターセプターを指定できます。 インターセプターを指定する方法は次のとおりです。
try
{
this.AddInterceptor(new EFCommandInterceptor());
}
catch (Exception ex)
{
// handle exception
}
ユーザーは、提供されている拡張機能を使用して結果をキャッシュすることもできるようになりました。 IQueryable
.
名前空間を指定します
using Alachisoft.NCache.Integrations.EntityFramework.Caching;
キャッシュ メソッドのオーバーロードを今すぐ呼び出します IQueryable
var students = from s in db.Students
select s;
students.Cache();
// results are not cached yet
foreach (Student student in students)
//results are cached when enumerator from query is iterated completely
{
//using student
}
「efcaching.ncconf」に変更が必要です。
プロバイダー構成には、キャッシュおよびキャッシュ ポリシー関連の情報が含まれています。 以下は、efcaching.ncconf で必要な変更です。
<configuration>
<app-config app-id = "PersonNameApp" mode = "analysis|caching">
「分析」モードは、各クエリの実行回数を監視し、レポートを生成するために使用されます。 レポートには、クエリテキストと各クエリの呼び出し数が含まれます。 このレポートは、カスタムポリシーで使用できます。 このモードではキャッシュは行われません。
<analysis-policy log-path = "" analysis-time = "1min" cache-enable-threshold = "1" default-expiration-type = "Sliding" default-expiration-time = "180sec" dbsyncdependency= "false"/>
Log-path:分析ログファイルが生成されるパス。
デフォルト= < インストールディレクトリ > / log-files / efcaching-analysis-logs /
「キャッシュ」モードの場合、ラッピングプロバイダーは指定されたすべてのクエリの結果をキャッシュします。 どちらのポリシーにも独自の仕様があります。 それぞれのデータベースで更新 (UPDATE、INSERT、または DELETE) が検出されるたびに、プロバイダーは、更新されたテーブルのいずれかに依存していたキャッシュされたクエリをすべて削除することで、影響を受けるキャッシュ エントリを無効にします。
<cache-policy-configuration database = "none|sqlserver|oracle" cache-id = "mycache">
カスタム ポリシーの場合、結果とともにキャッシュする必要があるユーザー構成可能なクエリのリストが含まれます。 キャッシュが有効になっているクエリ結果のみがキャッシュされます。 すべてのクエリに対してカスタム キャッシュ ポリシーを指定できます。
<!--sql-query = "SELECT [Extent1].[CustomerID] AS [CustomerID],= @param_0"-->
<query>
<cache-query query text="SELECT [Extent1].[OrderID] AS [OrderID], < @param_0"/>
<cache-policy vary-by-cache-param="param_0" expiration-type="Sliding"
enabled="True" expiration-time="180sec" dbsyncdependency="False"/>
</query>
ストアド プロシージャの場合、クエリ テキストはストアド プロシージャの名前になり、デフォルトのポリシーやデータベース同期の依存関係はありません。 ユーザーはストアド プロシージャの結果を有効期限付きでのみキャッシュできます。ここではデータベースへの依存関係は必要ありません。
API レベル キャッシュ (Entity Framework 6.0 および 6.1 でサポート) の場合、名前空間で提供される拡張メソッドを使用してクエリをキャッシュできます。
using Alachisoft.NCache.Integrations.EntityFramework.Caching;
また、次のタグの構成ファイルで API レベルのキャッシュのキャッシュ ポリシーを指定できます。
<api-level-caching expiration-type="sliding|absolute" enable="True|False"
expiration-time="120sec" dbsyncdependency="True|False">