コード例 #1
0
ファイル: Form1.cs プロジェクト: giangcoffee/cafef.redis
 private void button23_Click(object sender, EventArgs e)
 {
     var redis = new RedisClient(ConfigRedis.Host, ConfigRedis.Port);
     var sql = new SqlDb();
     sql.OpenDb();
     var date = "";
     #region Lịch sự kiện
     var lskdt = sql.GetLichSuKien(date);
     var keys = redis.ContainsKey(RedisKey.KeyLichSuKien) ? redis.Get<List<string>>(RedisKey.KeyLichSuKien) : new List<string>();
     var removals = keys.FindAll(s => s.Substring(0, 8) == date.Replace(".", ""));
     foreach (var removal in removals)
     {
         var key = string.Format(RedisKey.KeyLichSuKienObject, removal.Substring(removal.LastIndexOf(":") + 1));
         if (redis.ContainsKey(key)) redis.Remove(key);
         keys.Remove(removal);
     }
     foreach (DataRow ldr in lskdt.Rows)
     {
         var o = new LichSuKien() { ID = int.Parse(ldr["ID"].ToString()), LoaiSuKien = ldr["EventType_List"].ToString(), MaCK = ldr["StockSymbols"].ToString(), MaSan = 0, News_ID = ldr["News_ID"].ToString(), Title = ldr["EventTitle"].ToString(), NgayBatDau = ldr["NgayBatDau"].ToString(), NgayKetThuc = ldr["NgayKetThuc"].ToString(), NgayThucHien = ldr["NgayThucHien"].ToString(), TenCty = "", TomTat = "", PostDate = (DateTime) ldr["PostDate"]};
         try
         {
             o.EventDate = (DateTime) ldr["EventDate"];
         }catch(Exception)
         {
             o.EventDate = DateTime.Parse("2000-01-01");
         }
         var key = string.Format(RedisKey.KeyLichSuKienObject, o.ID);
         if (redis.ContainsKey(key))
             redis.Set(key, o);
         else
             redis.Add(key, o);
         key = string.Format(RedisKey.KeyLichSuKienObjectInList, o.EventDate.ToString("yyyyMMdd"), string.IsNullOrEmpty(o.LoaiSuKien.Trim())? "_" : o.LoaiSuKien, o.ID);
         if (!keys.Contains(key)) keys.Add(key);
     }
     if (redis.ContainsKey(RedisKey.KeyLichSuKien))
         redis.Set(RedisKey.KeyLichSuKien, keys);
     else
         redis.Add(RedisKey.KeyLichSuKien, keys);
     #endregion
     #region Lịch sự kiện tóm tắt
     lskdt = sql.GetLichSuKienTomTat();
     var lls = new List<LichSuKien>();
     foreach (DataRow ldr in lskdt.Rows)
     {
         var o = new LichSuKien() { ID = int.Parse(ldr["ID"].ToString()), LoaiSuKien = ldr["EventType_List"].ToString(), MaCK = ldr["StockSymbols"].ToString(), MaSan = 0, News_ID = ldr["News_ID"].ToString(), Title = ldr["EventTitle"].ToString(), NgayBatDau = ldr["NgayBatDau"].ToString(), NgayKetThuc = ldr["NgayKetThuc"].ToString(), NgayThucHien = ldr["NgayThucHien"].ToString(), TenCty = "", TomTat = "", PostDate = (DateTime)ldr["PostDate"] };
         try
         {
             o.EventDate = (DateTime)ldr["EventDate"];
         }
         catch (Exception)
         {
             o.EventDate = DateTime.Parse("2000-01-01");
         }
         lls.Add(o);
     }
     if (redis.ContainsKey(RedisKey.KeyLichSuKienTomTat))
         redis.Set(RedisKey.KeyLichSuKienTomTat, lls);
     else
         redis.Add(RedisKey.KeyLichSuKienTomTat, lls);
     #endregion
     sql.CloseDb();
 }