private void OnSync(object state) { try { if (Monitor.TryEnter(syncObject, 1000)) { try { var list = CacheChangeManager.Current.GetKeys(0, 100); foreach (var pair in list) { string key = pair.Key; try { CacheItemSet itemSet; CacheType cacheType = CacheType.None; dynamic entity = CacheFactory.GetPersonalEntity(key, out itemSet); if (itemSet != null) { cacheType = itemSet.ItemType; } if (entity == null) { entity = CovertEntityObject(key, pair.Value); if (entity != null) { SchemaTable schema; Type entityType = entity.GetType(); if (!EntitySchemaSet.TryGet(entityType, out schema)) { EntitySchemaSet.InitSchema(entityType); } if (schema != null || EntitySchemaSet.TryGet(entityType, out schema)) { cacheType = schema.CacheType; } } } if (entity != null && cacheType != CacheType.None) { string redisKey = cacheType == CacheType.Dictionary ? key.Split('|')[0] : key.Split('_')[0]; using (IDataSender sender = new RedisDataSender(redisKey)) { sender.Send(entity); if (itemSet != null) { itemSet.SetUnChange(); } Type type = entity.GetType(); string entityKey = string.Format("{0},{1}", key, type.Assembly.GetName().Name); CacheChangeManager.Current.PutSql(entityKey); } } } catch (Exception e) { TraceLog.WriteError("Redis sync key:{0} error:{1}", key, e); } } } finally { Monitor.Exit(syncObject); } } } catch (Exception ex) { TraceLog.WriteError("Redis sync error:{0}", ex); } }
private static void OnEntitySyncQueue(object state) { if (Interlocked.CompareExchange(ref _entityQueueRunning, 1, 0) == 0) { try { var tempRemove = Interlocked.Exchange(ref _entityRemoteSet, new HashSet <string>()); var temp = Interlocked.Exchange(ref _entitySet, new HashSet <string>()); if (temp.Count == 0 || _queueWatchTimers == null) { return; } TraceLog.WriteWarn("OnEntitySyncQueue execute count:{0}, success:{1}/total {2}, fail:{3} start...", temp.Count, ExecuteSuccessCount, SendWaitCount, ExecuteFailCount); RedisConnectionPool.Process(client => { while (temp.Count > 0) { var dumpSet = temp.Take(ExecuteCount).ToArray(); var pipeline = client.CreatePipeline(); try { bool hasPost = false; foreach (var key in dumpSet) { var keyValues = key.Split('_', '|'); if (keyValues.Length != 3) { TraceLog.WriteWarn("OnEntitySyncQueue:{0}", key); ExecuteFailCount++; continue; } AbstractEntity entity = CacheFactory.GetPersonalEntity(key) as AbstractEntity; int id = keyValues[1].ToInt(); string keyCode = keyValues[2]; string redisKey = string.Format("{0}_{1}", keyValues[0], keyCode); string hashId = GetRedisSyncQueueKey(id); byte[] idBytes = BufferUtils.GetBytes(id); var keyBytes = RedisConnectionPool.ToByteKey(redisKey); bool isDelete; byte[] entityBytes; if (entity != null) { isDelete = entity.IsDelete; entityBytes = _serializer.Serialize(entity); //modify resean: set unchange status. entity.Reset(); } else if (tempRemove.Contains(key)) { entityBytes = new byte[0]; isDelete = true; } else { ExecuteFailCount++; TraceLog.WriteError("EntitySync queue key {0} faild object is null.", key); continue; } byte[] stateBytes = BufferUtils.GetBytes(isDelete ? 1 : 0); byte[] values = BufferUtils.MergeBytes(BufferUtils.GetBytes(idBytes.Length + stateBytes.Length), idBytes, stateBytes, entityBytes); pipeline.QueueCommand(c => { ((RedisClient)c).HSet(hashId, keyBytes, values); }); hasPost = true; ExecuteSuccessCount++; } if (hasPost) { pipeline.Flush(); } } finally { pipeline.Dispose(); } try { foreach (var key in dumpSet) { temp.Remove(key); } } catch (Exception) { } } }); } catch (Exception ex) { TraceLog.WriteError("EntitySync queue {0}", ex); } finally { Interlocked.Exchange(ref _entityQueueRunning, 0); } } }
private static void OnEntitySyncQueue(object state) { if (Interlocked.CompareExchange(ref _entityQueueRunning, 1, 0) == 0) { try { var tempRemove = Interlocked.Exchange(ref _entityRemoteSet, new HashSet <string>()); var temp = Interlocked.Exchange(ref _entitySet, new HashSet <string>()); if (temp.Count == 0) { return; } using (var client = RedisConnectionPool.GetClient()) { var pipeline = client.CreatePipeline(); try { bool hasPost = false; foreach (var key in temp) { var keyValues = key.Split('_', '|'); if (keyValues.Length != 3) { continue; } AbstractEntity entity = CacheFactory.GetPersonalEntity(key) as AbstractEntity; int id = keyValues[1].ToInt(); string keyCode = keyValues[2]; string redisKey = string.Format("{0}_{1}", keyValues[0], keyCode); string hashId = GetRedisSyncQueueKey(id); byte[] idBytes = BufferUtils.GetBytes(id); var keyBytes = RedisConnectionPool.ToByteKey(redisKey); bool isDelete; byte[] entityBytes; if (entity != null) { isDelete = entity.IsDelete; entityBytes = _serializer.Serialize(entity); } else if (tempRemove.Contains(key)) { entityBytes = new byte[0]; isDelete = true; } else { TraceLog.WriteError("EntitySync queue key {0} faild object is null.", key); continue; } byte[] stateBytes = BufferUtils.GetBytes(isDelete ? 1 : 0); byte[] values = BufferUtils.MergeBytes(BufferUtils.GetBytes(idBytes.Length + stateBytes.Length), idBytes, stateBytes, entityBytes); pipeline.QueueCommand(c => ((RedisClient)c).HSet(hashId, keyBytes, values)); hasPost = true; } if (hasPost) { pipeline.Flush(); } } finally { pipeline.Dispose(); } } } catch (Exception ex) { TraceLog.WriteError("EntitySync queue {0}", ex); } finally { Interlocked.Exchange(ref _entityQueueRunning, 0); } } }