------------------------------------------------------------c#代码----------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using HuaTong.General.Utility;
using HuaTong.MeiDongPay.Entity.DBEntity.TableView;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using HuaTong.MeiDongPay.Entity.WebApi;
using System.Web.Caching;
using HuaTong.General.DBHelper;
using HuaTong.MeiDongPay.Entity.DBEntity;
using HuaTong.MeiDongPay.Entity.DBEntity.Procedure; namespace HuaTong.MeiDongPay.Entity
{
/// <summary>
/// webapi返回公用参数实现
/// </summary>
public static class GlobalCache
{
static IDbHelper DB = DbFactory.CreateHelper();
static object _sync = new object();
static bool IsLoading = false;
static string SqlCacheDepcName = "SqlCacheDepc"; private static string[] CacheTables = new[]
{
"BuyerChannelSet",
"MerchantChannelBuyerSet",
"MerchantSlaveChannelBuyerSet",
"MerchantChannelSet",
"MerchantPayAccountSet",
"BuyerInfo",
"MerchantInfo",
"PayAccountInfo",
"PayChannelInfo",
"BankInfo",
"BuyerBankSet",
"MerchantInfoExt",
"BankAccountInfo"
}; public static List<BuyerChannelSet> BuyerChannelSet { get; private set; }
public static List<MerchantChannelBuyerSet> MerchantChannelBuyerSet { get; private set; }
public static List<MerchantSlaveChannelBuyerSet> MerchantSlaveChannelBuyerSet { get; private set; }
public static List<MerchantChannelSet> MerchantChannelSet { get; private set; }
public static List<MerchantPayAccountSet> MerchantPayAccountSet { get; private set; }
public static List<BuyerInfo> BuyerInfo { get; private set; }
public static List<MerchantInfo> MerchantInfo { get; private set; }
public static List<PayAccountInfo> PayAccountInfo { get; private set; }
public static List<PayChannelInfo> PayChannelInfo { get; private set; }
public static List<BankInfo> BankInfo { get; private set; }
public static List<BuyerBankSet> BuyerBankSet { get; private set; }
public static List<MerchantInfoExt> MerchantInfoExt { get; private set; }
public static List<BankAccountInfo> BankAccountInfo { get; private set; } static GlobalCache()
{
RegTableNotifications(); GlobalCache.BuyerChannelSet = new List<BuyerChannelSet>();
GlobalCache.BuyerInfo = new List<BuyerInfo>();
GlobalCache.MerchantChannelBuyerSet = new List<MerchantChannelBuyerSet>();
GlobalCache.MerchantSlaveChannelBuyerSet = new List<MerchantSlaveChannelBuyerSet>();
GlobalCache.MerchantChannelSet = new List<MerchantChannelSet>();
GlobalCache.MerchantInfo = new List<MerchantInfo>();
GlobalCache.MerchantPayAccountSet = new List<MerchantPayAccountSet>();
GlobalCache.PayAccountInfo = new List<PayAccountInfo>();
GlobalCache.PayChannelInfo = new List<PayChannelInfo>();
GlobalCache.BankInfo = new List<BankInfo>();
GlobalCache.BuyerBankSet = new List<BuyerBankSet>();
GlobalCache.MerchantInfoExt = new List<MerchantInfoExt>();
GlobalCache.BankAccountInfo = new List<BankAccountInfo>();
} /// <summary>
/// 注册数据库缓存依赖内容
/// </summary>
private static void RegTableNotifications()
{
SqlCacheDependencyAdmin.EnableNotifications(DB.ConnctionString);
string[] notiTables = SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(DB.ConnctionString);
foreach (var tableName in CacheTables)
{
if (!notiTables.Contains(tableName))
{
SqlCacheDependencyAdmin.EnableTableForNotifications(DB.ConnctionString, tableName);
}
}
} /// <summary>
/// 缓存加载主入口
/// </summary>
public static void LoadCache()
{
if (!IsLoading)
{
IsLoading = true; List<Task> taskList = new List<Task>();
foreach (var tbName in CacheTables)
{
taskList.Add(Task.Factory.StartNew(delegate (object tbname)
{
LoadCache(tbname.ToString(), true);
}, tbName));
} Task.WaitAll(taskList.ToArray()); IsLoading = false;
}
} /// <summary>
/// 按表名分别加载缓存集合
/// </summary>
private static void LoadCache(string tableName, bool isInit = false)
{
var proc = new Proc_LoadGlobalCache() { tableName = tableName };
var dt = proc.ExecuteDataTable(); switch (tableName)
{
case "BuyerChannelSet":
var tmp_list1 = dt.ToList<BuyerChannelSet>();
lock (_sync)
{
BuyerChannelSet.Clear();
BuyerChannelSet = tmp_list1;
}
break;
case "MerchantChannelBuyerSet":
var tmp_list2 = dt.ToList<MerchantChannelBuyerSet>();
lock (_sync)
{
MerchantChannelBuyerSet.Clear();
MerchantChannelBuyerSet = tmp_list2;
}
break;
case "MerchantSlaveChannelBuyerSet":
var tmp_list12 = dt.ToList<MerchantSlaveChannelBuyerSet>();
lock (_sync)
{
MerchantSlaveChannelBuyerSet.Clear();
MerchantSlaveChannelBuyerSet = tmp_list12;
}
break;
case "MerchantChannelSet":
var tmp_list3 = dt.ToList<MerchantChannelSet>();
lock (_sync)
{
MerchantChannelSet.Clear();
MerchantChannelSet = tmp_list3;
}
break;
case "MerchantPayAccountSet":
var tmp_list4 = dt.ToList<MerchantPayAccountSet>();
lock (_sync)
{
MerchantPayAccountSet.Clear();
MerchantPayAccountSet = tmp_list4;
}
break;
case "BuyerInfo":
var tmp_list5 = dt.ToList<BuyerInfo>();
lock (_sync)
{
BuyerInfo.Clear();
BuyerInfo = tmp_list5;
}
break;
case "MerchantInfo":
var tmp_list6 = dt.ToList<MerchantInfo>();
lock (_sync)
{
MerchantInfo.Clear();
MerchantInfo = tmp_list6;
}
break;
case "PayAccountInfo":
var tmp_list8 = dt.ToList<PayAccountInfo>();
lock (_sync)
{
PayAccountInfo.Clear();
PayAccountInfo = tmp_list8;
}
break;
case "PayChannelInfo":
var tmp_list7 = dt.ToList<PayChannelInfo>();
lock (_sync)
{
PayChannelInfo.Clear();
PayChannelInfo = tmp_list7;
}
break;
case "BankInfo":
var tmp_list9 = dt.ToList<BankInfo>();
lock (_sync)
{
BankInfo.Clear();
BankInfo = tmp_list9;
}
break;
case "BuyerBankSet":
var tmp_list10 = dt.ToList<BuyerBankSet>();
lock (_sync)
{
BuyerBankSet.Clear();
BuyerBankSet = tmp_list10;
}
break;
case "MerchantInfoExt":
var tmp_list11 = dt.ToList<MerchantInfoExt>();
lock (_sync)
{
MerchantInfoExt.Clear();
MerchantInfoExt = tmp_list11;
}
break; case "BankAccountInfo":
var tmp_list13 = dt.ToList<BankAccountInfo>();
lock (_sync)
{
BankAccountInfo.Clear();
BankAccountInfo = tmp_list13;
}
break;
} if (isInit)
{
//初始化时创建缓存依赖
SqlCacheDependency sqlDependency = new SqlCacheDependency(SqlCacheDepcName, tableName);
string cacheKey = "@@@" + tableName + "@@@";
HttpRuntime.Cache.Insert(cacheKey, 1, sqlDependency, Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration, OnCacheUpdateCallback);
}
} /// <summary>
/// 获取缓存个数
/// </summary>
/// <returns></returns>
public static dynamic CacheCount()
{
var obj = new
{
BInfo_Count = GlobalCache.BuyerInfo.Count,
MInfo_Count = GlobalCache.MerchantInfo.Count,
AInfo_Count = GlobalCache.PayAccountInfo.Count,
CInfo_Count = GlobalCache.PayChannelInfo.Count,
BCSet_Count = GlobalCache.BuyerChannelSet.Count,
MCBSet_Count = GlobalCache.MerchantChannelBuyerSet.Count,
MSCBSet_Count = GlobalCache.MerchantSlaveChannelBuyerSet.Count,
MCSet_Count = GlobalCache.MerchantChannelSet.Count,
MASet_Count = GlobalCache.MerchantPayAccountSet.Count
}; return obj;
} private static void OnCacheUpdateCallback(string key, CacheItemUpdateReason reason, out object expensiveObject,
out CacheDependency dependency, out DateTime absoluteExpiration, out TimeSpan slidingExpiration)
{
//缓存值增加计数
string strValue = Convert.ToString(HttpRuntime.Cache.Get(key));
long cacheValue;
if (!long.TryParse(strValue, out cacheValue)) cacheValue = 1;
cacheValue++; //重新加载缓存集合
string tableName = key.Trim('@');
LoadCache(tableName); //设置缓存所需返回值
expensiveObject = cacheValue;
dependency = new SqlCacheDependency(SqlCacheDepcName, tableName);
absoluteExpiration = Cache.NoAbsoluteExpiration;
slidingExpiration = Cache.NoSlidingExpiration;
}
}
}
------------------------------------------------------------c#代码-------------------------------------------------------
------------------------------------------------------------sqlserver存储过程代码-------------------------------------------------------

USE [MeiDongPay]
GO
/****** Object: StoredProcedure [dbo].[AspNet_SqlCachePollingStoredProcedure] Script Date: 05/12/2017 15:34:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AspNet_SqlCachePollingStoredProcedure] AS
SELECT tableName, changeId FROM dbo.AspNet_SqlCacheTablesForChangeNotification
RETURN 0

USE [MeiDongPay]
GO
/****** Object: StoredProcedure [dbo].[AspNet_SqlCacheQueryRegisteredTablesStoredProcedure] Script Date: 05/12/2017 15:35:50 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AspNet_SqlCacheQueryRegisteredTablesStoredProcedure]
AS
SELECT tableName FROM dbo.AspNet_SqlCacheTablesForChangeNotification

USE [MeiDongPay]
GO
/****** Object: StoredProcedure [dbo].[AspNet_SqlCacheRegisterTableStoredProcedure] Script Date: 05/12/2017 15:36:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AspNet_SqlCacheRegisterTableStoredProcedure]
@tableName NVARCHAR(450)
AS
BEGIN

DECLARE @triggerName AS NVARCHAR(3000)
DECLARE @fullTriggerName AS NVARCHAR(3000)
DECLARE @canonTableName NVARCHAR(3000)
DECLARE @quotedTableName NVARCHAR(3000)

/* Create the trigger name */
SET @triggerName = REPLACE(@tableName, '[', '__o__')
SET @triggerName = REPLACE(@triggerName, ']', '__c__')
SET @triggerName = @triggerName + '_AspNet_SqlCacheNotification_Trigger'
SET @fullTriggerName = 'dbo.[' + @triggerName + ']'

/* Create the cannonicalized table name for trigger creation */
/* Do not touch it if the name contains other delimiters */
IF (CHARINDEX('.', @tableName) <> 0 OR
CHARINDEX('[', @tableName) <> 0 OR
CHARINDEX(']', @tableName) <> 0)
SET @canonTableName = @tableName
ELSE
SET @canonTableName = '[' + @tableName + ']'

/* First make sure the table exists */
IF (SELECT OBJECT_ID(@tableName, 'U')) IS NULL
BEGIN
RAISERROR ('00000001', 16, 1)
RETURN
END

BEGIN TRAN
/* Insert the value into the notification table */
IF NOT EXISTS (SELECT tableName FROM dbo.AspNet_SqlCacheTablesForChangeNotification WITH (NOLOCK) WHERE tableName = @tableName)
IF NOT EXISTS (SELECT tableName FROM dbo.AspNet_SqlCacheTablesForChangeNotification WITH (TABLOCKX) WHERE tableName = @tableName)
INSERT dbo.AspNet_SqlCacheTablesForChangeNotification
VALUES (@tableName, GETDATE(), 0)

/* Create the trigger */
SET @quotedTableName = QUOTENAME(@tableName, '''')
IF NOT EXISTS (SELECT name FROM sysobjects WITH (NOLOCK) WHERE name = @triggerName AND type = 'TR')
IF NOT EXISTS (SELECT name FROM sysobjects WITH (TABLOCKX) WHERE name = @triggerName AND type = 'TR')
EXEC('CREATE TRIGGER ' + @fullTriggerName + ' ON ' + @canonTableName +'
FOR INSERT, UPDATE, DELETE AS BEGIN
SET NOCOUNT ON
EXEC dbo.AspNet_SqlCacheUpdateChangeIdStoredProcedure N' + @quotedTableName + '
END
')
COMMIT TRAN
END

USE [MeiDongPay]
GO
/****** Object: StoredProcedure [dbo].[AspNet_SqlCacheUnRegisterTableStoredProcedure] Script Date: 05/12/2017 15:36:10 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AspNet_SqlCacheUnRegisterTableStoredProcedure]
@tableName NVARCHAR(450)
AS
BEGIN

BEGIN TRAN
DECLARE @triggerName AS NVARCHAR(3000)
DECLARE @fullTriggerName AS NVARCHAR(3000)
SET @triggerName = REPLACE(@tableName, '[', '__o__')
SET @triggerName = REPLACE(@triggerName, ']', '__c__')
SET @triggerName = @triggerName + '_AspNet_SqlCacheNotification_Trigger'
SET @fullTriggerName = 'dbo.[' + @triggerName + ']'

/* Remove the table-row from the notification table */
IF EXISTS (SELECT name FROM sysobjects WITH (NOLOCK) WHERE name = 'AspNet_SqlCacheTablesForChangeNotification' AND type = 'U')
IF EXISTS (SELECT name FROM sysobjects WITH (TABLOCKX) WHERE name = 'AspNet_SqlCacheTablesForChangeNotification' AND type = 'U')
DELETE FROM dbo.AspNet_SqlCacheTablesForChangeNotification WHERE tableName = @tableName

/* Remove the trigger */
IF EXISTS (SELECT name FROM sysobjects WITH (NOLOCK) WHERE name = @triggerName AND type = 'TR')
IF EXISTS (SELECT name FROM sysobjects WITH (TABLOCKX) WHERE name = @triggerName AND type = 'TR')
EXEC('DROP TRIGGER ' + @fullTriggerName)

COMMIT TRAN
END

USE [MeiDongPay]
GO
/****** Object: StoredProcedure [dbo].[AspNet_SqlCacheUpdateChangeIdStoredProcedure] Script Date: 05/12/2017 15:36:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AspNet_SqlCacheUpdateChangeIdStoredProcedure]
@tableName NVARCHAR(450)
AS

BEGIN
UPDATE dbo.AspNet_SqlCacheTablesForChangeNotification WITH (ROWLOCK) SET changeId = changeId + 1
WHERE tableName = @tableName
END

------------------------------------------------------------sqlserver存储过程代码-------------------------------------------------------

  

SqlServer缓存依赖 示例的更多相关文章

  1. SQL数据缓存依赖 [SqlServer | Cache | SqlCacheDependency ]

    前言 本文主要是对<ASP.NET 2.0开发指南>——<数据缓存>章节内容的提取并略有补充. 参考资料 1.     <ASP.NET 2.0开发指南> 2.   ...

  2. ASP.NET MVC 实现与SQLSERVER的依赖缓存

    原文:ASP.NET MVC 实现与SQLSERVER的依赖缓存 本文主要是为了了解ASP.NET MVC 实现与SQLSERVER的依赖缓存的功能,针对自己对于这方面知识的学习的过程的一个记录.由于 ...

  3. System.Web.Caching.Cache类 缓存 各种缓存依赖

    原文:System.Web.Caching.Cache类 缓存 各种缓存依赖 Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.C ...

  4. System.Web.Caching.Cache类 缓存 各种缓存依赖(转)

    转自:http://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntime ...

  5. System.Web.Caching.Cache类 Asp.Net缓存 各种缓存依赖

    Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.Cache都是该类的实例. 一.属性 属性 说明 Count 获取存储在缓存中的 ...

  6. C# System.Web.Caching.Cache类 缓存 各种缓存依赖

    原文:https://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntim ...

  7. SQL server数据缓存依赖

    SQL server数据缓存依赖有两种实现模式,轮询模式,通知模式. 1  轮询模式实现步骤 此模式需要SQL SERVER 7.0/2000/2005版本以上版本都支持        主要包含以下几 ...

  8. cache应用(asp.net 2.0 SQL数据缓存依赖 [SqlCacheDependency ] )

    Asp.net 2.0 提供了一个新的数据缓存功能,就是利用sql server2005 的异步通知功能来实现缓存 1.首先在sqlserver2005 中创建一个test的数据库. 在SQL Ser ...

  9. ASP.NET缓存全解析6:数据库缓存依赖 转自网络原文作者李天平

    更多的时候,我们的服务器性能损耗还是在查询数据库的时候,所以对数据库的缓存还是显得特别重要,上面几种方式都可以实现部分数据缓存功能.但问题是我们的数据有时候是在变化的,这样用户可能在缓存期间查询的数据 ...

随机推荐

  1. 百度天气接口api

    百度天气接口 以GET形式提交,返回JSON或XML URL:http://api.map.baidu.com/telematics/v3/weather?location={城市名}&out ...

  2. PAT 1127 ZigZagging on a Tree[难]

    1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...

  3. 【转】Deep Learning(深度学习)学习笔记整理系列之(一)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0  2013-0 ...

  4. with语句与__enter__,__exit__

    class Foo(object): def func(self): print("func") pass def __enter__(self): print("ent ...

  5. HDU - 2844 Coins(多重背包+完全背包)

    题意 给n个币的价值和其数量,问能组合成\(1-m\)中多少个不同的值. 分析 对\(c[i]*a[i]>=m\)的币,相当于完全背包:\(c[i]*a[i]<m\)的币则是多重背包,考虑 ...

  6. Apache配置WSGI

    Apache配置WSGI 什么是WSGI WSGI被称作web服务器网关接口,在笔者看来其实是基于CGI标准针对Python语言做了一些改进,其主要功能是规范了web 服务器与Pythonj应用程序之 ...

  7. Python3.x:函数定义

    Python3.x:函数定义 1,函数定义: def 函数名称([参数1,参数2,参数3......]): 执行语句 2,实例一(不带参数和没返回值): def helloWorld(): print ...

  8. GOEXIF读取和写入EXIF信息

    最新版本的gexif,直接基于gdi+实现了exif信息的读取和写入,代码更清晰. /* * File: gexif.h * Purpose: cpp EXIF reader * 3/2/2017 & ...

  9. 如何解决tensorflow报:Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2

    答:使能AVX,AVX2和FMA来进行源码编译,这样可以提速噢 具体编译方法,请参考windows10下如何进行源码编译安装tensorflow

  10. IntelliJ IDEA 常用快捷键,maven依赖图,个性化设置,禁用Search Everywhere

    查看idea 中jar关系图 快捷键: Ctrl+/ 用于注释,取消注释 Ctrl+Shift+F 全文搜索 Ctrl+F 单页面查找 Ctrl+Alt+Shift+L  格式化代码 ======== ...