Beispiel #1
0
        private static void DoTestClassEquals(long firstLong, long secondLong)
        {
            // Two cache keys are equal except for the parameter.
            CacheKey key = new CacheKey();

            key.Update(firstLong);

            CacheKey aDifferentKey = new CacheKey();

            key.Update(secondLong);

            Assert.IsFalse(aDifferentKey.Equals(key)); // should not be equal.
        }
Beispiel #2
0
        public void TestCacheHit()
        {
            CacheModel cache = GetCacheModel();
            CacheKey key = new CacheKey();
            key.Update("testKey");

            string value = "testValue";
            cache[key] = value;

            object returnedObject = cache[key];
            Assert.AreEqual(value, returnedObject);
            Assert.AreEqual(HashCodeProvider.GetIdentityHashCode(value), HashCodeProvider.GetIdentityHashCode(returnedObject));
            Assert.AreEqual(1, cache.HitRatio);
        }
Beispiel #3
0
        public void CacheKeyWithTwoParamsSameHashcode()
        {
            CacheKey key1 = new CacheKey();
            CacheKey key2 = new CacheKey();

            key1.Update("HS1CS001");
            key1.Update("HS1D4001");

            key2.Update("HS1D4001");
            key2.Update("HS1CS001");

            #if dotnet2
            Assert.Ignore("The .NET 2.0 CLR uses a different algorithm for string hashing than the .NET 1.1 CLR.");
            #else
            Assert.AreEqual(key1.GetHashCode(), key2.GetHashCode(), "Expect same hashcode.");
            Assert.IsFalse(key1.Equals(key2), "Expect not equal");
            #endif
        }
Beispiel #4
0
        public void CacheKeyWithSameHashcode()
        {
            CacheKey key1 = new CacheKey();
            CacheKey key2 = new CacheKey();

            key1.Update("HS1CS001");
            key2.Update("HS1D4001");
            /*
             The string hash algorithm is not an industry standard and is not guaranteed to produce the same behaviour between versions.
             And in fact it does not. The .NET 2.0 CLR uses a different algorithm for string hashing than the .NET 1.1 CLR.
            */

            #if dotnet2
            Assert.Ignore("The .NET 2.0 CLR uses a different algorithm for string hashing than the .NET 1.1 CLR.");
            #else
            Assert.AreEqual( key1.GetHashCode(), key2.GetHashCode(), "Expect same hashcode.");
            Assert.IsFalse( key1.Equals(key2),"Expect not equal");
            #endif
        }
Beispiel #5
0
        public void TestReturnCopyOfCachedOject()
        {
            ICacheController cacheController = new LruCacheController();
            IDictionary props = new HybridDictionary();
            props.Add("CacheSize", "1");
            cacheController.Configure(props);

            FlushInterval interval = new FlushInterval();
            interval.Hours = 1;
            interval.Initialize();

            CacheModel cacheModel = new CacheModel();
            cacheModel.FlushInterval = interval;
            cacheModel.CacheController = cacheController;
            cacheModel.IsReadOnly = false;
            cacheModel.IsSerializable = true;

            Order order = new Order();
            order.CardNumber = "CardNumber";
            order.Date = DateTime.Now;
            order.LineItemsCollection = new LineItemCollection();
            LineItem item = new LineItem();
            item.Code = "Code1";
            order.LineItemsCollection.Add(item);
            item = new LineItem();
            item.Code = "Code2";
            order.LineItemsCollection.Add(item);

            CacheKey key = new CacheKey();
            key.Update(order);

            int firstId = HashCodeProvider.GetIdentityHashCode(order);
            cacheModel[ key ] = order;

            Order order2 = cacheModel[ key ] as Order;
            int secondId = HashCodeProvider.GetIdentityHashCode(order2);
            Assert.AreNotEqual(firstId, secondId, "hasCode equal");
        }
Beispiel #6
0
 public object GetLock(CacheKey key)
 {
     int controllerId = HashCodeProvider.GetIdentityHashCode(_controller);
     int keyHash = key.GetHashCode();
     int lockKey = 29 * controllerId + keyHash;
     object lok =_lockMap[lockKey];
     if (lok == null)
     {
         lok = lockKey; //might as well use the same object
         _lockMap[lockKey] = lok;
     }
     return lok;
 }
Beispiel #7
0
        /// <summary>
        /// Adds an item with the specified key and value into cached data.
        /// Gets a cached object with the specified key.
        /// </summary>
        /// <value>The cached object or <c>null</c></value>
        /// <remarks>
        /// A side effect of this method is that is may clear the cache
        /// if it has not been cleared in the flushInterval.
        /// </remarks> 
        public object this[CacheKey key]
        {
            get
            {
                lock(this)
                {
                    if (_lastFlush != NO_FLUSH_INTERVAL
                        && (DateTime.Now.Ticks - _lastFlush > _flushInterval.Interval))
                    {
                        Flush();
                    }
                }

                object value = null;
                lock (GetLock(key))
                {
                    value = _controller[key];
                }

                if(_isSerializable && !_isReadOnly &&
                    (value != NULL_OBJECT && value != null))
                {
                    try
                    {
                        MemoryStream stream = new MemoryStream((byte[]) value);
                        stream.Position = 0;
                        BinaryFormatter formatter = new BinaryFormatter();
                        value = formatter.Deserialize( stream );
                    }
                    catch(Exception ex)
                    {
                        throw new IBatisNetException("Error caching serializable object.  Be sure you're not attempting to use " +
                            "a serialized cache for an object that may be taking advantage of lazy loading.  Cause: "+ex.Message, ex);
                    }
                }

                lock(_statLock)
                {
                    _requests++;
                    if (value != null)
                    {
                        _hits++;
                    }
                }

                if (_logger.IsDebugEnabled)
                {
                    if (value != null)
                    {
                        _logger.Debug(String.Format("Retrieved cached object '{0}' using key '{1}' ", value, key));
                    }
                    else
                    {
                        _logger.Debug(String.Format("Cache miss using key '{0}' ", key));
                    }
                }
                return value;
            }
            set
            {
                if (null == value) {value = NULL_OBJECT;}
                if(_isSerializable && !_isReadOnly && value != NULL_OBJECT)
                {
                    try
                    {
                        MemoryStream stream = new MemoryStream();
                        BinaryFormatter formatter = new BinaryFormatter();
                        formatter.Serialize(stream, value);
                        value = stream.ToArray();
                    }
                    catch(Exception ex)
                    {
                        throw new IBatisNetException("Error caching serializable object. Cause: "+ex.Message, ex);
                    }
                }
                _controller[key] = value;
                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug(String.Format("Cache object '{0}' using key '{1}' ", value, key));
                }
            }
        }
Beispiel #8
0
        public void TestCacheHitMiss()
        {
            CacheModel cache = GetCacheModel();
            CacheKey key = new CacheKey();
            key.Update("testKey");

            string value = "testValue";
            cache[key] = value;

            object returnedObject = cache[key];
            Assert.AreEqual(value, returnedObject);
            Assert.AreEqual(HashCodeProvider.GetIdentityHashCode(value), HashCodeProvider.GetIdentityHashCode(returnedObject));

            CacheKey wrongKey = new CacheKey();
            wrongKey.Update("wrongKey");

            returnedObject = cache[wrongKey];
            Assert.IsTrue(!value.Equals(returnedObject));
            Assert.IsNull(returnedObject) ;
            Assert.AreEqual(0.5, cache.HitRatio);
        }
Beispiel #9
0
        public void TestDuplicateAddCache()
        {
            CacheModel cache = GetCacheModel();
            CacheKey key = new CacheKey();
            key.Update("testKey");
            string value = "testValue";

            object obj = null;
            obj = cache[key];
            Assert.IsNull(obj);
            obj = cache[key];
            Assert.IsNull(obj);

            cache[key] = value;
            cache[key] = value;

            object returnedObject = cache[key];
            Assert.AreEqual(value, returnedObject);
            Assert.AreEqual(HashCodeProvider.GetIdentityHashCode(value), HashCodeProvider.GetIdentityHashCode(returnedObject));
        }
Beispiel #10
0
        public void TestCacheNullObject()
        {
            CacheModel cache = GetCacheModel();
            CacheKey key = new CacheKey();
            key.Update("testKey");

            cache[key] = null;

            object returnedObject = cache[key];
            Assert.AreEqual(CacheModel.NULL_OBJECT, returnedObject);
            Assert.AreEqual(HashCodeProvider.GetIdentityHashCode(CacheModel.NULL_OBJECT), HashCodeProvider.GetIdentityHashCode(returnedObject));
            Assert.AreEqual(1, cache.HitRatio);
        }
Beispiel #11
0
        public void TestCacheMiss()
        {
            CacheModel cache = GetCacheModel();
            CacheKey key = new CacheKey();
            key.Update("testKey");

            string value = "testValue";
            cache[key] = value;

            CacheKey wrongKey = new CacheKey();
            wrongKey.Update("wrongKey");

            object returnedObject = cache[wrongKey];
            Assert.IsTrue(!value.Equals(returnedObject));
            Assert.IsNull(returnedObject) ;
            Assert.AreEqual(0, cache.HitRatio);
        }