示例#1
0
        public void RecentlyUsedLookupTest()
        {
            string value;

            MruCache <int, string> mruCache = new MruCache <int, string>(100);

            for (int i = 0; i < 200; ++i)
            {
                mruCache.TryAddValue(i, i.ToString());
                for (int j = 0; j < i; j += 10)
                {
                    Assert.True(mruCache.TryGetValue(j, out value));
                    Assert.Equal(j.ToString(), value);
                }
            }

            for (int j = 0; j < 100; j += 10)
            {
                Assert.True(mruCache.TryGetValue(j, out value));
                Assert.Equal(j.ToString(), value);
            }

            for (int i = 170; i < 200; ++i)
            {
                Assert.True(mruCache.TryGetValue(i, out value));
                Assert.Equal(i.ToString(), value);
            }
        }
示例#2
0
        public void RaiseCallStateChanged(Call call)
        {
            CallStateChangedEventArgs ea;

            if (_eaCache.TryGetValue(new ValueWrapper <int>(call.Id), out ea))
            {
                ea.InviteState = call.InviteState;
                ea.MediaState  = call.MediaState;
                CallStateChanged(this, ea);
            }
        }
        public void Clear_removes_all_entries()
        {
            var cache = new MruCache <int, string>(capacity: 3);

            cache.Add(1, "one");
            cache.Add(2, "two");
            cache.Add(3, "three");

            cache.Clear();

            Assert.False(cache.TryGetValue(1, out _));
            Assert.False(cache.TryGetValue(2, out _));
            Assert.False(cache.TryGetValue(3, out _));
        }
示例#4
0
        public static Func <object[], TResult> GetFunc <TResult>
        (
            CodeActivityMetadata metadata
            , ConstructorInfo constructorInfo
            , MruCache <ConstructorInfo, Func <object[], TResult> > cache
            , ReaderWriterLockSlim locker
        )
        {
            Func <object[], TResult> func = null;

            if (constructorInfo != null)
            {
                locker.EnterWriteLock();
                try
                {
                    cache.TryGetValue(constructorInfo, out func);
                }
                finally
                {
                    locker.ExitWriteLock();
                }
            }
            if (func == null)
            {
                func = GetFunc <TResult>(metadata, constructorInfo);
                if (constructorInfo != null)
                {
                    locker.EnterWriteLock();
                    try
                    {
                        //MruCache has on ContainsKey(), so we use TryGetValue()
                        Func <object[], TResult> result = null;
                        if (!cache.TryGetValue(constructorInfo, out result))
                        {
                            cache.Add(constructorInfo, func);
                        }
                        else
                        {
                            func = result;
                        }
                    }
                    finally
                    {
                        locker.ExitWriteLock();
                    }
                }
            }
            return(func);
        }
        public void TryRemove_removes_existing_entries()
        {
            var cache = new MruCache <int, string>(capacity: 3);

            cache.Add(1, "one");
            cache.Add(2, "two");
            cache.Add(3, "three");

            Assert.True(cache.TryRemove(1, out var removed));
            Assert.AreEqual("one", removed);
            Assert.False(cache.TryGetValue(1, out _));

            Assert.True(cache.TryGetValue(2, out _));
            Assert.True(cache.TryGetValue(3, out _));
        }
示例#6
0
 private void AppendEnumAsString(StringBuilder sb, Enum value)
 {
     if (!_enumCache.TryGetValue(value, out var textValue))
     {
         textValue = value.ToString();
         _enumCache.TryAddValue(value, textValue);
     }
     sb.Append(textValue);
 }
示例#7
0
        public void SimpleCacheAddAndLookupTest()
        {
            MruCache <int, string> mruCache = new MruCache <int, string>(100);

            for (int i = 0; i < 100; ++i)
            {
                mruCache.TryAddValue(i, i.ToString());
            }

            string value;

            for (int i = 0; i < 100; ++i)
            {
                Assert.True(mruCache.TryGetValue(i, out value));
                Assert.Equal(i.ToString(), value);
            }

            Assert.False(mruCache.TryGetValue(101, out value));
        }
示例#8
0
        public void OverflowCacheAndLookupTest()
        {
            MruCache <int, string> mruCache = new MruCache <int, string>(100);

            for (int i = 0; i < 200; ++i)
            {
                mruCache.TryAddValue(i, i.ToString());
            }

            string value;

            Assert.False(mruCache.TryGetValue(0, out value));
            Assert.False(mruCache.TryGetValue(90, out value));

            for (int i = 120; i < 200; ++i)
            {
                Assert.True(mruCache.TryGetValue(i, out value));
                Assert.Equal(i.ToString(), value);
            }
        }
示例#9
0
        private string EnumAsString(Enum value)
        {
            string textValue;

            if (!_enumCache.TryGetValue(value, out textValue))
            {
                textValue = Convert.ToString(value, CultureInfo.InvariantCulture);
                _enumCache.TryAddValue(value, textValue);
            }
            return(textValue);
        }
示例#10
0
        public void OverflowFreshCacheAndLookupTest()
        {
            string value;
            MruCache <int, string> mruCache = new MruCache <int, string>(100);

            for (int i = 0; i < 200; ++i)
            {
                mruCache.TryAddValue(i, i.ToString());
                Assert.True(mruCache.TryGetValue(i, out value));    // No longer a virgin
                Assert.Equal(i.ToString(), value);
            }

            for (int j = 0; j < 2; ++j)
            {
                for (int i = 110; i < 200; ++i)
                {
                    if (!mruCache.TryGetValue(i, out value))
                    {
                        mruCache.TryAddValue(i, i.ToString());
                        Assert.True(mruCache.TryGetValue(i, out value));
                    }
                }
            }

            for (int i = 300; i < 310; ++i)
            {
                mruCache.TryAddValue(i, i.ToString());
            }

            int cacheCount = 0;

            for (int i = 110; i < 200; ++i)
            {
                if (mruCache.TryGetValue(i, out value))
                {
                    ++cacheCount;
                }
            }

            Assert.True(cacheCount > 60);   // See that old cache was not killed
        }
示例#11
0
        public void RaiseCallStateChanged(ICallInternal call)
        {
            CallStateChangedEventArgs ea;

            if (_eaCache.TryGetValue(new ValueWrapper <int>(call.Id), out ea))
            {
                ea.InviteState = call.InviteState;
                ea.MediaState  = call.MediaState;
                //try
                //{
                //    if (_syncContext != null)
                //        _syncContext.Post(s => CallStateChanged(this, ea), null);
                //    else
                //        CallStateChanged(this, ea);
                //}
                //catch (InvalidOperationException)
                //{
                CallStateChanged(this, ea);
                //}
            }
        }
示例#12
0
        /// <summary>
        /// Get properties, cached for a type
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]> GetProps(object value)
        {
            var type = value.GetType();
            KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]> props;

            if (_propsCache.TryGetValue(type, out props))
            {
                if (props.Key.Length != 0 && props.Value.Length == 0)
                {
                    var lateBoundMethods = new ReflectionHelpers.LateBoundMethod[props.Key.Length];
                    for (int i = 0; i < props.Key.Length; i++)
                    {
                        lateBoundMethods[i] = ReflectionHelpers.CreateLateBoundMethod(props.Key[i].GetGetMethod());
                    }
                    props = new KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]>(props.Key, lateBoundMethods);
                    _propsCache.TryAddValue(type, props);
                }
                return(props);
            }

            PropertyInfo[] properties = null;

            try
            {
                properties = type.GetProperties(PublicProperties);
            }
            catch (Exception ex)
            {
                InternalLogger.Warn(ex, "Failed to get JSON properties for type: {0}", type);
            }
            finally
            {
                if (properties == null)
                {
                    properties = ArrayHelper.Empty <PropertyInfo>();
                }
            }

            // Skip Index-Item-Properties (Ex. public string this[int Index])
            foreach (var prop in properties)
            {
                if (!prop.CanRead || prop.GetIndexParameters().Length != 0 || prop.GetGetMethod() == null)
                {
                    properties = properties.Where(p => p.CanRead && p.GetIndexParameters().Length == 0 && p.GetGetMethod() != null).ToArray();
                    break;
                }
            }

            props = new KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]>(properties, ArrayHelper.Empty <ReflectionHelpers.LateBoundMethod>());
            _propsCache.TryAddValue(type, props);
            return(props);
        }
示例#13
0
        public void when_TryGetValue_called_with_nonexistent_key__should_return_false()
        {
            var mruCache = new MruCache<ValueWrapper<int>, object>(12);

            object added = new object();
            var key = new ValueWrapper<int>(0);

            mruCache.Add(key, added);

            object res;
            Assert.IsFalse(mruCache.TryGetValue(new ValueWrapper<int>(1), out res));
            Assert.IsNull(res);
        }
示例#14
0
        public void when_TryGetValue_called_with_existing_key__should_return_what_it_was_provided()
        {
            var mruCache = new MruCache<ValueWrapper<int>, object>(12);

            object added = new object();
            var key = new ValueWrapper<int>(0);

            mruCache.Add(key, added);

            object res;
            Assert.IsTrue(mruCache.TryGetValue(new ValueWrapper<int>(0), out res));
            Assert.AreEqual(added, res);
        }
        internal static Func <object, object[], object> GetFunc(CodeActivityMetadata metadata, MethodInfo methodInfo,
                                                                MruCache <MethodInfo, Func <object, object[], object> > cache, ReaderWriterLockSlim locker, bool valueTypeReference = false)
        {
            Func <object, object[], object> func = null;

            locker.EnterWriteLock();
            try
            {
                cache.TryGetValue(methodInfo, out func);
            }
            finally
            {
                locker.ExitWriteLock();
            }
            if (func == null)
            {
                func = GetFunc(metadata, methodInfo, valueTypeReference);
                locker.EnterWriteLock();
                try
                {
                    //MruCache has on ContainsKey(), so we use TryGetValue()
                    Func <object, object[], object> result = null;
                    if (!cache.TryGetValue(methodInfo, out result))
                    {
                        cache.Add(methodInfo, func);
                    }
                    else
                    {
                        func = result;
                    }
                }
                finally
                {
                    locker.ExitWriteLock();
                }
            }
            return(func);
        }
示例#16
0
        public void OverflowVersionCacheAndLookupTest()
        {
            string value;
            MruCache <int, string> mruCache = new MruCache <int, string>(100);

            for (int i = 0; i < 200; ++i)
            {
                mruCache.TryAddValue(i, i.ToString());
                Assert.True(mruCache.TryGetValue(i, out value));    // No longer a virgin
                Assert.Equal(i.ToString(), value);
            }

            for (int i = 0; i < 90; ++i)
            {
                Assert.False(mruCache.TryGetValue(i, out value));
            }

            for (int i = 140; i < 200; ++i)
            {
                Assert.True(mruCache.TryGetValue(i, out value));
                Assert.Equal(i.ToString(), value);
            }
        }
示例#17
0
        public void when_TryGetValue_called_with_nonexistent_key__should_return_false()
        {
            var mruCache = new MruCache <ValueWrapper <int>, object>(12);

            object added = new object();
            var    key   = new ValueWrapper <int>(0);

            mruCache.Add(key, added);

            object res;

            Assert.IsFalse(mruCache.TryGetValue(new ValueWrapper <int>(1), out res));
            Assert.IsNull(res);
        }
示例#18
0
        public void when_TryGetValue_called_with_existing_key__should_return_what_it_was_provided()
        {
            var mruCache = new MruCache <ValueWrapper <int>, object>(12);

            object added = new object();
            var    key   = new ValueWrapper <int>(0);

            mruCache.Add(key, added);

            object res;

            Assert.IsTrue(mruCache.TryGetValue(new ValueWrapper <int>(0), out res));
            Assert.AreEqual(added, res);
        }
示例#19
0
        /// <summary>
        /// Get properties, cached for a type
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]> GetProps(object value)
        {
            var type = value.GetType();
            KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]> props;

            if (_propsCache.TryGetValue(type, out props))
            {
                if (props.Key.Length != 0 && props.Value.Length == 0)
                {
                    var lateBoundMethods = new ReflectionHelpers.LateBoundMethod[props.Key.Length];
                    for (int i = 0; i < props.Key.Length; i++)
                    {
                        lateBoundMethods[i] = ReflectionHelpers.CreateLateBoundMethod(props.Key[i].GetGetMethod());
                    }
                    props = new KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]>(props.Key, lateBoundMethods);
                    _propsCache.TryAddValue(type, props);
                }
                return(props);
            }

            PropertyInfo[] properties = null;

            try
            {
                properties = type.GetProperties(PublicProperties);
            }
            catch (Exception ex)
            {
                Common.InternalLogger.Warn(ex, "Failed to get JSON properties for type: {0}", type);
            }
            finally
            {
                if (properties == null)
                {
                    properties = ArrayHelper.Empty <PropertyInfo>();
                }
            }

            props = new KeyValuePair <PropertyInfo[], ReflectionHelpers.LateBoundMethod[]>(properties, ArrayHelper.Empty <ReflectionHelpers.LateBoundMethod>());
            _propsCache.TryAddValue(type, props);
            return(props);
        }
示例#20
0
        public void TryGetValue_renews_lifespan_of_entry()
        {
            var cache = new MruCache <int, string>(capacity: 3);

            cache.Add(1, "one");
            cache.Add(2, "two");
            cache.Add(3, "three");

            Assert.True(cache.TryGetValue(3, out _));
            Assert.True(cache.TryGetValue(2, out _));
            Assert.True(cache.TryGetValue(1, out _));

            cache.Add(4, "four");

            Assert.True(cache.TryGetValue(1, out _));
            Assert.True(cache.TryGetValue(2, out _));
            Assert.False(cache.TryGetValue(3, out _));
            Assert.True(cache.TryGetValue(4, out _));
        }
示例#21
0
        public void Add_expires_last_used_entry_when_at_capacity()
        {
            var cache = new MruCache <int, string>(capacity: 3);

            cache.Add(1, "one");
            cache.Add(2, "two");
            cache.Add(3, "three");

            Assert.True(cache.TryGetValue(1, out _));
            Assert.True(cache.TryGetValue(2, out _));
            Assert.True(cache.TryGetValue(3, out _));

            cache.Add(4, "four");

            Assert.False(cache.TryGetValue(1, out _));
            Assert.True(cache.TryGetValue(2, out _));
            Assert.True(cache.TryGetValue(3, out _));
            Assert.True(cache.TryGetValue(4, out _));
        }