예제 #1
0
 public void Save(MemoryData data)
 {
     if (AddOrUpdate(data.Id.ToString(), data))
     {
         //DataSyncQueueManager.SendToDb(GetPropertyValue, null, data);
         DataSyncManager.SendSql(new MemoryData[] { data }, true, GetPropertyValue);
     }
 }
예제 #2
0
        /// <summary>
        /// 从Redis内存移除,并保存到数据库,
        /// </summary>
        /// <param name="match">实体类型, 实体Key列表</param>
        public static void RemoveToDatabase(params KeyValuePair <Type, IList <string> >[] match)
        {
            var removeEntityKeys = new List <KeyValuePair <string, byte[][]> >();
            var entityList       = new List <EntityHistory>();

            RedisConnectionPool.ProcessReadOnly(client =>
            {
                foreach (var express in match)
                {
                    try
                    {
                        string hashtId    = RedisConnectionPool.GetRedisEntityKeyName(express.Key);
                        byte[][] keyBytes = express.Value.Select(t => RedisConnectionPool.ToByteKey(t)).ToArray();
                        if (keyBytes.Length == 0)
                        {
                            continue;
                        }
                        removeEntityKeys.Add(new KeyValuePair <string, byte[][]>(hashtId, keyBytes));
                        //转存到DB使用protobuf
                        byte[][] valueBytes = client.HMGet(hashtId, keyBytes);
                        for (int i = 0; i < keyBytes.Length; i++)
                        {
                            entityList.Add(new EntityHistory()
                            {
                                Key   = string.Format("{0}_{1}", hashtId, RedisConnectionPool.ToStringKey(keyBytes[i])),
                                Value = valueBytes[i]
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        TraceLog.WriteError("Redis cache remove key:{0} to Database error:{1}", express, ex);
                    }
                }
            });

            if (entityList.Count > 0)
            {
                DataSyncManager.SendSql <EntityHistory>(entityList, false, true);
                RedisConnectionPool.ProcessReadOnly(client =>
                {
                    foreach (var pair in removeEntityKeys)
                    {
                        client.HDel(pair.Key, pair.Value);
                    }
                });
            }
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="entityList"></param>
 /// <param name="synchronous"></param>
 /// <returns></returns>
 protected bool TrySaveToDb(IEnumerable <T> entityList, bool synchronous = false)
 {
     return(DataSyncManager.SendSql <T>(entityList, true, GetPropertyValue, null, synchronous));
 }