示例#1
0
 private static void Insert()
 {
     while (true)
     {
         lock (CacheCollection)
         {
             for (int i = 0; i < CacheCollection.Count(); i++)
             {
                 var person = CacheCollection[i];
                 if (!person.HasProcessed)
                 {
                     lock (myConnection)
                     {
                         Thread.Sleep(1200);
                         myConnection.Open();
                         var          strInsertSQL = "INSERT INTO `person` (`Id`,`Name`) VALUES ('" + person.Id + "', '" + person.Name + "')";
                         MySqlCommand cmdInserttblProductFrance = new MySqlCommand(strInsertSQL, myConnection);
                         cmdInserttblProductFrance.ExecuteNonQuery();
                         CloseConnection();
                         person.HasProcessed = true;
                     }
                 }
             }
         }
         CacheCollection.Clear();
     }
 }
示例#2
0
        public void GetOrCreate()
        {
            var cnt = 0;
            var key = 10;
            var src = new CacheCollection <int, int>(n => Sigma(n));

            src.Created += (s, e) => ++ cnt;
            Parallel.For(0, 10, _ => src.GetOrCreate(key));
            Task.Delay(200).Wait();

            Assert.That(cnt, Is.EqualTo(1));
            Assert.That(src.Count, Is.EqualTo(1));
            Assert.That(src.TryGetValue(key, out var _), Is.True, nameof(src.TryGetValue));
            Assert.That(src.TryGetValue(999, out var _), Is.False, nameof(src.TryGetValue));
            Assert.That(src.Contains(key), Is.True, nameof(src.Contains));
            Assert.That(src.GetOrCreate(key), Is.EqualTo(55), nameof(src.GetOrCreate));

            src.Remove(key);
            src.Clear();
            Assert.That(src.Count, Is.EqualTo(0));
        }
示例#3
0
        public void GetOrCreate()
        {
            using (var src = new CacheCollection <int, int>(i => Sigma(i)))
            {
                var n   = 0;
                var key = 10;

                src.Created += (s, e) => ++ n;
                Parallel.For(0, 10, e => src.GetOrCreate(key));

                Assert.That(Wait(() => n > 0), "Timeout");
                Assert.That(src.Count, Is.EqualTo(1), nameof(src.Count));
                Assert.That(src.TryGetValue(key, out _), Is.True, nameof(src.TryGetValue));
                Assert.That(src.TryGetValue(999, out _), Is.False, nameof(src.TryGetValue));
                Assert.That(src.Contains(key), Is.True, nameof(src.Contains));
                Assert.That(src.GetOrCreate(key), Is.EqualTo(55), nameof(src.GetOrCreate));

                src.Remove(key);
                src.Clear();
                Assert.That(src.Count, Is.EqualTo(0));
            }
        }