SQL Server는 분산 캐시와 같은 이벤트 알림 메커니즘을 제공합니다. NCache 통해 변경 알림에 등록할 수 있습니다. SqlCacheDependency 그런 다음 데이터베이스에서 기본 데이터가 변경될 때 SQL Server로부터 알림을 받습니다. 이를 통해 NCache 해당 캐시 항목을 즉시 무효화하거나 다시 로드하면 캐시가 항상 데이터베이스와 동기화됩니다. 하지만, SqlCacheDependency 캐시를 데이터베이스와 동기화하는 매우 리소스 집약적인 방법이 될 수 있습니다.
더 나은 대안은 다음과 연결되는 CLR 저장 프로시저를 작성하는 것입니다. NCache SQL Server 내에서 해당 캐시 항목을 직접 업데이트하거나 무효화합니다. 관련된 데이터 구조를 생성하지 않기 때문에 더 자원 효율적입니다. SqlCacheDependency. 또한 .NET 이벤트를 실행하지 않습니다. NCache. 대신, 다음을 엽니다. NCache 클라이언트 연결 및 직접 알려줍니다 NCache 캐시된 항목을 무효화할지 다시 로드할지 여부. 그리고, 이 연결은 NCache 고도로 최적화되었으며 .NET 이벤트보다 훨씬 빠르고 가볍습니다.
use Northwind
alter database Northwind
set trustworthy on;
go
drop assembly SMdiagnostics
drop assembly [System.Web]
drop assembly [System.Messaging]
drop assembly [System.ServiceModel]
drop assembly [System.Management]
CREATE ASSEMBLY SMdiagnostics AUTHORIZATION dbo FROM N'C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication
Foundation\SMdiagnostics.dll' WITH permission_set = unsafe
CREATE ASSEMBLY [System.Web] AUTHORIZATION dbo FROM N'C:\Windows\Microsoft.NET\Framework64\v2.0.50727\System.Web.dll' WITH permission_set = unsafe
CREATE ASSEMBLY [System.Management] AUTHORIZATION dbo FROM N'C:\Windows\Microsoft.NET\Framework64\v2.0.50727\System.management.dll' WITH permission_set = unsafe
CREATE ASSEMBLY [System.Messaging] AUTHORIZATION dbo FROM N'C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Messaging.dll' WITH permission_set = unsafe
CREATE ASSEMBLY [System.ServiceModel] AUTHORIZATION dbo FROM N'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\System.ServiceModel.dll' WITH permission_set = unsafe
CREATE ASSEMBLY NCache from N'C:\Program Files\NCache\bin\assembly\2.0\Alachisoft.NCache.Web.dll' WITH permission_set = unsafe
public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void TestSProc(string cacheName)
{
//--- Put your code here
SqlPipe sp = SqlContext.Pipe;
try
{
sp.Send("Starting .....");
if (string.IsNullOrEmpty(cacheName))
cacheName = "mycache";
Cache _cache = NCache.InitializeCache(cacheName);
_cache.Insert("key", DateTime.Now.ToString());
sp.Send("Test is completed ...");
}
sp_configure 'clr enabled', 1
GO
RECONFIGURE
GO
CLR 기반 저장 프로시저 또는 트리거는 응용 프로그램 성능을 크게 향상시킬 수 있습니다. SqlCacheDependency 이는 상대적으로 느리고 대규모 데이터 세트의 경우 압도적일 수 있습니다.