private static void SynLocalCacheByCacheConfig(DataTable dtCacheTable) { string strTableName = dtCacheTable.Rows[0]["TableName"].ToString(); string strId = dtCacheTable.Rows[0]["Id"].ToString(); string strConn = dtCacheTable.Rows[0]["ConnectionString"].ToString(); DatabaseTable databaseTable = CacheManager.GetDatabaseTable(strConn, SqlType.SqlServer, strTableName); if (databaseTable != null) { string strDdl = CacheManager.RunTableDdl(databaseTable, SqlType.SQLite); strDdl += "; SELECT 'ok'"; IDbHelper helper = GlobalHelp.GetDataAccessSqliteHelper(); helper.CreateCommand("DROP Table IF EXISTS [" + strTableName + "]"); helper.ExecuteNonQuery(); helper.CreateCommand(strDdl); DataTable dtCache = helper.ExecuteQuery(); if (dtCache.Rows.Count > 0 && dtCache.Rows[0][0].ToString().Trim() == "ok") { //删除本地缓存设置表数据 helper.CreateCommand("DELETE FROM CacheConfig WHERE TableName ='" + strTableName + "' "); helper.ExecuteNonQuery(); //获取服务器缓存表数据,并同步至本地缓存数据库 IDbHelper helperSql = new MSSQLHelper(strConn); helperSql.CreateCommand("SELECT * FROM " + strTableName + " with(nolock)"); DataTable dtBatch = helperSql.ExecuteQuery(); dtBatch.TableName = strTableName; for (int i = 0; i < dtBatch.Columns.Count; i++) { dtBatch.Columns[i].AutoIncrement = false; } helper.BatchInsert(dtBatch); //同步本地缓存设置表数据 dtCacheTable.TableName = "CacheConfig"; for (int i = 0; i < dtCacheTable.Columns.Count; i++) { dtCacheTable.Columns[i].AutoIncrement = false; } for (int i = 0; i < dtCacheTable.Rows.Count; i++) { dtCacheTable.Rows[i]["ConnectionString"] = string.Empty; } helper.BatchInsert(dtCacheTable); if (!CacheManager.CacheDictionary.ContainsKey(strTableName)) { CacheManager.CacheDictionary.Add(strTableName, strTableName); } } } }
/// <summary>处理接受到的消息 /// /// </summary> /// <param name="data"></param> /// <param name="responseChannel"></param> public static void Received(NetworkData data, IConnection responseChannel) { try { var message = MessageConverter.ToMessage(data); if (message.Command == Command.Send) { if (message.Content.Contains("join group successful")) { return; } CacheMessageEntity entity = JsonConvert.DeserializeObject <CacheMessageEntity>(message.Content); string strKey = entity.CacheKey; string strOperation = entity.Operation; DataTable dtCacheTable = entity.DataTableCache; //新增缓存配置,同步缓存 if (strOperation == "Add") { SynLocalCacheByCacheConfig(dtCacheTable); } //删除缓存配置,同步缓存 else if (strOperation == "Delete") { string strTableName = dtCacheTable.Rows[0]["TableName"].ToString(); string strId = dtCacheTable.Rows[0]["Id"].ToString(); IDbHelper helper = GlobalHelp.GetDataAccessSqliteHelper(); helper.CreateCommand("DELETE FROM CacheConfig WHERE Id=" + strId + ""); helper.ExecuteNonQuery(); helper.CreateCommand("DROP Table IF EXISTS [" + strTableName + "]"); helper.ExecuteNonQuery(); if (CacheManager.CacheDictionary.ContainsKey(strTableName)) { CacheManager.CacheDictionary.Remove(strTableName); } } } } catch (Exception) { } }
internal static string GetLocalCacheIds() { IDbHelper dbSqliteHelper = GlobalHelp.GetDataAccessSqliteHelper(); string strSql = "Select Id from CacheConfig "; dbSqliteHelper.CreateCommand(strSql); //dbSqliteHelper. DataTable dt = dbSqliteHelper.ExecuteQuery(); if (dt.Rows.Count > 0) { StringBuilder sb = new StringBuilder(); sb.Append(("(")); foreach (DataRow dr in dt.Rows) { sb.Append(dr["Id"]); sb.Append(","); } sb.Remove(sb.Length - 1, 1); sb.Append((")")); return(sb.ToString()); } return(string.Empty); }
/// <summary>同步缓存 /// /// </summary> public static void SynCache() { IDbHelper helperSqlServer = GlobalHelp.GetDataAccessHelper(); IDbHelper helperSqlite = GlobalHelp.GetDataAccessSqliteHelper(); string strSql = "Select * from CacheConfig"; string strIds = GetLocalCacheIds(); if (strIds != string.Empty) { strSql = strSql + " where Id not in(" + strIds + ") "; } helperSqlServer.CreateCommand(strSql); DataTable dtCacheTable = helperSqlServer.ExecuteQuery(); if (dtCacheTable.Rows.Count <= 0) { return; } foreach (DataRow dr in dtCacheTable.Rows) { string strTableName = dr["TableName"].ToString(); string strId = dr["Id"].ToString(); string strConn = dr["ConnectionString"].ToString(); DatabaseTable databaseTable = GetDatabaseTable(strConn, SqlType.SqlServer, strTableName); if (databaseTable != null) { #region 创建本地脚本 string strDdl = RunTableDdl(databaseTable, SqlType.SQLite); strDdl += "; SELECT 'ok'"; helperSqlite.CreateCommand("DROP Table IF EXISTS [" + strTableName + "]"); helperSqlite.ExecuteNonQuery(); helperSqlite.CreateCommand(strDdl); DataTable dtCache = helperSqlite.ExecuteQuery(); #endregion if (dtCache.Rows.Count > 0 && dtCache.Rows[0][0].ToString().Trim() == "ok") { //删除本地缓存设置表数据 helperSqlite.CreateCommand("DELETE FROM CacheConfig WHERE TableName ='" + strTableName + "' "); helperSqlite.ExecuteNonQuery(); //获取服务器缓存表数据,并同步至本地缓存数据库 IDbHelper helperTemp = new MSSQLHelper(strConn); helperTemp.CreateCommand("SELECT * FROM " + strTableName + " with(nolock)"); DataTable dtBatch = helperTemp.ExecuteQuery(); dtBatch.TableName = strTableName; for (int i = 0; i < dtBatch.Columns.Count; i++) { dtBatch.Columns[i].AutoIncrement = false; } helperSqlite.BatchInsert(dtBatch); //同步本地缓存设置表数据 dtCacheTable.TableName = "CacheConfig"; for (int i = 0; i < dtCacheTable.Columns.Count; i++) { dtCacheTable.Columns[i].AutoIncrement = false; } for (int i = 0; i < dtCacheTable.Rows.Count; i++) { dtCacheTable.Rows[i]["ConnectionString"] = string.Empty; } helperSqlite.BatchInsert(dtCacheTable); } } } }