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);
        }
Esempio n. 2
0
        private void Populate(int targetId, bool slidingExpiration = false)
        {
here:
            var target = vcs[targetId];

            _logger.Information("{0}: Start of Populating target cache {1}.", DateTime.Now, target.Name);
            try
            {
                for (int i = 0; i < MaxCacheEntries; i++)
                {
                    target.Set(string.Format("Hello{0}", i), string.Format("Value{0}", i), new CacheItemPolicy()
                    {
                        SlidingExpiration = new TimeSpan(0, 15, 0)
                    });
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine("Exception encountered: " + exc.Message);
                DisposeVCs(vcs);
                vcs = CreateVCs(ThreadCount);
                // this is a test/demo code, "goto" should be fine (in a hurry :).
                goto here;
            }
            _logger.Information("{0}: End Populating target cache {1}.", DateTime.Now, target.Name);
        }
Esempio n. 3
0
 private void Populate(ObjectCache target)
 {
     _logger.Information("{0}: Start of Populating target cache {1}.", DateTime.Now, target.Name);
     for (int i = 0; i < MaxCacheEntries; i++)
     {
         target.Set(string.Format("Hello{0}", i), string.Format("Value{0}", i), null);
     }
     _logger.Information("{0}: End Populating target cache {1}.", DateTime.Now, target.Name);
 }
Esempio n. 4
0
 private void Populate(ObjectCache target, bool slidingExpiration = false)
 {
     _logger.Information("{0}: Start of Populating target cache {1}.", DateTime.Now, target.Name);
     for (int i = 0; i < MaxCacheEntries; i++)
     {
         target.Set(string.Format("Hello{0}", i), string.Format("Value{0}", i), new CacheItemPolicy()
         {
             SlidingExpiration = new TimeSpan(0, 15, 0)
         });
     }
     _logger.Information("{0}: End Populating target cache {1}.", DateTime.Now, target.Name);
 }
Esempio n. 5
0
 const int MaxCacheEntries = 100000;       // 33778;         //10000000;
 private void Populate(ObjectCache target, int count = MaxCacheEntries)
 {
     _logger.Information("{0}: Start of Populating target cache {1}.", DateTime.Now, target.Name);
     for (int i = 0; i < count; i++)
     {
         if (i % 100 == 0)
         {
             _logger.Verbose("Cache Set on item# {0}.", i);
         }
         target.Set(string.Format("Hello{0}", i), string.Format("Value{0}", i), null);
     }
     _logger.Information("{0}: End Populating target cache {1}.", DateTime.Now, target.Name);
 }
Esempio n. 6
0
        private List <VirtualCacheWithBackgroundRefresh> CreateVCs(int ThreadCount)
        {
            //VirtualCache.Persisted = true;
            List <VirtualCacheWithBackgroundRefresh> vcs = new List <VirtualCacheWithBackgroundRefresh>();
            // 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 VirtualCacheWithBackgroundRefresh(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);
        }
        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);
        }
Esempio n. 8
0
        public void Run()
        {
            // Use a profile that uses more resources to accomodate more data in-memory (faster!).
            var profile = new Profile
            {
                MemoryLimitInPercent = 98,
                MaxStoreCount        = 1,
                BTreeSlotLength      = 250
            };

            using (Sop.Caching.VirtualCache vc = new Sop.Caching.VirtualCache("MyCacheStore", true, null, profile))
            {
                _logger          = vc.Logger;
                _logger.LogLevel = Log.LogLevels.Verbose;
                _logger.Information("Start of VirtualCache demo.");
                Populate(vc);
                RetrieveAll(vc);
                _logger.Information("End of VirtualCache demo.");
                _logger.Information("You just 'cached' & 'accessed all' {0} records.", MaxCacheEntries);
            }
        }
Esempio n. 9
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.");
        }
Esempio n. 10
0
        public void Run()
        {
            const int LoopCount = 3;

            Sop.Caching.VirtualCache vc = new Sop.Caching.VirtualCache("MyCacheStore", true);
            for (int i = 0; i < LoopCount; i++)
            {
                _logger          = vc.Logger;
                _logger.LogLevel = Log.LogLevels.Verbose;
                _logger.Information("Start of VirtualCache MemoryCache Compare demo.");
                try
                {
                    ObjectCache oc = MemoryCache.Default;
                    if (i == 0)
                    {
                        Populate(vc);
                    }
                    else
                    {
                        Populate(vc);
                    }
                    //Populate(oc);
                    RetrieveAll(vc);
                    RetrieveAll(oc);
                    Compare(vc, oc);
                    _logger.Information("End of VirtualCache MemoryCache Compare demo.");
                    vc.Commit(true);
                }
                catch (TransactionRolledbackException)
                {
                    vc.Dispose();
                    vc = new Sop.Caching.VirtualCache("MyCacheStore");
                }
            }
            vc.Dispose();
        }