Esempio n. 1
0
        public void TestSmartCacheMutableObjects()
        {
            Shape shape1 = Shapes.newValidShape();
            Shape shape2 = Shapes.newValidShape();

            shape1.AddSubstroke(Shapes.newValidSubstroke());

            int shape1Value = numSubstrokes(shape1);
            int shape2Value = numSubstrokes(shape2);

            timesNumSubstrokesCalled = 0;

            SmartCache <Shape, int> cache = new SmartCache <Shape, int>(numSubstrokes);

            Assert.AreEqual(0, timesNumSubstrokesCalled, "SmartCache called the computation function in its constructor!?");

            Assert.AreEqual(shape1Value, cache[shape1], "SmartCache did not compute shape1 value correctly");
            Assert.AreEqual(shape2Value, cache[shape2], "SmartCache did not compute shape2 value correctly");

            Assert.AreEqual(shape1Value, cache[shape1], "SmartCache did not save shape1 value correctly");
            Assert.AreEqual(shape2Value, cache[shape2], "SmartCache did not save shape2 value correctly");

            Assert.AreEqual(2, timesNumSubstrokesCalled, "SmartCache didn't call the computation function twice!?");

            shape1.AddSubstroke(Shapes.newValidSubstroke());
            shape1Value = shape1.SubstrokesL.Count;

            Assert.AreEqual(shape1Value, cache[shape1], "SmartCache did not recompute shape1 value correctly");
            Assert.AreEqual(shape2Value, cache[shape2], "SmartCache did not save shape2 value correctly");

            Assert.AreEqual(3, timesNumSubstrokesCalled, "SmartCache didn't recompute shape1 value!?");
        }
Esempio n. 2
0
        public void Test_Sliding()
        {
            MockTimeProvider time = new MockTimeProvider();

            time.Now = new DateTime(2000, 1, 1);

            SmartCache <int> cache = new SmartCache <int>(5, time);

            int item;

            cache.Add("1", 1, TimeSpan.FromMinutes(100));
            cache.Add("2", 2, TimeSpan.FromMinutes(200));
            cache.Add("3", 3, TimeSpan.FromMinutes(300));

            time.Now += TimeSpan.FromMinutes(20);

            Assert.IsTrue(cache.TryGetValue("1", out item));
            Assert.AreEqual(3, cache.ItemCount);

            time.Now += TimeSpan.FromMinutes(150);

            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsTrue(cache.TryGetValue("2", out item));
            Assert.AreEqual(2, cache.ItemCount);

            time.Now += TimeSpan.FromDays(1);

            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsFalse(cache.TryGetValue("2", out item));
            Assert.IsFalse(cache.TryGetValue("3", out item));

            Assert.AreEqual(0, cache.ItemCount);
        }
Esempio n. 3
0
 public SmartCacheTest()
 {
     _cache      = new MemoryCache(new MemoryCacheOptions {
     });
     _smartCache = new SmartCache(_cache);
     _cache.Set("*****@*****.**", SmartCacheData.GetSample());
 }
Esempio n. 4
0
        public void Test_Remove()
        {
            SmartCache <int> cache = new SmartCache <int>(5);

            int item;

            Assert.AreEqual(0, cache.ItemCount);

            Assert.IsFalse(cache.TryGetValue("1", out item));

            cache.Add("1", 1);
            Assert.AreEqual(1, cache.ItemCount);

            cache.Add("1", 1);
            cache.Add("2", 2);
            cache.Add("3", 3);
            cache.Add("4", 4);
            cache.Add("5", 5);
            Assert.AreEqual(5, cache.ItemCount);

            cache.Remove("3");
            Assert.IsTrue(cache.TryGetValue("1", out item));
            Assert.AreEqual(1, item);
            Assert.IsTrue(cache.TryGetValue("2", out item));
            Assert.AreEqual(2, item);
            Assert.IsFalse(cache.TryGetValue("3", out item));
            Assert.IsTrue(cache.TryGetValue("4", out item));
            Assert.AreEqual(4, item);
            Assert.IsTrue(cache.TryGetValue("5", out item));
            Assert.AreEqual(5, item);
            Assert.AreEqual(4, cache.ItemCount);

            cache.Remove("1");
            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsTrue(cache.TryGetValue("2", out item));
            Assert.AreEqual(2, item);
            Assert.IsFalse(cache.TryGetValue("3", out item));
            Assert.IsTrue(cache.TryGetValue("4", out item));
            Assert.AreEqual(4, item);
            Assert.IsTrue(cache.TryGetValue("5", out item));
            Assert.AreEqual(5, item);
            Assert.AreEqual(3, cache.ItemCount);

            cache.Add("3", 3);
            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsTrue(cache.TryGetValue("2", out item));
            Assert.IsTrue(cache.TryGetValue("3", out item));
            Assert.IsTrue(cache.TryGetValue("4", out item));
            Assert.IsTrue(cache.TryGetValue("5", out item));
            Assert.AreEqual(4, cache.ItemCount);

            cache.Remove("6");
            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsTrue(cache.TryGetValue("2", out item));
            Assert.IsTrue(cache.TryGetValue("3", out item));
            Assert.IsTrue(cache.TryGetValue("4", out item));
            Assert.IsTrue(cache.TryGetValue("5", out item));
            Assert.AreEqual(4, cache.ItemCount);
        }
Esempio n. 5
0
        private void Oauth2Authorize(HttpRequestMessage request, HttpResponseMessage response)
        {
            if (request.Headers.Authorization == null || string.IsNullOrEmpty(request.Headers.Authorization.Parameter))
            {
                response.StatusCode = HttpStatusCode.Forbidden;
            }
            else
            {
                string   allowedScopes = ConfigurationManager.AppSettings["eso:scope"];
                string[] allowScopesCollection;
                if (!string.IsNullOrEmpty(allowedScopes))
                {
                    allowScopesCollection = allowedScopes.Split(' ');
                }

                string token = request.Headers.Authorization.Parameter;

                try
                {
                    var result = SmartCache <string> .Get(token, (key) => ParseToJWT(key));

                    //scopes compare and is time expires
                    var jobject       = (JObject)JsonConvert.DeserializeObject(result);
                    var scopeProperty = (JProperty)jobject.Children().FirstOrDefault(p => ((JProperty)p).Name.Equals("scope"));
                    var scope         = scopeProperty.Value;

                    var expProperty = (JProperty)jobject.Children().FirstOrDefault(p => ((JProperty)p).Name.Equals("exp"));

                    DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
                    var      exp       = startTime.AddSeconds(double.Parse(expProperty.Value.ToString()));
                    var      now       = DateTime.Now;

                    //expire check and resource scope check
                    //if (!allowScopesCollection.Any(p => p.Trim().Equals(scope.ToString().Trim())) || exp < now)
                    //{
                    //    response.StatusCode = HttpStatusCode.Forbidden;
                    //}
                    if (exp < now)
                    {
                        response.StatusCode = HttpStatusCode.Forbidden;
                    }
                    //client id check
                    var allowedClientId = ConfigurationManager.AppSettings["eso:client-id"];
                    if (!string.IsNullOrEmpty(allowedClientId))
                    {
                        var clientIdProperty = (JProperty)jobject.Children().FirstOrDefault(p => ((JProperty)p).Name.Equals("client_id"));
                        var clientId         = clientIdProperty.Value.ToString();
                        if (!allowedClientId.Trim().Equals(clientId.Trim()))
                        {
                            response.StatusCode = HttpStatusCode.Forbidden;
                        }
                    }
                }
                catch (Exception ex)
                {
                    response.StatusCode = HttpStatusCode.Forbidden;
                }
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Sets up the caches for a given featureSketch. This step is very important, since without caching
 /// the search refiner is intolerably slow.
 /// </summary>
 /// <param name="featureSketch"></param>
 public virtual void Start(Featurefy.FeatureSketch featureSketch)
 {
     _classifications = new SmartCache <Substroke, string>(
         s => {
         return(_classifier.classify(s, featureSketch));
     });
     _identificationResults = new Dictionary <SubstrokeCollection, Dictionary <ShapeType, RecognitionResult> >();
 }
Esempio n. 7
0
        /// <summary>
        /// Constructor. Does not add any substrokes from the sketch; you must do that yourself after construction.
        /// </summary>
        /// <param name="sketch"></param>
        /// <param name="extensionLengthsExtreme"></param>
        public IntersectionSketch(Sketch.Sketch sketch, SmartCache <Substroke, double> extensionLengthsExtreme)
        {
            m_Sketch = sketch;

            double avgArcLength = GetAvgArcLength(sketch);

            m_Strokes = new List <Substroke>();
            m_Boxes   = new SmartCache <Substroke, System.Drawing.RectangleF>(computeBoundingBox);
            m_Lines   = new SmartCache <Substroke, List <Line> >(computeLines);

            m_ExtensionLengthsExtreme = extensionLengthsExtreme;

            m_Stroke2Intersections = new Dictionary <Substroke, Dictionary <Substroke, Future <IntersectionPair> > >();
        }
Esempio n. 8
0
        public void TestSmartCacheRefs()
        {
            var cache = new SmartCache <Tuple <int, int>, int>(pairSum);

            timesPairSumCalled = 0;
            var p1 = Tuple.Create(1, 2);

            // Sanity check
            Assert.AreEqual(cache[p1], 3, "Computed value is incorrect");
            Assert.AreEqual(1, timesPairSumCalled, "Cache called pair sum too many times");
            Assert.AreEqual(cache[p1], 3, "Cached value is incorrect");
            Assert.AreEqual(1, timesPairSumCalled, "Cache called pair sum too many times");

            // Identical pairs should give identical results but should be recomputed
            Assert.AreEqual(cache[Tuple.Create(1, 2)], 3, "Cached value is incorrect");
            Assert.AreEqual(2, timesPairSumCalled, "Identical objects should be recomputed if they are a physically different object");
        }
Esempio n. 9
0
        public void ThreadedTest()
        {
            SmartCache <int> cache = new SmartCache <int>(50);

            Thread[] threads = new Thread[50];

            int    exceptionCount     = 0;
            object exceptionCountLock = new object();

            for (int i = 0; i < 50; i++)
            {
                threads[i] = new Thread(data =>
                {
                    try
                    {
                        for (int i1 = 0; i1 < 1000; i1++)
                        {
                            int x;

                            if (!cache.TryGetValue(i1.ToString(), out x))
                            {
                                cache.Add(i1.ToString(), i1);
                            }
                        }
                    }
                    catch
                    {
                        lock (exceptionCountLock)
                            exceptionCount++;

                        throw;
                    }
                });

                threads[i].Start();
            }

            for (int i = 0; i < 50; i++)
            {
                threads[i].Join();
            }

            Assert.AreEqual(50, cache.ItemCount);
            Assert.AreEqual(0, exceptionCount);
        }
Esempio n. 10
0
        public void TestSmartCacheGC()
        {
            var cache = new SmartCache <IntRef, int>(v => { return(v.Value); });

            int value = 5;
            var key   = new IntRef(value);

            // Sanity check
            Assert.AreEqual(value, cache[key]);
            key.Value = value = 15;
            Assert.AreEqual(value, cache[key]);

            // Let's reclaim the key.
            numObjectsFinalized = 0;
            key = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();

            // Make sure the value was reclaimed
            Assert.AreEqual(1, numObjectsFinalized, "Cache retained value when it should have been collected");
        }
Esempio n. 11
0
        public void ThreadedTest()
        {
            SmartCache<int> cache = new SmartCache<int>(50);

            Thread[] threads = new Thread[50];

            _exceptionCount = 0;

            for (int i=0;i<50;i++)
            {
                threads[i] = new Thread(trd);

                threads[i].Start(cache);
            }

            for (int i = 0; i < 50; i++)
                threads[i].Join();

            Assert.AreEqual(50,cache.ItemCount);
            Assert.AreEqual(0, _exceptionCount);
        }
Esempio n. 12
0
        private void ThreadCacheRemove(object data)
        {
            SmartCache <int> cache = (SmartCache <int>)data;

            try
            {
                Random random = new Random();

                for (int i = 0; i < 10000; i++)
                {
                    int x;

                    if (!cache.TryGetValue(i.ToString(), out x))
                    {
                        if (random.Next() % 4 != 0)
                        {
                            cache.Remove(i.ToString());
                        }
                        else
                        {
                            cache.Add(i.ToString(), i);
                        }
                    }
                    else
                    {
                        if (random.Next() % 3 == 0)
                        {
                            cache.Remove(i.ToString());
                        }
                    }
                }
            }
            catch
            {
                lock (_exceptionCountLock)
                    _exceptionCount++;

                throw;
            }
        }
Esempio n. 13
0
        public void ThreadedTest()
        {
            SmartCache <int> cache = new SmartCache <int>(50);

            Thread[] threads = new Thread[50];

            _exceptionCount = 0;

            for (int i = 0; i < 50; i++)
            {
                threads[i] = new Thread(trd);

                threads[i].Start(cache);
            }

            for (int i = 0; i < 50; i++)
            {
                threads[i].Join();
            }

            Assert.AreEqual(50, cache.ItemCount);
            Assert.AreEqual(0, _exceptionCount);
        }
Esempio n. 14
0
        private void trd(object data)
        {
            SmartCache <int> cache = (SmartCache <int>)data;

            try
            {
                for (int i = 0; i < 1000; i++)
                {
                    int x;

                    if (!cache.TryGetValue(i.ToString(), out x))
                    {
                        cache.Add(i.ToString(), i);
                    }
                }
            }
            catch
            {
                lock (_exceptionCountLock)
                    _exceptionCount++;

                throw;
            }
        }
Esempio n. 15
0
        private void Oauth1Authorize(HttpRequestMessage request, HttpResponseMessage response)
        {
            if (request.Headers.Authorization == null || string.IsNullOrEmpty(request.Headers.Authorization.Parameter))
            {
                response.StatusCode = HttpStatusCode.Forbidden;
            }
            else
            {
                // https://federation-sts.accenture.com/services/jwt/issue/adfs Production
                // https://federation-sts-stage.accenture.com/services/jwt/issue/adfs Staging
                string token = request.Headers.Authorization.Parameter;

                try
                {
                    var result = SmartCache <string> .Get(token, (key) => GetESOResponse(key));

                    response.StatusCode = string.IsNullOrEmpty(result) ? HttpStatusCode.Forbidden : HttpStatusCode.OK;
                }
                catch (Exception)
                {
                    response.StatusCode = HttpStatusCode.Forbidden;
                }
            }
        }
Esempio n. 16
0
 public UpdateCacheService()
 {
     smartCache = SmartCache.Instance;
     binding    = new NetTcpBinding();
     InitializeHosts();
 }
Esempio n. 17
0
 /// <summary>
 /// Creates a new AdaptiveImageRecognizer with the given templates
 /// </summary>
 /// <param name="templates"></param>
 public ImageRecognizer(List <BitmapSymbol> templates)
 {
     _shapesToSymbols = new SmartCache <Shape, BitmapSymbol>(createNewBitmapSymbolForShape);
     _templates       = templates;
 }
Esempio n. 18
0
        public void Test1()
        {
            SmartCache <int> cache = new SmartCache <int>(5);

            int item;

            Assert.AreEqual(0, cache.ItemCount);

            Assert.IsFalse(cache.TryGetValue("1", out item));

            cache.Add("1", 1);

            Assert.AreEqual(1, cache.ItemCount);

            Assert.IsTrue(cache.TryGetValue("1", out item));

            cache.Add("2", 2);
            cache.Add("3", 3);
            cache.Add("4", 4);
            cache.Add("5", 5);

            Assert.AreEqual(5, cache.ItemCount);

            Assert.IsTrue(cache.TryGetValue("1", out item));
            Assert.AreEqual(1, item);
            Assert.IsTrue(cache.TryGetValue("2", out item));
            Assert.AreEqual(2, item);
            Assert.IsTrue(cache.TryGetValue("3", out item));
            Assert.AreEqual(3, item);
            Assert.IsTrue(cache.TryGetValue("4", out item));
            Assert.AreEqual(4, item);
            Assert.IsTrue(cache.TryGetValue("5", out item));
            Assert.AreEqual(5, item);

            cache.Add("6", 6);

            Assert.AreEqual(5, cache.ItemCount);

            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsTrue(cache.TryGetValue("6", out item));

            Assert.IsTrue(cache.TryGetValue("3", out item));

            cache.Add("7", 7);
            cache.Add("8", 8);
            cache.Add("9", 9);

            Assert.AreEqual(5, cache.ItemCount);

            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsFalse(cache.TryGetValue("2", out item));
            Assert.IsTrue(cache.TryGetValue("3", out item));
            Assert.AreEqual(3, item);
            Assert.IsFalse(cache.TryGetValue("4", out item));
            Assert.IsFalse(cache.TryGetValue("5", out item));
            Assert.IsTrue(cache.TryGetValue("6", out item));
            Assert.AreEqual(6, item);
            Assert.IsTrue(cache.TryGetValue("7", out item));
            Assert.AreEqual(7, item);
            Assert.IsTrue(cache.TryGetValue("8", out item));
            Assert.AreEqual(8, item);
            Assert.IsTrue(cache.TryGetValue("9", out item));
            Assert.AreEqual(9, item);

            Assert.IsTrue(cache.TryGetValue("7", out item));
            Assert.AreEqual(7, item);

            cache.CacheSize = 2;

            Assert.AreEqual(2, cache.ItemCount);

            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsFalse(cache.TryGetValue("2", out item));
            Assert.IsFalse(cache.TryGetValue("3", out item));
            Assert.IsFalse(cache.TryGetValue("4", out item));
            Assert.IsFalse(cache.TryGetValue("5", out item));
            Assert.IsFalse(cache.TryGetValue("6", out item));
            Assert.IsTrue(cache.TryGetValue("7", out item));
            Assert.IsFalse(cache.TryGetValue("8", out item));
            Assert.IsTrue(cache.TryGetValue("9", out item));

            cache.ClearCache();

            Assert.AreEqual(0, cache.ItemCount);

            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsFalse(cache.TryGetValue("2", out item));
            Assert.IsFalse(cache.TryGetValue("3", out item));
            Assert.IsFalse(cache.TryGetValue("4", out item));
            Assert.IsFalse(cache.TryGetValue("5", out item));
            Assert.IsFalse(cache.TryGetValue("6", out item));
            Assert.IsFalse(cache.TryGetValue("7", out item));
            Assert.IsFalse(cache.TryGetValue("8", out item));
            Assert.IsFalse(cache.TryGetValue("9", out item));
        }
 public SmartCacheProtection()
     : base()
 {
     this.TargetCache  = new SmartCache(true, Connection.MAX_CACHE_SIZE, CacheMode.SimpleByteScan, 2500);
     this.ReceiveCache = new SmartCache(true, Connection.MAX_CACHE_SIZE, CacheMode.SimpleByteScan, 2500);
 }
Esempio n. 20
0
 public EmailRepository(SmartCache cache, MysqlContext db)
 {
     _cache = cache;
     _db    = db;
 }
Esempio n. 21
0
        public void Test_Sliding()
        {
            MockTimeProvider time = new MockTimeProvider();

            time.Now = new DateTime(2000, 1, 1);

            SmartCache<int> cache = new SmartCache<int>(5, time);

            int item;

            cache.Add("1", 1, TimeSpan.FromMinutes(100));
            cache.Add("2", 2, TimeSpan.FromMinutes(200));
            cache.Add("3", 3, TimeSpan.FromMinutes(300));

            time.Now += TimeSpan.FromMinutes(20);

            Assert.IsTrue(cache.TryGetValue("1", out item));
            Assert.AreEqual(3, cache.ItemCount);

            time.Now += TimeSpan.FromMinutes(150);

            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsTrue(cache.TryGetValue("2", out item));
            Assert.AreEqual(2, cache.ItemCount);

            time.Now += TimeSpan.FromDays(1);

            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsFalse(cache.TryGetValue("2", out item));
            Assert.IsFalse(cache.TryGetValue("3", out item));

            Assert.AreEqual(0,cache.ItemCount);
        }
Esempio n. 22
0
        public void Test_Remove()
        {
            SmartCache<int> cache = new SmartCache<int>(5);

            int item;

            Assert.AreEqual(0, cache.ItemCount);

            Assert.IsFalse(cache.TryGetValue("1", out item));

            cache.Add("1", 1);
            Assert.AreEqual(1, cache.ItemCount);

            cache.Add("1", 1);
            cache.Add("2", 2);
            cache.Add("3", 3);
            cache.Add("4", 4);
            cache.Add("5", 5);
            Assert.AreEqual(5, cache.ItemCount);

            cache.Remove("3");
            Assert.IsTrue(cache.TryGetValue("1", out item));
            Assert.AreEqual(1, item);
            Assert.IsTrue(cache.TryGetValue("2", out item));
            Assert.AreEqual(2, item);
            Assert.IsFalse(cache.TryGetValue("3", out item));
            Assert.IsTrue(cache.TryGetValue("4", out item));
            Assert.AreEqual(4, item);
            Assert.IsTrue(cache.TryGetValue("5", out item));
            Assert.AreEqual(5, item);
            Assert.AreEqual(4, cache.ItemCount);

            cache.Remove("1");
            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsTrue(cache.TryGetValue("2", out item));
            Assert.AreEqual(2, item);
            Assert.IsFalse(cache.TryGetValue("3", out item));
            Assert.IsTrue(cache.TryGetValue("4", out item));
            Assert.AreEqual(4, item);
            Assert.IsTrue(cache.TryGetValue("5", out item));
            Assert.AreEqual(5, item);
            Assert.AreEqual(3, cache.ItemCount);

            cache.Add("3", 3);
            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsTrue(cache.TryGetValue("2", out item));
            Assert.IsTrue(cache.TryGetValue("3", out item));
            Assert.IsTrue(cache.TryGetValue("4", out item));
            Assert.IsTrue(cache.TryGetValue("5", out item));
            Assert.AreEqual(4, cache.ItemCount);

            cache.Remove("6");
            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsTrue(cache.TryGetValue("2", out item));
            Assert.IsTrue(cache.TryGetValue("3", out item));
            Assert.IsTrue(cache.TryGetValue("4", out item));
            Assert.IsTrue(cache.TryGetValue("5", out item));
            Assert.AreEqual(4, cache.ItemCount);
        }
Esempio n. 23
0
        public void Test1()
        {
            SmartCache<int> cache = new SmartCache<int>(5);

            int item;

            Assert.AreEqual(0, cache.ItemCount);

            Assert.IsFalse(cache.TryGetValue("1", out item));

            cache.Add("1",1);

            Assert.AreEqual(1, cache.ItemCount);

            Assert.IsTrue(cache.TryGetValue("1", out item));

            cache.Add("2", 2);
            cache.Add("3", 3);
            cache.Add("4", 4);
            cache.Add("5", 5);

            Assert.AreEqual(5, cache.ItemCount);

            Assert.IsTrue(cache.TryGetValue("1", out item));
            Assert.AreEqual(1, item);
            Assert.IsTrue(cache.TryGetValue("2", out item));
            Assert.AreEqual(2, item);
            Assert.IsTrue(cache.TryGetValue("3", out item));
            Assert.AreEqual(3, item);
            Assert.IsTrue(cache.TryGetValue("4", out item));
            Assert.AreEqual(4, item);
            Assert.IsTrue(cache.TryGetValue("5", out item));
            Assert.AreEqual(5, item);

            cache.Add("6",6);

            Assert.AreEqual(5, cache.ItemCount);

            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsTrue(cache.TryGetValue("6", out item));

            Assert.IsTrue(cache.TryGetValue("3", out item));

            cache.Add("7", 7);
            cache.Add("8", 8);
            cache.Add("9", 9);

            Assert.AreEqual(5, cache.ItemCount);

            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsFalse(cache.TryGetValue("2", out item));
            Assert.IsTrue(cache.TryGetValue("3", out item));
            Assert.AreEqual(3, item);
            Assert.IsFalse(cache.TryGetValue("4", out item));
            Assert.IsFalse(cache.TryGetValue("5", out item));
            Assert.IsTrue(cache.TryGetValue("6", out item));
            Assert.AreEqual(6, item);
            Assert.IsTrue(cache.TryGetValue("7", out item));
            Assert.AreEqual(7, item);
            Assert.IsTrue(cache.TryGetValue("8", out item));
            Assert.AreEqual(8, item);
            Assert.IsTrue(cache.TryGetValue("9", out item));
            Assert.AreEqual(9, item);

            Assert.IsTrue(cache.TryGetValue("7", out item));
            Assert.AreEqual(7, item);

            cache.CacheSize = 2;

            Assert.AreEqual(2, cache.ItemCount);

            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsFalse(cache.TryGetValue("2", out item));
            Assert.IsFalse(cache.TryGetValue("3", out item));
            Assert.IsFalse(cache.TryGetValue("4", out item));
            Assert.IsFalse(cache.TryGetValue("5", out item));
            Assert.IsFalse(cache.TryGetValue("6", out item));
            Assert.IsTrue(cache.TryGetValue("7", out item));
            Assert.IsFalse(cache.TryGetValue("8", out item));
            Assert.IsTrue(cache.TryGetValue("9", out item));

            cache.ClearCache();

            Assert.AreEqual(0,cache.ItemCount);

            Assert.IsFalse(cache.TryGetValue("1", out item));
            Assert.IsFalse(cache.TryGetValue("2", out item));
            Assert.IsFalse(cache.TryGetValue("3", out item));
            Assert.IsFalse(cache.TryGetValue("4", out item));
            Assert.IsFalse(cache.TryGetValue("5", out item));
            Assert.IsFalse(cache.TryGetValue("6", out item));
            Assert.IsFalse(cache.TryGetValue("7", out item));
            Assert.IsFalse(cache.TryGetValue("8", out item));
            Assert.IsFalse(cache.TryGetValue("9", out item));
        }
Esempio n. 24
0
        public void ThreadedRemoveTest()
        {
            SmartCache <int> cache = new SmartCache <int>(50);

            Thread[] threads = new Thread[50];

            int    exceptionCount     = 0;
            object exceptionCountLock = new object();

            ParameterizedThreadStart t = o =>
            {
                try
                {
                    Random random = new Random();

                    for (int i1 = 0; i1 < 10000; i1++)
                    {
                        int x;

                        if (!cache.TryGetValue(i1.ToString(), out x))
                        {
                            if (random.Next() % 4 != 0)
                            {
                                cache.Remove(i1.ToString());
                            }
                            else
                            {
                                cache.Add(i1.ToString(), i1);
                            }
                        }
                        else
                        {
                            if (random.Next() % 3 == 0)
                            {
                                cache.Remove(i1.ToString());
                            }
                        }
                    }
                }
                catch
                {
                    lock (exceptionCountLock)
                        exceptionCount++;

                    throw;
                }
            };

            for (int i = 0; i < 50; i++)
            {
                threads[i] = new Thread(t);

                threads[i].Start();
            }

            for (int i = 0; i < 50; i++)
            {
                threads[i].Join();
            }

            Assert.AreEqual(50, cache.ItemCount);
            Assert.AreEqual(0, exceptionCount);
        }
 public EmailRepositoryStub()
 {
     _cache = new SmartCache(new MemoryCache(new MemoryCacheOptions {
     }));
 }