예제 #1
0
        public void TestGetItem_ShouldCallHttpContextProviderCurrentCache(string key)
        {
            // Arrange
            var cache = HttpRuntime.Cache;

            var mockedHttpContextProvider = new Mock <IHttpContextProvider>();

            mockedHttpContextProvider.Setup(p => p.ContextCache).Returns(cache);

            var provider = new CachingProvider(mockedHttpContextProvider.Object);

            // Act
            provider.GetItem(key);

            // Assert
            mockedHttpContextProvider.Verify(p => p.ContextCache, Times.Once);
        }
예제 #2
0
        public void TestGetItem_ShouldReturnCorrectly(string key)
        {
            // Arrange
            var obj = new object();

            var cache = HttpRuntime.Cache;

            cache[key] = obj;

            var mockedHttpContextProvider = new Mock <IHttpContextProvider>();

            mockedHttpContextProvider.Setup(p => p.ContextCache).Returns(cache);

            var provider = new CachingProvider(mockedHttpContextProvider.Object);

            // Act
            var result = provider.GetItem(key);

            // Assert
            Assert.AreSame(obj, result);
        }
예제 #3
0
        /// <summary>
        /// Creates and caches a Regex object for later use and retrieves it in a later call if it is cacehd
        /// </summary>
        public static Regex GetCachedRegex(string pattern, RegexOptions options = RegexOptions.None, int timeoutSeconds = 2)
        {
            Requires.NotNull("pattern", pattern);
            string key = pattern;

            if ((options & RegexOptions.IgnoreCase) != 0)
            {
                key = (options & RegexOptions.CultureInvariant) != 0
                    ? pattern.ToUpperInvariant()
                    : pattern.ToUpper();
            }

            // // should not allow for compiled dynamic regex object
            options &= ~RegexOptions.Compiled;
            key      = string.Join(":", "REGEX_ITEM", options.ToString("X"), key.GetHashCode().ToString("X"));

            // limit timeout between 1 and 10 seconds
            if (timeoutSeconds < 1)
            {
                timeoutSeconds = 1;
            }
            else if (timeoutSeconds > 10)
            {
                timeoutSeconds = 10;
            }

            CachingProvider cache = CachingProvider.Instance();
            Regex           regex = cache.GetItem(key) as Regex;

            if (regex == null)
            {
                regex = new Regex(pattern, options & ~RegexOptions.Compiled, TimeSpan.FromSeconds(timeoutSeconds));
                cache.Insert(key, regex, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10), CacheItemPriority.BelowNormal, null);
            }

            return(regex);
        }