Ejemplo n.º 1
0
        private MemberDb memberDb = null;  //this is the database

        public MemberCache(MemberDb theDb)
        {
            if (theDb == null)
            {
                throw new ArgumentException();
            }

            this.memberDb = theDb;
        }
Ejemplo n.º 2
0
 // add some initial data.
 private static void InitDatabase(MemberDb memberDb)
 {
     memberDb.AddMember(new Member("Mia"));
     memberDb.AddMember(new Member("Vincent"));
     memberDb.AddMember(new Member("Jules"));
     memberDb.AddMember(new Member("Marsellus"));
     memberDb.AddMember(new Member("Winston"));
     memberDb.AddMember(new Member("Butch"));
     memberDb.AddMember(new Member("Jimmie"));
     memberDb.AddMember(new Member("Honey"));
     memberDb.AddMember(new Member("Pumpkin"));
     memberDb.AddMember(new Member("Koons"));
 }
Ejemplo n.º 3
0
        public static void AddNewMemberTask(MemberDb memberDb, List <Member> newMembers,
                                            int timespan, int interval)
        {
            int run     = 0;
            int elapsed = 0;

            while (elapsed < timespan && run < newMembers.Count)
            {
                memberDb.AddMember(newMembers[run]);
                run++;
                Thread.Sleep(interval);
                elapsed = elapsed + interval;
            }
        }
Ejemplo n.º 4
0
        public static void DeleteMemberTask(MemberDb memberDb, int timespan, int
                                            interval, int minId, int maxId)
        {
            int    elapsed  = 0;
            Random rand     = new Random();
            int    memberId = 0;

            while (elapsed < timespan)
            {
                memberId = rand.Next(minId, maxId); //Note, Id may not be valid
                memberDb.RemoveMember(memberId);
                Thread.Sleep(interval);
                elapsed = elapsed + interval;
            }
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            Console.WriteLine("Testing simple cache ...");

            MemberDb memberDb = new MemberDb();

            InitDatabase(memberDb);
            MemberCache memberCache = new MemberCache(memberDb);
            List <Task> tasks       = new List <Task>();

            //Start a task to add new members. Adds a new member every .5 secons for 5 secons
            Task task = new Task(() => AddNewMemberTask(memberDb, GetNewMemberList(), 5000, 500));

            task.Start();
            tasks.Add(task);

            //Start a task to delete random members. Deletes a member with id betwween
            //5-20 every .5 seconds for 5 seconds. Note: some id's may not be valid.
            task = new Task(() => DeleteMemberTask(memberDb, 5000, 500, 6, 15));
            task.Start();
            tasks.Add(task);

            //Create 2 tasks that read random ids between 1-7 every 250 ms for 5 seconds.
            task = new Task(() => CacheReader(memberCache, 5000, 250, 8));
            task.Start();
            tasks.Add(task);
            task = new Task(() => CacheReader(memberCache, 5000, 250, 8));
            task.Start();
            tasks.Add(task);

            //Create a task that read random ids between 1-20 every 250 ms for 5 seconds.
            //Note some requested Ids may not be valid
            task = new Task(() => CacheReader(memberCache, 5000, 250, 20));
            task.Start();
            tasks.Add(task);

            Task.WaitAll(tasks.ToArray());


            memberCache.PrintCache();
            memberDb.PrintDb();
        }