Example #1
0
        private List <VirtualCacheBase> CreateVCs(int ThreadCount)
        {
            //VirtualCache.Persisted = true;
            List <VirtualCacheBase> vcs = new List <VirtualCacheBase>();
            // Use a profile that uses more resources to accomodate more data in-memory (faster!).
            var profile = new Profile
            {
                MemoryLimitInPercent = 98,
                MaxStoreCount        = ThreadCount,
                BTreeSlotLength      = 250
            };

            for (int i = 0; i < ThreadCount; i++)
            {
                var vc = new VirtualCache(string.Format("MyCacheStore{0}", i), true, null, profile);
                if (_logger == null)
                {
                    _logger          = vc.Logger;
                    _logger.LogLevel = Log.LogLevels.Verbose;
                    _logger.Information("Start of VirtualCache demo.");
                }
                vcs.Add(vc);
            }
            return(vcs);
        }
        private bool Populate(VirtualCache cache, bool slidingExpiration, int start)
        {
            var target = cache;

            _logger.Information("{0}: Start of Populating target cache {1}.", DateTime.Now, target.Name);
            try
            {
                // using a batch allows more efficient use of SOP data store so it can do bulk insert.
                CacheKeyValue[] batch  = new CacheKeyValue[5000];
                var             policy = new CacheItemPolicy()
                {
                    SlidingExpiration = new TimeSpan(0, 15, 0)
                };
                for (int i = start; i < start + MaxCacheEntries; i++)
                {
                    batch[i % batch.Length] = new CacheKeyValue()
                    {
                        Key    = string.Format("Hello{0}", i),
                        Value  = string.Format("Value{0}", i),
                        Policy = policy
                    };
                    if (i % batch.Length == batch.Length - 1)
                    {
                        target.SetValues(batch);
                    }
                }
            }
            catch (Exception exc)
            {
                _logger.Fatal(exc, "{0}: Failed Populating target cache {1}.", DateTime.Now, target.Name);
                return(false);
            }
            _logger.Information("{0}: End Populating target cache {1}.", DateTime.Now, target.Name);
            return(true);
        }
        public void Run()
        {
            var           ThreadCount = 5;
            VirtualCache  cache;
            List <Action> actions = new List <Action>();

            // Use a profile that uses more resources to accomodate more data in-memory (faster!).
            var profile = new Profile
            {
                MemoryLimitInPercent = 99,
                MaxStoreCount        = 1,
                BTreeSlotLength      = 150
            };

            cache = new VirtualCache("MyCacheStore", true, null, profile);
            if (_logger == null)
            {
                _logger          = cache.Logger;
                _logger.LogLevel = Log.LogLevels.Verbose;
                _logger.Information("Start of VirtualCache multi-clients simulation demo.");
            }
            // create threads that will populate Virtual Cache and retrieve the items.
            for (int i = 0; i < ThreadCount; i++)
            {
                var vcIndex = i;
                actions.Add(() =>
                {
                    if (Populate(cache, vcIndex % 2 == 0, vcIndex * MaxCacheEntries))
                    {
                        RetrieveAll(cache, vcIndex * MaxCacheEntries);
                    }
                });
            }

            Console.WriteLine("Starting client simulation threads.");
            List <Task> tasks = new List <Task>();

            foreach (var a in actions)
            {
                var t = TaskRun(a);
                if (t == null)
                {
                    continue;
                }
                tasks.Add(t);
            }
            // wait until all threads are finished.
            if (_threaded)
            {
                Task.WaitAll(tasks.ToArray());
            }

            Console.WriteLine("Before VirtualCache dispose.");
            cache.Dispose();
            Console.WriteLine("VirtualCache was disposed.");
            Console.WriteLine("End of VirtualCache demo.");
            Console.WriteLine("'Cached' & 'Accessed all' {0} records across {1} simulated clients.", MaxCacheEntries * ThreadCount, ThreadCount);
        }
Example #4
0
        public void Run()
        {
            List <Action>           actions = new List <Action>();
            List <VirtualCacheBase> vcs     = new List <VirtualCacheBase>();

            // create threads that will populate Virtual Cache and retrieve the items.
            for (int i = 0; i < ThreadCount; i++)
            {
                // Set "isPersisted" flag true if wanting to persist cached data across runs.
                // Non-persisted run (default) causes VirtualCache to be used as memory extender
                // utilizing disk for extending cached data capacity beyond what can fit in memory to
                // what Disk can accomodate.
                var vc = new VirtualCache(string.Format("MyCacheStore{0}", i), true);
                if (_logger == null)
                {
                    _logger          = vc.Logger;
                    _logger.LogLevel = Log.LogLevels.Verbose;
                    _logger.Information("Start of VirtualCache demo.");
                }
                // function to execute by the thread.
                actions.Add(() =>
                {
                    Populate(vc, i % 2 == 0);
                    RetrieveAll(vc);
                    var name = vc.Name;
                    vc.Dispose();
                    Console.WriteLine("VirtualCache {0} was disposed.", name);
                });
                vcs.Add(vc);
            }
            List <Task> tasks = new List <Task>();

            // launch or start the threads all at once.
            foreach (var a in actions)
            {
                var t = TaskRun(a);
                if (t == null)
                {
                    continue;
                }
                tasks.Add(t);
            }
            // wait until all threads are finished.
            if (_threaded)
            {
                Task.WaitAll(tasks.ToArray());
            }
            Console.WriteLine("End of VirtualCache demo.");
        }
        private bool RetrieveAll(VirtualCache cache, int start)
        {
            var source = cache;

            _logger.Information("{0}: Start of RetrieveAll cache entries from {1}.", DateTime.Now, source.Name);
            try
            {
                for (int i = start; i < start + MaxCacheEntries; i++)
                {
                    if ((string)source[string.Format("Hello{0}", i)] == null)
                    {
                        Console.WriteLine("Entry {0} was not found in cache {1}, 'it could have expired and got evicted.", string.Format("Hello{0}", i), source.Name);
                    }
                }
            }
            catch (Exception exc)
            {
                _logger.Fatal(exc, "{0}: Failed Retrieving items from target cache {1}.", DateTime.Now, source.Name);
                return(false);
            }
            _logger.Information("{0}: End of RetrieveAll cache entries from {1}.", DateTime.Now, source.Name);
            return(true);
        }
Example #6
0
        /// <summary>
        /// Sample code that uses this VirtualCache.
        /// </summary>
        public static void Run()
        {
            Console.WriteLine("{0}: Virtual Cache demo started...", DateTime.Now);
            VirtualCache.ServerPath = "SopBin\\";
            VirtualCache vc = new VirtualCache();
            ISortedDictionaryOnDisk cache1 = vc.GetObjectCache(1);
            const int MaxCount = 40000;
            for (int i = 0; i < MaxCount; i++)
                cache1.Add(i, string.Format("{0} cached data", i));

            Console.WriteLine("{0}: Finished inserting {1} records, reading 'em starts now...", DateTime.Now, MaxCount);
            cache1.MoveFirst();
            cache1.HintSequentialRead = true;
            for (int i = 0; i < MaxCount; i++)
            {
                string s = cache1.CurrentValue as string;
                if (string.IsNullOrEmpty(s) ||
                    s != string.Format("{0} cached data", i))
                    Console.WriteLine("Error, data not found.");
                cache1.MoveNext();
            }
            Console.WriteLine("{0}: Virtual Cache demo ended... {1} records were read.", DateTime.Now, MaxCount);
        }