コード例 #1
0
        public void CacheKeyConstructorEmptyTagTest()
        {
            var cacheTag = new CacheTag(string.Empty);

            cacheTag.Should().NotBeNull();
            cacheTag.ToString().Should().BeEmpty();
        }
コード例 #2
0
        /// <summary>
        /// Expires the specified cache tag.
        /// </summary>
        /// <param name="cacheTag">The cache tag.</param>
        /// <returns></returns>
        public virtual int Expire(CacheTag cacheTag)
        {
            var provider = ResolveProvider();
            var item     = provider.Expire(cacheTag);

            return(item);
        }
コード例 #3
0
 public void CacheTagConstructorTest()
 {
     string tag = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture); 
     var cacheTag = new CacheTag(tag);
     cacheTag.Should().NotBeNull();
     cacheTag.ToString().Should().Be(tag);
 }
コード例 #4
0
ファイル: CacheTagTest.cs プロジェクト: jackyzhou/ZSharp
        public void cache_tag_constructor_empty_test()
        {
            var cacheTag = new CacheTag(string.Empty);

            cacheTag.Should().NotBeNull();
            cacheTag.ToString().Should().BeEmpty();
        }
コード例 #5
0
        //[Fact]
        public void ExpireTest()
        {
            var cacheManager = new CacheManager();
            string key = "ExpireTest" + DateTime.Now.Ticks;
            var tags = new[] { "a", "b" };
            var cacheKey = new CacheKey(key, tags);
            var value = "Test Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();

            bool result = cacheManager.Add(cacheKey, value, cachePolicy);
            result.Should().BeTrue();

            // add second value with same tag
            string key2 = "ExpireTest2" + DateTime.Now.Ticks;
            var tags2 = new[] { "a", "c" };
            var cacheKey2 = new CacheKey(key2, tags2);
            var value2 = "Test Value 2 " + DateTime.Now;
            var cachePolicy2 = new CachePolicy();

            bool result2 = cacheManager.Add(cacheKey2, value2, cachePolicy2);
            result2.Should().BeTrue();

            // add third value with same tag
            string key3 = "ExpireTest3" + DateTime.Now.Ticks;
            var tags3 = new[] { "b", "c" };
            var cacheKey3 = new CacheKey(key3, tags3);
            var value3 = "Test Value 3 " + DateTime.Now;
            var cachePolicy3 = new CachePolicy();

            bool result3 = cacheManager.Add(cacheKey3, value3, cachePolicy3);
            result3.Should().BeTrue();


            var cacheTag = new CacheTag("a");
            string tagKey = MemoryCacheProvider.GetTagKey(cacheTag);
            tagKey.Should().NotBeNullOrEmpty();

            var cachedTag = cacheManager.Get<object>(tagKey);
            cachedTag.Should().NotBeNull();

            // expire actually just changes the value for tag key
            cacheManager.Expire(cacheTag);

            // allow flush
            System.Threading.Thread.Sleep(500);

            var expiredTag = cacheManager.Get<object>(tagKey);
            expiredTag.Should().NotBeNull();
            expiredTag.Should().NotBe(cachedTag);

            // items should have been removed
            var expiredValue = cacheManager.Get<string>(cacheKey.Key);
            expiredValue.Should().BeNull();

            var expiredValue2 = cacheManager.Get<string>(cacheKey2.Key);
            expiredValue2.Should().BeNull();

            var expiredValue3 = cacheManager.Get<string>(cacheKey3.Key);
            expiredValue3.Should().NotBeNull();
        }
コード例 #6
0
        public void AddWithTagsTest()
        {
            var provider = new MemoryCacheProvider();
            string key = "AddWithTagsTest" + DateTime.Now.Ticks;
            string[] tags = new[] { "a", "b" };
            var cacheKey = new CacheKey(key, tags);
            var value = "Test Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();

            bool result = provider.Add(cacheKey, value, cachePolicy);
            result.Should().BeTrue();

            // look in underlying MemoryCache
            string innerKey = MemoryCacheProvider.GetKey(cacheKey);
            var cachedValue = MemoryCache.Default.Get(innerKey);
            cachedValue.Should().NotBeNull();
            cachedValue.Should().Be(value);

            // make sure cache key is in underlying MemoryCache
            var cacheTag = new CacheTag("a");
            string tagKey = MemoryCacheProvider.GetTagKey(cacheTag);
            tagKey.Should().NotBeNullOrEmpty();

            var cachedTag = MemoryCache.Default.Get(tagKey);
            cachedTag.Should().NotBeNull();
        }
コード例 #7
0
        public void AddWithExistingTagTest()
        {
            var provider = new MemoryCacheProvider();
            string key = "AddWithExistingTagTest" + DateTime.Now.Ticks;
            string[] tags = new[] { "a", "b" };
            var cacheKey = new CacheKey(key, tags);
            var value = "Test Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();

            bool result = provider.Add(cacheKey, value, cachePolicy);
            result.Should().BeTrue();

            // make sure cache key is in underlying MemoryCache
            var cacheTag = new CacheTag("a");
            string tagKey = MemoryCacheProvider.GetTagKey(cacheTag);
            tagKey.Should().NotBeNullOrEmpty();

            var cachedTag = MemoryCache.Default.Get(tagKey);
            cachedTag.Should().NotBeNull();

            // add second value with same tag
            string key2 = "AddWithExistingTagTest2" + DateTime.Now.Ticks;
            string[] tags2 = new[] { "a", "c" };
            var cacheKey2 = new CacheKey(key2, tags2);
            var value2 = "Test Value 2 " + DateTime.Now;
            var cachePolicy2 = new CachePolicy();

            bool result2 = provider.Add(cacheKey2, value2, cachePolicy2);
            result2.Should().BeTrue();

            // tag 'a' should have same value
            var cachedTag2 = MemoryCache.Default.Get(tagKey);
            cachedTag2.Should().NotBeNull();
            cachedTag2.Should().Be(cachedTag);
        }
コード例 #8
0
        public void AddWithTagsTest()
        {
            var    provider = new MemoryCacheProvider();
            string key      = "AddTest" + DateTime.Now.Ticks;

            string[] tags        = new[] { "a", "b" };
            var      cacheKey    = new CacheKey(key, tags);
            var      value       = "Test Value " + DateTime.Now;
            var      cachePolicy = new CachePolicy();

            bool result = provider.Add(cacheKey, value, cachePolicy);

            result.Should().BeTrue();

            // look in underlying MemoryCache
            string innerKey    = MemoryCacheProvider.GetKey(cacheKey);
            var    cachedValue = MemoryCache.Default.Get(innerKey);

            cachedValue.Should().NotBeNull();
            cachedValue.Should().Be(value);

            // make sure cache key is in underlying MemoryCache
            var    cacheTag = new CacheTag("a");
            string tagKey   = MemoryCacheProvider.GetTagKey(cacheTag);

            tagKey.Should().NotBeNullOrEmpty();

            var cachedTag = MemoryCache.Default.Get(tagKey);

            cachedTag.Should().NotBeNull();
        }
コード例 #9
0
ファイル: CacheTagTest.cs プロジェクト: jackyzhou/ZSharp
        public void cache_tag_constructor_test()
        {
            string tag      = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
            var    cacheTag = new CacheTag(tag);

            cacheTag.Should().NotBeNull();
            cacheTag.ToString().Should().Be(tag);
        }
コード例 #10
0
        public T Get <T>(CacheTag tag, string cacheId, T defaultValue)
        {
            var fullCacheId = tag + "_" + cacheId;

            T result;

            return(Cache.TryGetValue <T>(fullCacheId, out result) ? result : defaultValue);
        }
コード例 #11
0
        public void GetHashCodeTest()
        {
            string tag = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
            int hashCode = tag.GetHashCode();

            var cacheTag = new CacheTag(tag);
            cacheTag.Should().NotBeNull();
            cacheTag.ToString().Should().Be(tag);
            cacheTag.GetHashCode().Should().Be(hashCode);
        }
コード例 #12
0
ファイル: CacheTagTest.cs プロジェクト: jackyzhou/ZSharp
        public void cache_tag_get_hash_code_test()
        {
            string tag      = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
            int    hashCode = tag.GetHashCode();

            var cacheTag = new CacheTag(tag);

            cacheTag.Should().NotBeNull();
            cacheTag.ToString().Should().Be(tag);
            cacheTag.GetHashCode().Should().Be(hashCode);
        }
コード例 #13
0
        public void ClearTagCache(CacheTag tag)
        {
            throw new NotImplementedException();
            //var alldata = GetAllCache()
            //    .Where(s => s.Key.IndexOf(tag.ToString()) == 0); //获取所有以tag开头的缓存项

            //foreach(var item in alldata)
            //{
            //    Cache.Remove(item.Key);
            //}
        }
コード例 #14
0
        private IReadOnlyDictionary <string, ICacheTag> LocalizeCacheTags(ITemplateInfo template, ILocalizationLocator localizationInfo)
        {
            if (localizationInfo == null || localizationInfo.ParameterSymbols == null)
            {
                return(template.Tags);
            }

            IReadOnlyDictionary <string, ICacheTag> templateTags = template.Tags;
            IReadOnlyDictionary <string, IParameterSymbolLocalizationModel> localizedParameterSymbols = localizationInfo.ParameterSymbols;

            Dictionary <string, ICacheTag> localizedCacheTags = new Dictionary <string, ICacheTag>();

            foreach (KeyValuePair <string, ICacheTag> templateTag in templateTags)
            {
                if (localizedParameterSymbols.TryGetValue(templateTag.Key, out IParameterSymbolLocalizationModel localizationForTag))
                {   // there is loc for this symbol
                    Dictionary <string, string> localizedChoicesAndDescriptions = new Dictionary <string, string>();

                    foreach (KeyValuePair <string, string> templateChoice in templateTag.Value.ChoicesAndDescriptions)
                    {
                        if (localizationForTag.ChoicesAndDescriptions.TryGetValue(templateChoice.Key, out string localizedDesc) && !string.IsNullOrWhiteSpace(localizedDesc))
                        {
                            localizedChoicesAndDescriptions.Add(templateChoice.Key, localizedDesc);
                        }
                        else
                        {
                            localizedChoicesAndDescriptions.Add(templateChoice.Key, templateChoice.Value);
                        }
                    }

                    string tagDescription = localizationForTag.Description ?? templateTag.Value.Description;

                    ICacheTag localizedTag;
                    if (templateTag.Value is IAllowDefaultIfOptionWithoutValue tagWithNoValueDefault)
                    {
                        localizedTag = new CacheTag(tagDescription, localizedChoicesAndDescriptions, templateTag.Value.DefaultValue, tagWithNoValueDefault.DefaultIfOptionWithoutValue);
                    }
                    else
                    {
                        localizedTag = new CacheTag(tagDescription, localizedChoicesAndDescriptions, templateTag.Value.DefaultValue);
                    }

                    localizedCacheTags.Add(templateTag.Key, localizedTag);
                }
                else
                {
                    localizedCacheTags.Add(templateTag.Key, templateTag.Value);
                }
            }

            return(localizedCacheTags);
        }
コード例 #15
0
        public T Get <T>(CacheTag tag, string cacheId, Func <T> DataFunc, int?ValidateSecs = null)
        {
            var result      = DataFunc.Invoke();
            var fullCacheId = tag + "_" + cacheId;

            if (ValidateSecs.HasValue)
            {
                return(Cache.Set <T>(fullCacheId, result, DateTime.Now.AddSeconds(ValidateSecs.Value)));
            }
            else
            {
                return(Cache.Set <T>(fullCacheId, result));
            }
        }
コード例 #16
0
ファイル: TemplateCache.cs プロジェクト: ajeckmans/templating
        private IReadOnlyDictionary <string, ICacheTag> LocalizeCacheTags(ITemplateInfo template, ILocalizationLocator localizationInfo)
        {
            if (localizationInfo == null || localizationInfo.ParameterSymbols == null)
            {
                return(template.Tags);
            }

            IReadOnlyDictionary <string, ICacheTag> templateTags = template.Tags;
            IReadOnlyDictionary <string, IParameterSymbolLocalizationModel> localizedParameterSymbols = localizationInfo.ParameterSymbols;

            Dictionary <string, ICacheTag> localizedCacheTags = new Dictionary <string, ICacheTag>();

            foreach (KeyValuePair <string, ICacheTag> templateTag in templateTags)
            {
                if (!localizedParameterSymbols.TryGetValue(templateTag.Key, out IParameterSymbolLocalizationModel localizationForTag))
                {
                    // There is no localization for this symbol. Use the symbol as is.
                    localizedCacheTags.Add(templateTag.Key, templateTag.Value);
                    continue;
                }

                // There is localization. Create a localized instance, starting with the choices.
                var localizedChoices = new Dictionary <string, ParameterChoice>();

                foreach (KeyValuePair <string, ParameterChoice> templateChoice in templateTag.Value.Choices)
                {
                    ParameterChoice localizedChoice = new ParameterChoice(
                        templateChoice.Value.DisplayName,
                        templateChoice.Value.Description);

                    if (localizationForTag.Choices.TryGetValue(templateChoice.Key, out ParameterChoiceLocalizationModel locModel))
                    {
                        localizedChoice.Localize(locModel);
                    }

                    localizedChoices.Add(templateChoice.Key, localizedChoice);
                }

                ICacheTag localizedTag = new CacheTag(
                    localizationForTag.DisplayName ?? templateTag.Value.DisplayName,
                    localizationForTag.Description ?? templateTag.Value.Description,
                    localizedChoices,
                    templateTag.Value.DefaultValue,
                    (templateTag.Value as IAllowDefaultIfOptionWithoutValue)?.DefaultIfOptionWithoutValue);

                localizedCacheTags.Add(templateTag.Key, localizedTag);
            }

            return(localizedCacheTags);
        }
コード例 #17
0
        public static TemplateInfo FromJObject(JObject entry)
        {
            TemplateInfo info = new TemplateInfo();

            info.ConfigMountPointId = Guid.Parse(entry.ToString(nameof(TemplateInfo.ConfigMountPointId)));
            info.Author             = entry.ToString(nameof(TemplateInfo.Author));
            JArray classificationsArray = entry.Get <JArray>(nameof(TemplateInfo.Classifications));

            List <string> classifications = new List <string>();

            info.Classifications = classifications;
            //using (Timing.Over("Read classifications"))
            foreach (JToken item in classificationsArray)
            {
                classifications.Add(item.ToString());
            }

            info.DefaultName   = entry.ToString(nameof(TemplateInfo.DefaultName));
            info.Description   = entry.ToString(nameof(TemplateInfo.Description));
            info.Identity      = entry.ToString(nameof(TemplateInfo.Identity));
            info.GeneratorId   = Guid.Parse(entry.ToString(nameof(TemplateInfo.GeneratorId)));
            info.GroupIdentity = entry.ToString(nameof(TemplateInfo.GroupIdentity));
            info.Name          = entry.ToString(nameof(TemplateInfo.Name));
            info.ShortName     = entry.ToString(nameof(TemplateInfo.ShortName));

            // tags are just "name": "description"
            // e.g.: "language": "C#"
            JObject tagsObject = entry.Get <JObject>(nameof(TemplateInfo.Tags));
            Dictionary <string, ICacheTag> tags = new Dictionary <string, ICacheTag>();

            info.Tags = tags;
            foreach (JProperty item in tagsObject.Properties())
            {
                Dictionary <string, ParameterChoice> choicesAndDescriptions = new Dictionary <string, ParameterChoice>(StringComparer.OrdinalIgnoreCase);
                choicesAndDescriptions.Add(item.Value.ToString(), new ParameterChoice(string.Empty, string.Empty));
                ICacheTag cacheTag = new CacheTag(
                    displayName: string.Empty,
                    description: string.Empty,
                    choicesAndDescriptions,
                    item.Value.ToString());

                tags.Add(item.Name.ToString(), cacheTag);
            }

            info.ConfigPlace = entry.ToString(nameof(TemplateInfo.ConfigPlace));
            info.LocaleConfigMountPointId = Guid.Parse(entry.ToString(nameof(TemplateInfo.LocaleConfigMountPointId)));
            info.LocaleConfigPlace        = entry.ToString(nameof(TemplateInfo.LocaleConfigPlace));

            return(info);
        }
コード例 #18
0
        public void EqualsTest()
        {
            string tag = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
            var leftTag = new CacheTag(tag);
            leftTag.Should().NotBeNull();
            leftTag.ToString().Should().Be(tag);

            var rightTag = new CacheTag(tag);
            rightTag.Should().NotBeNull();
            rightTag.ToString().Should().Be(tag);

            leftTag.Equals(rightTag).Should().BeTrue();

            (leftTag == rightTag).Should().BeTrue();
        }
コード例 #19
0
ファイル: CacheTagTest.cs プロジェクト: jackyzhou/ZSharp
        public void cache_tag_equals_test()
        {
            string tag     = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
            var    leftTag = new CacheTag(tag);

            leftTag.Should().NotBeNull();
            leftTag.ToString().Should().Be(tag);

            var rightTag = new CacheTag(tag);

            rightTag.Should().NotBeNull();
            rightTag.ToString().Should().Be(tag);

            leftTag.Equals(rightTag).Should().BeTrue();

            (leftTag == rightTag).Should().BeTrue();
        }
コード例 #20
0
        public void AddWithExistingTagTest()
        {
            var    provider = new MemoryCacheProvider();
            string key      = "AddTest" + DateTime.Now.Ticks;

            string[] tags        = new[] { "a", "b" };
            var      cacheKey    = new CacheKey(key, tags);
            var      value       = "Test Value " + DateTime.Now;
            var      cachePolicy = new CachePolicy();

            bool result = provider.Add(cacheKey, value, cachePolicy);

            result.Should().BeTrue();

            // make sure cache key is in underlying MemoryCache
            var    cacheTag = new CacheTag("a");
            string tagKey   = MemoryCacheProvider.GetTagKey(cacheTag);

            tagKey.Should().NotBeNullOrEmpty();

            var cachedTag = MemoryCache.Default.Get(tagKey);

            cachedTag.Should().NotBeNull();

            // add second value with same tag
            string key2 = "AddTest2" + DateTime.Now.Ticks;

            string[] tags2        = new[] { "a", "c" };
            var      cacheKey2    = new CacheKey(key2, tags2);
            var      value2       = "Test Value 2 " + DateTime.Now;
            var      cachePolicy2 = new CachePolicy();

            bool result2 = provider.Add(cacheKey2, value2, cachePolicy2);

            result2.Should().BeTrue();

            // tag 'a' should have same value
            var cachedTag2 = MemoryCache.Default.Get(tagKey);

            cachedTag2.Should().NotBeNull();
            cachedTag2.Should().Be(cachedTag);
        }
コード例 #21
0
        protected override ICacheTag ReadOneTag(JProperty item)
        {
            Dictionary <string, ParameterChoice> choices = new Dictionary <string, ParameterChoice>(StringComparer.OrdinalIgnoreCase);

            foreach (JProperty choiceObject in item.Value.PropertiesOf("Choices"))
            {
                choices.Add(choiceObject.Name, new ParameterChoice(
                                choiceObject.Value.ToString("DisplayName"),
                                choiceObject.Value.ToString("Description")));
            }

            CacheTag tag = new CacheTag(
                displayName: item.Value.ToString("DisplayName"),
                description: item.Value.ToString("Description"),
                choices,
                item.Value.ToString("DefaultValue"));

            tag.DefaultIfOptionWithoutValue = item.Value.ToString("DefaultIfOptionWithoutValue");
            return(tag);
        }
コード例 #22
0
        public virtual void Invalidate(IEnumerable <string> tags)
        {
            Precondition.Require(tags, () => Error.ArgumentNull("tags"));
            Precondition.Require(!_disposed, () => Error.ObjectDisposed("entries"));

            _tagsLock.EnterWriteLock();

            try
            {
                DateTime time = _dateAccessor();
                foreach (string value in tags)
                {
                    _tags[value] = new CacheTag(value, time.ToFileTimeUtc());
                }
            }
            finally
            {
                _tagsLock.ExitWriteLock();
            }
        }
コード例 #23
0
        public void CreateChangeMonitorTest()
        {
            string key = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);

            string[] tags     = new[] { "a", "b" };
            var      cacheKey = new CacheKey(key, tags);

            cacheKey.Should().NotBeNull();

            var monitor = MemoryCacheProvider.CreateChangeMonitor(cacheKey);

            monitor.Should().NotBeNull();
            monitor.CacheKeys.Should().HaveCount(2);

            var    cacheTag = new CacheTag("a");
            string tagKey   = MemoryCacheProvider.GetTagKey(cacheTag);

            tagKey.Should().NotBeNullOrEmpty();

            monitor.CacheKeys.Should().Contain(tagKey);
        }
コード例 #24
0
        private IEnumerable <CacheTag> CreateEntryTags(IEnumerable <string> values)
        {
            List <CacheTag> tags = new List <CacheTag>();

            if (values == null)
            {
                return(tags);
            }

            _tagsLock.EnterUpgradeableReadLock();
            try
            {
                DateTime time = _dateAccessor();
                foreach (string value in values)
                {
                    CacheTag tag;
                    if (!_tags.TryGetValue(value, out tag))
                    {
                        tag         = new CacheTag(value);
                        tag.Version = time.ToFileTimeUtc();

                        _tagsLock.EnterWriteLock();
                        try
                        {
                            _tags.Add(value, tag);
                        }
                        finally
                        {
                            _tagsLock.ExitWriteLock();
                        }
                    }
                    tags.Add(tag);
                }
            }
            finally
            {
                _tagsLock.ExitUpgradeableReadLock();
            }
            return(tags);
        }
コード例 #25
0
 /// <summary>
 /// Expires the specified cache tag.
 /// </summary>
 /// <param name="tag">The cache tag.</param>
 /// <returns></returns>
 public int Expire(string tag)
 {
     var cacheTag = new CacheTag(tag);
     return Expire(cacheTag);
 }
コード例 #26
0
        public void ClearTheCache(CacheTag tag, string cacheId)
        {
            var fullCacheId = tag + "_" + cacheId;

            Cache.Remove(fullCacheId);
        }
コード例 #27
0
        public void CreateChangeMonitorTest()
        {
            string key = DateTime.Now.Ticks.ToString(CultureInfo.InvariantCulture);
            string[] tags = new[] { "a", "b" };
            var cacheKey = new CacheKey(key, tags);
            cacheKey.Should().NotBeNull();

            var monitor = MemoryCacheProvider.CreateChangeMonitor(cacheKey);
            monitor.Should().NotBeNull();
            monitor.CacheKeys.Should().HaveCount(2);

            var cacheTag = new CacheTag("a");
            string tagKey = MemoryCacheProvider.GetTagKey(cacheTag);
            tagKey.Should().NotBeNullOrEmpty();

            monitor.CacheKeys.Should().Contain(tagKey);
        }
コード例 #28
0
        public void ExpireTest()
        {
            var cache = MemoryCache.Default;
            // purge all values
            foreach (KeyValuePair<string, object> pair in cache)
                cache.Remove(pair.Key);

            var provider = new MemoryCacheProvider();
            string key = "ExpireTest" + DateTime.Now.Ticks;
            var tags = new[] { "a", "b" };
            var cacheKey = new CacheKey(key, tags);
            var value = "Test Value " + DateTime.Now;
            var cachePolicy = new CachePolicy();

            bool result = provider.Add(cacheKey, value, cachePolicy);
            result.Should().BeTrue();

            // add second value with same tag
            string key2 = "ExpireTest2" + DateTime.Now.Ticks;
            var tags2 = new[] { "a", "c" };
            var cacheKey2 = new CacheKey(key2, tags2);
            var value2 = "Test Value 2 " + DateTime.Now;
            var cachePolicy2 = new CachePolicy();

            bool result2 = provider.Add(cacheKey2, value2, cachePolicy2);
            result2.Should().BeTrue();

            // add third value with same tag
            string key3 = "ExpireTest3" + DateTime.Now.Ticks;
            var tags3 = new[] { "b", "c" };
            var cacheKey3 = new CacheKey(key3, tags3);
            var value3 = "Test Value 3 " + DateTime.Now;
            var cachePolicy3 = new CachePolicy();

            bool result3 = provider.Add(cacheKey3, value3, cachePolicy3);
            result3.Should().BeTrue();


            var cacheTag = new CacheTag("a");
            string tagKey = MemoryCacheProvider.GetTagKey(cacheTag);
            tagKey.Should().NotBeNullOrEmpty();

            // underlying cache
            cache.GetCount().Should().Be(6);

            var cachedTag = cache.Get(tagKey);
            cachedTag.Should().NotBeNull();

            System.Threading.Thread.Sleep(500);

            // expire actually just changes the value for tag key
            provider.Expire(cacheTag);

            var expiredTag = cache.Get(tagKey);
            expiredTag.Should().NotBeNull();
            expiredTag.Should().NotBe(cachedTag);

            // items should have been removed
            var expiredValue = provider.Get<string>(cacheKey);
            expiredValue.Should().BeNull();

            var expiredValue2 = provider.Get<string>(cacheKey2);
            expiredValue2.Should().BeNull();

            var expiredValue3 = provider.Get<string>(cacheKey3);
            expiredValue3.Should().NotBeNull();

            cache.GetCount().Should().Be(4);
        }
コード例 #29
0
        public void AddTag(string key, string value)
        {
            var cacheTag = new CacheTag("", new Dictionary <string, string>(), value);

            _tags.Add(key, cacheTag);
        }
コード例 #30
0
        public static TemplateInfo FromJObject(JObject entry)
        {
            TemplateInfo info = new TemplateInfo();

            info.ConfigMountPointId = Guid.Parse(entry.ToString(nameof(TemplateInfo.ConfigMountPointId)));
            info.Author             = entry.ToString(nameof(TemplateInfo.Author));
            JArray classificationsArray = entry.Get <JArray>(nameof(TemplateInfo.Classifications));

            List <string> classifications = new List <string>();

            info.Classifications = classifications;
            //using (Timing.Over("Read classifications"))
            foreach (JToken item in classificationsArray)
            {
                classifications.Add(item.ToString());
            }

            info.DefaultName   = entry.ToString(nameof(TemplateInfo.DefaultName));
            info.Description   = entry.ToString(nameof(TemplateInfo.Description));
            info.Identity      = entry.ToString(nameof(TemplateInfo.Identity));
            info.GeneratorId   = Guid.Parse(entry.ToString(nameof(TemplateInfo.GeneratorId)));
            info.GroupIdentity = entry.ToString(nameof(TemplateInfo.GroupIdentity));
            info.Precedence    = entry.ToInt32(nameof(TemplateInfo.Precedence));
            info.Name          = entry.ToString(nameof(TemplateInfo.Name));
            info.ShortName     = entry.ToString(nameof(TemplateInfo.ShortName));

            // parse the cached tags
            Dictionary <string, ICacheTag> tags = new Dictionary <string, ICacheTag>();
            JObject tagsObject = entry.Get <JObject>(nameof(TemplateInfo.Tags));

            if (tagsObject != null)
            {
                foreach (JProperty item in tagsObject.Properties())
                {
                    Dictionary <string, string> choicesAndDescriptions = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
                    JObject cdToken = item.Value.Get <JObject>(nameof(ICacheTag.ChoicesAndDescriptions));
                    foreach (JProperty cdPair in cdToken.Properties())
                    {
                        choicesAndDescriptions.Add(cdPair.Name.ToString(), cdPair.Value.ToString());
                    }

                    ICacheTag cacheTag = new CacheTag(
                        item.Value.ToString(nameof(ICacheTag.Description)),
                        choicesAndDescriptions,
                        item.Value.ToString(nameof(ICacheTag.DefaultValue)));
                    tags.Add(item.Name.ToString(), cacheTag);
                }
            }
            info.Tags = tags;

            // parse the cached params
            JObject cacheParamsObject = entry.Get <JObject>(nameof(TemplateInfo.CacheParameters));
            Dictionary <string, ICacheParameter> cacheParams = new Dictionary <string, ICacheParameter>();

            if (cacheParamsObject != null)
            {
                foreach (JProperty item in cacheParamsObject.Properties())
                {
                    ICacheParameter param = new CacheParameter
                    {
                        DataType     = item.Value.ToString(nameof(ICacheParameter.DataType)),
                        DefaultValue = item.Value.ToString(nameof(ICacheParameter.DefaultValue)),
                        Description  = item.Value.ToString(nameof(ICacheParameter.Description))
                    };

                    cacheParams[item.Name.ToString()] = param;
                }
            }
            info.CacheParameters = cacheParams;

            info.ConfigPlace = entry.ToString(nameof(TemplateInfo.ConfigPlace));
            info.LocaleConfigMountPointId = Guid.Parse(entry.ToString(nameof(TemplateInfo.LocaleConfigMountPointId)));
            info.LocaleConfigPlace        = entry.ToString(nameof(TemplateInfo.LocaleConfigPlace));

            info.HostConfigMountPointId = Guid.Parse(entry.ToString(nameof(TemplateInfo.HostConfigMountPointId)));
            info.HostConfigPlace        = entry.ToString(nameof(TemplateInfo.HostConfigPlace));

            return(info);
        }
コード例 #31
0
ファイル: TemplateInfo.cs プロジェクト: karajas/templating
        public TemplateInfo(JObject entry)
        {
            ConfigMountPointId = Guid.Parse(entry.ToString(nameof(ConfigMountPointId)));
            Author             = entry.ToString(nameof(Author));
            JArray classificationsArray = entry.Get <JArray>(nameof(Classifications));

            List <string> classifications = new List <string>();

            Classifications = classifications;
            //using (Timing.Over("Read classifications"))
            foreach (JToken item in classificationsArray)
            {
                classifications.Add(item.ToString());
            }

            DefaultName   = entry.ToString(nameof(DefaultName));
            Description   = entry.ToString(nameof(Description));
            Identity      = entry.ToString(nameof(Identity));
            GeneratorId   = Guid.Parse(entry.ToString(nameof(GeneratorId)));
            GroupIdentity = entry.ToString(nameof(GroupIdentity));
            Name          = entry.ToString(nameof(Name));
            ShortName     = entry.ToString(nameof(ShortName));

            // parse the cached tags
            Dictionary <string, ICacheTag> tags = new Dictionary <string, ICacheTag>();
            JObject tagsObject = entry.Get <JObject>(nameof(Tags));

            foreach (JProperty item in tagsObject.Properties())
            {
                Dictionary <string, string> choicesAndDescriptions = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
                JObject cdToken = item.Value.Get <JObject>(nameof(ICacheTag.ChoicesAndDescriptions));
                foreach (JProperty cdPair in cdToken.Properties())
                {
                    choicesAndDescriptions.Add(cdPair.Name.ToString(), cdPair.Value.ToString());
                }

                ICacheTag cacheTag = new CacheTag(
                    item.Value.ToString(nameof(ICacheTag.Description)),
                    choicesAndDescriptions,
                    item.Value.ToString(nameof(ICacheTag.DefaultValue)));
                tags.Add(item.Name.ToString(), cacheTag);
            }
            Tags = tags;

            // parse the cached params
            JObject cacheParamsObject = entry.Get <JObject>(nameof(CacheParameters));
            Dictionary <string, ICacheParameter> cacheParams = new Dictionary <string, ICacheParameter>();

            foreach (JProperty item in cacheParamsObject.Properties())
            {
                ICacheParameter param = new CacheParameter
                {
                    DataType     = item.Value.ToString(nameof(ICacheParameter.DataType)),
                    DefaultValue = item.Value.ToString(nameof(ICacheParameter.DefaultValue)),
                    Description  = item.Value.ToString(nameof(ICacheParameter.Description))
                };

                cacheParams[item.Name.ToString()] = param;
            }
            CacheParameters = cacheParams;

            ConfigPlace = entry.ToString(nameof(ConfigPlace));
            LocaleConfigMountPointId = Guid.Parse(entry.ToString(nameof(LocaleConfigMountPointId)));
            LocaleConfigPlace        = entry.ToString(nameof(LocaleConfigPlace));

            HostConfigMountPointId = Guid.Parse(entry.ToString(nameof(HostConfigMountPointId)));
            HostConfigPlace        = entry.ToString(nameof(HostConfigPlace));
        }
コード例 #32
0
        public void ExpireTest()
        {
            var cache = MemoryCache.Default;

            // purge all values
            foreach (KeyValuePair <string, object> pair in cache)
            {
                cache.Remove(pair.Key);
            }

            var    provider    = new MemoryCacheProvider();
            string key         = "AddTest" + DateTime.Now.Ticks;
            var    tags        = new[] { "a", "b" };
            var    cacheKey    = new CacheKey(key, tags);
            var    value       = "Test Value " + DateTime.Now;
            var    cachePolicy = new CachePolicy();

            bool result = provider.Add(cacheKey, value, cachePolicy);

            result.Should().BeTrue();

            // add second value with same tag
            string key2         = "AddTest2" + DateTime.Now.Ticks;
            var    tags2        = new[] { "a", "c" };
            var    cacheKey2    = new CacheKey(key2, tags2);
            var    value2       = "Test Value 2 " + DateTime.Now;
            var    cachePolicy2 = new CachePolicy();

            bool result2 = provider.Add(cacheKey2, value2, cachePolicy2);

            result2.Should().BeTrue();

            // add third value with same tag
            string key3         = "AddTest3" + DateTime.Now.Ticks;
            var    tags3        = new[] { "b", "c" };
            var    cacheKey3    = new CacheKey(key3, tags3);
            var    value3       = "Test Value 3 " + DateTime.Now;
            var    cachePolicy3 = new CachePolicy();

            bool result3 = provider.Add(cacheKey3, value3, cachePolicy3);

            result3.Should().BeTrue();


            var    cacheTag = new CacheTag("a");
            string tagKey   = MemoryCacheProvider.GetTagKey(cacheTag);

            tagKey.Should().NotBeNullOrEmpty();

            // underlying cache
            cache.GetCount().Should().Be(6);

            var cachedTag = cache.Get(tagKey);

            cachedTag.Should().NotBeNull();

            // expire actually just changes the value for tag key
            provider.Expire(cacheTag);

            var expiredTag = cache.Get(tagKey);

            expiredTag.Should().NotBeNull();
            expiredTag.Should().NotBe(cachedTag);

            // items should have been removed
            var expiredValue = provider.Get(cacheKey);

            expiredValue.Should().BeNull();

            var expiredValue2 = provider.Get(cacheKey2);

            expiredValue2.Should().BeNull();

            var expiredValue3 = provider.Get(cacheKey3);

            expiredValue3.Should().NotBeNull();

            cache.GetCount().Should().Be(4);
        }
コード例 #33
0
        public void ExpireTest()
        {
            var    cacheManager = new CacheManager();
            string key          = "AddTest" + DateTime.Now.Ticks;
            var    tags         = new[] { "a", "b" };
            var    cacheKey     = new CacheKey(key, tags);
            var    value        = "Test Value " + DateTime.Now;
            var    cachePolicy  = new CachePolicy();

            bool result = cacheManager.Add(cacheKey, value, cachePolicy);

            result.Should().BeTrue();

            // add second value with same tag
            string key2         = "AddTest2" + DateTime.Now.Ticks;
            var    tags2        = new[] { "a", "c" };
            var    cacheKey2    = new CacheKey(key2, tags2);
            var    value2       = "Test Value 2 " + DateTime.Now;
            var    cachePolicy2 = new CachePolicy();

            bool result2 = cacheManager.Add(cacheKey2, value2, cachePolicy2);

            result2.Should().BeTrue();

            // add third value with same tag
            string key3         = "AddTest3" + DateTime.Now.Ticks;
            var    tags3        = new[] { "b", "c" };
            var    cacheKey3    = new CacheKey(key3, tags3);
            var    value3       = "Test Value 3 " + DateTime.Now;
            var    cachePolicy3 = new CachePolicy();

            bool result3 = cacheManager.Add(cacheKey3, value3, cachePolicy3);

            result3.Should().BeTrue();


            var    cacheTag = new CacheTag("a");
            string tagKey   = MemoryCacheProvider.GetTagKey(cacheTag);

            tagKey.Should().NotBeNullOrEmpty();

            var cachedTag = cacheManager.Get(tagKey);

            cachedTag.Should().NotBeNull();

            // expire actually just changes the value for tag key
            cacheManager.Expire(cacheTag);

            var expiredTag = cacheManager.Get(tagKey);

            expiredTag.Should().NotBeNull();
            expiredTag.Should().NotBe(cachedTag);

            // items should have been removed
            var expiredValue = cacheManager.Get(cacheKey.Key);

            expiredValue.Should().BeNull();

            var expiredValue2 = cacheManager.Get(cacheKey2.Key);

            expiredValue2.Should().BeNull();

            var expiredValue3 = cacheManager.Get(cacheKey3.Key);

            expiredValue3.Should().NotBeNull();
        }
コード例 #34
0
 internal static string GetTagKey(CacheTag t)
 {
     return string.Format(_tagKey, t);
 }
コード例 #35
0
        /// <summary>
        /// Expires the specified cache tag.
        /// </summary>
        /// <param name="cacheTag">The cache tag.</param>
        /// <returns>
        /// The number of items expired.
        /// </returns>
        public int Expire(CacheTag cacheTag)
        {
            string key = GetTagKey(cacheTag);
            var item = new CacheItem(key, DateTimeOffset.UtcNow.Ticks);
            var policy = new CacheItemPolicy { AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration };

            MemoryCache.Default.Set(item, policy);
            return 0;
        }
コード例 #36
0
        /// <summary>
        /// Expires the specified cache tag.
        /// </summary>
        /// <param name="tag">The cache tag.</param>
        /// <returns></returns>
        public int Expire(string tag)
        {
            var cacheTag = new CacheTag(tag);

            return(Expire(cacheTag));
        }
コード例 #37
0
 public void CacheKeyConstructorEmptyTagTest()
 {
     var cacheTag = new CacheTag(string.Empty);
     cacheTag.Should().NotBeNull();
     cacheTag.ToString().Should().BeEmpty();
 }
コード例 #38
0
        /// <summary>
        /// Expires the specified cache tag.
        /// </summary>
        /// <param name="cacheTag">The cache tag.</param>
        /// <returns></returns>
        public virtual int Expire(CacheTag cacheTag)
        {
            var provider = ResolveProvider();
            var item = provider.Expire(cacheTag);

            return item;
        }
コード例 #39
0
ファイル: QueryCacheProvider.cs プロジェクト: pomma89/KVLite
 /// <summary>
 ///   Expires the specified cache tag.
 /// </summary>
 /// <param name="cacheTag">The cache tag.</param>
 /// <returns>The number of items expired.</returns>
 public int Expire(CacheTag cacheTag)
 {
     Cache.Remove(EfCachePartition, cacheTag.ToString());
     return 0; // KVLite does not expose that information.
 }
コード例 #40
0
 /// <summary>
 ///   Expires the specified cache tag.
 /// </summary>
 /// <param name="cacheTag">The cache tag.</param>
 /// <returns>The number of items expired.</returns>
 public int Expire(CacheTag cacheTag)
 {
     Cache.Remove(EfCachePartition, cacheTag.ToString());
     return(0); // KVLite does not expose that information.
 }
コード例 #41
0
ファイル: BaseCacheTest.cs プロジェクト: jackyzhou/ZSharp
        public virtual void cache_expire_test()
        {
            string key         = "ExpireTest" + DateTime.Now.Ticks;
            var    tags        = new[] { "a", "b" };
            var    cacheKey    = new CacheKey(key, tags);
            var    value       = "Test Value " + DateTime.Now;
            var    cachePolicy = new CachePolicy();

            var result = CacheManager.Get <string>(cacheKey,
                                                   () =>
            {
                return(value);
            },
                                                   cachePolicy);

            result.Should().Be(value);

            // add second value with same tag
            string key2         = "ExpireTest2" + DateTime.Now.Ticks;
            var    tags2        = new[] { "a", "c" };
            var    cacheKey2    = new CacheKey(key2, tags2);
            var    value2       = "Test Value 2 " + DateTime.Now;
            var    cachePolicy2 = new CachePolicy();

            var result2 = CacheManager.Get <string>(cacheKey2,
                                                    () =>
            {
                return(value2);
            },
                                                    cachePolicy2);

            result2.Should().Be(value2);

            // add third value with same tag
            string key3         = "ExpireTest3" + DateTime.Now.Ticks;
            var    tags3        = new[] { "b", "c" };
            var    cacheKey3    = new CacheKey(key3, tags3);
            var    value3       = "Test Value 3 " + DateTime.Now;
            var    cachePolicy3 = new CachePolicy();

            var result3 = CacheManager.Get <string>(cacheKey3,
                                                    () =>
            {
                return(value3);
            },
                                                    cachePolicy3);

            result3.Should().Be(value3);


            var    cacheTag = new CacheTag("a");
            string tagKey   = new StaticCache().GetTagKey(cacheTag);

            tagKey.Should().NotBeNullOrEmpty();

            var cachedTag = CacheManager.Get <object>(tagKey, () => { return(null); });

            if (CacheManager.CacheType == CacheType.Web ||
                CacheManager.CacheType == CacheType.ConcurrentDictionary)
            {
                cachedTag.Should().BeNull();
            }
            else
            {
                cachedTag.Should().NotBeNull();
            }
            // expire actually just changes the value for tag key
            CacheManager.Expire(cacheTag);

            // allow flush
            System.Threading.Thread.Sleep(500);

            var expiredTag = CacheManager.Get <object>(tagKey, () => { return(null); });

            expiredTag.Should().BeNull();

            // items should have been removed
            var expiredValue = CacheManager.Get <string>(cacheKey.Key, () => { return(""); });

            expiredValue.Should().BeEmpty();
            var expiredValue2 = CacheManager.Get <string>(cacheKey2.Key, () => { return(""); });

            expiredValue2.Should().BeEmpty();

            var expiredValue3 = CacheManager.Get <string>(cacheKey3.Key, () => { return(""); });

            expiredValue3.Should().NotBeNull();
        }