Esempio n. 1
0
        public static void ReadCacheAndCountItems(MyCache cache, int i)
        {
            Random rnd   = new Random();
            int    rnNum = rnd.Next(1, 20);

            Thread.Sleep(200 * rnNum);

            var itemCount = cache.GetCachedItems().Count;

            Console.WriteLine($"Task {i}: found {itemCount} items in cache after sleeping for {200 * rnNum}");
        }
Esempio n. 2
0
        static void ReadAndUpdateCacheSimple(MyCache cache)
        {
            var cachedItems = cache.GetCachedItems();

            foreach (var item in cachedItems)
            {
                Console.WriteLine($"found item with id {item.Id } and name {item.Name}");
            }

            cache.RefreshCachedItems();
            cachedItems = cache.GetCachedItems();

            foreach (var item in cachedItems)
            {
                Console.WriteLine($"found item with id {item.Id } and name {item.Name}");
            }
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            var cache = new MyCache();

            //ReadAndUpdateCacheSimple(cache);

            for (int i = 0; i < 30; i++)
            {
                var taskId = i;
                Task.Factory.StartNew(() => ReadCacheAndCountItems(cache, taskId));
                //Console.WriteLine($"started task {taskId}");
            }

            Random rnd   = new Random();
            int    rnNum = rnd.Next(1, 20);

            Thread.Sleep(1000 * rnNum);

            cache.RefreshCachedItems();

            Console.Read();
        }