Пример #1
0
        public static T GetCacheItem <T>(this IAppPolicyCache provider,
                                         string cacheKey,
                                         Func <T> getCacheItem,
                                         TimeSpan?timeout,
                                         bool isSliding          = false,
                                         string[] dependentFiles = null)
        {
            var result = provider.Get(cacheKey, () => getCacheItem(), timeout, isSliding, dependentFiles);

            return(result == null ? default(T) : result.TryConvertTo <T>().Result);
        }
Пример #2
0
        public static T GetCacheItem <T>(this IAppPolicyCache provider,
                                         string cacheKey,
                                         Func <T> getCacheItem,
                                         TimeSpan?timeout,
                                         bool isSliding             = false,
                                         CacheItemPriority priority = CacheItemPriority.Normal,
                                         CacheItemRemovedCallback removedCallback = null,
                                         string[] dependentFiles = null)
        {
            var result = provider.Get(cacheKey, () => getCacheItem(), timeout, isSliding, priority, removedCallback, dependentFiles);

            return(result == null ? default(T) : result.TryConvertTo <T>().Result);
        }
        /// <summary>
        /// Check if the index has been rebuilt
        /// </summary>
        /// <param name="indexName"></param>
        /// <returns></returns>
        /// <remarks>
        /// This is kind of rudimentary since there's no way we can know that the index has rebuilt, we
        /// have a listener for the index op complete so we'll just check if that key is no longer there in the runtime cache
        /// </remarks>
        public ActionResult <ExamineIndexModel> PostCheckRebuildIndex(string indexName)
        {
            var validate = ValidateIndex(indexName, out var index);

            if (!validate.IsSuccessStatusCode())
            {
                return(validate);
            }

            validate = ValidatePopulator(index);
            if (!validate.IsSuccessStatusCode())
            {
                return(validate);
            }

            var cacheKey = "temp_indexing_op_" + indexName;
            var found    = _runtimeCache.Get(cacheKey);

            //if its still there then it's not done
            return(found != null
                ? null
                : CreateModel(index));
        }
        public void DefaultRepositoryCachePolicy(bool complete)
        {
            var             scopeProvider = (ScopeProvider)ScopeProvider;
            var             service       = (UserService)UserService;
            IAppPolicyCache globalCache   = AppCaches.IsolatedCaches.GetOrCreate(typeof(IUser));
            var             user          = (IUser) new User(GlobalSettings, "name", "email", "username", "rawPassword");

            service.Save(user);

            // User has been saved so the cache has been cleared of it
            var globalCached = (IUser)globalCache.Get(GetCacheIdKey <IUser>(user.Id), () => null);

            Assert.IsNull(globalCached);
            // Get user again to load it into the cache again, this also ensure we don't modify the one that's in the cache.
            user = service.GetUserById(user.Id);

            // global cache contains the entity
            globalCached = (IUser)globalCache.Get(GetCacheIdKey <IUser>(user.Id), () => null);
            Assert.IsNotNull(globalCached);
            Assert.AreEqual(user.Id, globalCached.Id);
            Assert.AreEqual("name", globalCached.Name);

            Assert.IsNull(scopeProvider.AmbientScope);
            using (IScope scope = scopeProvider.CreateScope(repositoryCacheMode: RepositoryCacheMode.Scoped))
            {
                Assert.IsInstanceOf <Scope>(scope);
                Assert.IsNotNull(scopeProvider.AmbientScope);
                Assert.AreSame(scope, scopeProvider.AmbientScope);

                // scope has its own isolated cache
                IAppPolicyCache scopedCache = scope.IsolatedCaches.GetOrCreate(typeof(IUser));
                Assert.AreNotSame(globalCache, scopedCache);

                user.Name = "changed";
                service.Save(user);

                // scoped cache contains the "new" entity
                var scopeCached = (IUser)scopedCache.Get(GetCacheIdKey <IUser>(user.Id), () => null);
                Assert.IsNotNull(scopeCached);
                Assert.AreEqual(user.Id, scopeCached.Id);
                Assert.AreEqual("changed", scopeCached.Name);

                // global cache is unchanged
                globalCached = (IUser)globalCache.Get(GetCacheIdKey <IUser>(user.Id), () => null);
                Assert.IsNotNull(globalCached);
                Assert.AreEqual(user.Id, globalCached.Id);
                Assert.AreEqual("name", globalCached.Name);

                if (complete)
                {
                    scope.Complete();
                }
            }

            Assert.IsNull(scopeProvider.AmbientScope);

            globalCached = (IUser)globalCache.Get(GetCacheIdKey <IUser>(user.Id), () => null);
            if (complete)
            {
                // global cache has been cleared
                Assert.IsNull(globalCached);
            }
            else
            {
                // global cache has *not* been cleared
                Assert.IsNotNull(globalCached);
            }

            // get again, updated if completed
            user = service.GetUserById(user.Id);
            Assert.AreEqual(complete ? "changed" : "name", user.Name);

            // global cache contains the entity again
            globalCached = (IUser)globalCache.Get(GetCacheIdKey <IUser>(user.Id), () => null);
            Assert.IsNotNull(globalCached);
            Assert.AreEqual(user.Id, globalCached.Id);
            Assert.AreEqual(complete ? "changed" : "name", globalCached.Name);
        }
        public void SingleItemsOnlyRepositoryCachePolicy(bool complete)
        {
            var scopeProvider                = (ScopeProvider)ScopeProvider;
            ILocalizationService service     = LocalizationService;
            IAppPolicyCache      globalCache = AppCaches.IsolatedCaches.GetOrCreate(typeof(IDictionaryItem));

            var lang = (ILanguage) new Language(GlobalSettings, "fr-FR");

            service.Save(lang);

            var item = (IDictionaryItem) new DictionaryItem("item-key");

            item.Translations = new IDictionaryTranslation[]
            {
                new DictionaryTranslation(lang.Id, "item-value"),
            };
            service.Save(item);

            // Refresh the cache manually because we can't unbind
            service.GetDictionaryItemById(item.Id);
            service.GetLanguageById(lang.Id);

            // global cache contains the entity
            var globalCached = (IDictionaryItem)globalCache.Get(GetCacheIdKey <IDictionaryItem>(item.Id), () => null);

            Assert.IsNotNull(globalCached);
            Assert.AreEqual(item.Id, globalCached.Id);
            Assert.AreEqual("item-key", globalCached.ItemKey);

            Assert.IsNull(scopeProvider.AmbientScope);
            using (IScope scope = scopeProvider.CreateScope(repositoryCacheMode: RepositoryCacheMode.Scoped))
            {
                Assert.IsInstanceOf <Scope>(scope);
                Assert.IsNotNull(scopeProvider.AmbientScope);
                Assert.AreSame(scope, scopeProvider.AmbientScope);

                // scope has its own isolated cache
                IAppPolicyCache scopedCache = scope.IsolatedCaches.GetOrCreate(typeof(IDictionaryItem));
                Assert.AreNotSame(globalCache, scopedCache);

                item.ItemKey = "item-changed";
                service.Save(item);

                // scoped cache contains the "new" entity
                var scopeCached = (IDictionaryItem)scopedCache.Get(GetCacheIdKey <IDictionaryItem>(item.Id), () => null);
                Assert.IsNotNull(scopeCached);
                Assert.AreEqual(item.Id, scopeCached.Id);
                Assert.AreEqual("item-changed", scopeCached.ItemKey);

                // global cache is unchanged
                globalCached = (IDictionaryItem)globalCache.Get(GetCacheIdKey <IDictionaryItem>(item.Id), () => null);
                Assert.IsNotNull(globalCached);
                Assert.AreEqual(item.Id, globalCached.Id);
                Assert.AreEqual("item-key", globalCached.ItemKey);

                if (complete)
                {
                    scope.Complete();
                }
            }

            Assert.IsNull(scopeProvider.AmbientScope);

            globalCached = (IDictionaryItem)globalCache.Get(GetCacheIdKey <IDictionaryItem>(item.Id), () => null);
            if (complete)
            {
                // global cache has been cleared
                Assert.IsNull(globalCached);
            }
            else
            {
                // global cache has *not* been cleared
                Assert.IsNotNull(globalCached);
            }

            // get again, updated if completed
            item = service.GetDictionaryItemById(item.Id);
            Assert.AreEqual(complete ? "item-changed" : "item-key", item.ItemKey);

            // global cache contains the entity again
            globalCached = (IDictionaryItem)globalCache.Get(GetCacheIdKey <IDictionaryItem>(item.Id), () => null);
            Assert.IsNotNull(globalCached);
            Assert.AreEqual(item.Id, globalCached.Id);
            Assert.AreEqual(complete ? "item-changed" : "item-key", globalCached.ItemKey);
        }
        public void FullDataSetRepositoryCachePolicy(bool complete)
        {
            var scopeProvider                = (ScopeProvider)ScopeProvider;
            ILocalizationService service     = LocalizationService;
            IAppPolicyCache      globalCache = AppCaches.IsolatedCaches.GetOrCreate(typeof(ILanguage));

            var lang = (ILanguage) new Language(GlobalSettings, "fr-FR");

            service.Save(lang);

            // global cache has been flushed, reload
            var globalFullCached = (IEnumerable <ILanguage>)globalCache.Get(GetCacheTypeKey <ILanguage>(), () => null);

            Assert.IsNull(globalFullCached);
            ILanguage reload = service.GetLanguageById(lang.Id);

            // global cache contains the entity
            globalFullCached = (IEnumerable <ILanguage>)globalCache.Get(GetCacheTypeKey <ILanguage>(), () => null);
            Assert.IsNotNull(globalFullCached);
            ILanguage globalCached = globalFullCached.First(x => x.Id == lang.Id);

            Assert.IsNotNull(globalCached);
            Assert.AreEqual(lang.Id, globalCached.Id);
            Assert.AreEqual("fr-FR", globalCached.IsoCode);

            Assert.IsNull(scopeProvider.AmbientScope);
            using (IScope scope = scopeProvider.CreateScope(repositoryCacheMode: RepositoryCacheMode.Scoped))
            {
                Assert.IsInstanceOf <Scope>(scope);
                Assert.IsNotNull(scopeProvider.AmbientScope);
                Assert.AreSame(scope, scopeProvider.AmbientScope);

                // scope has its own isolated cache
                IAppPolicyCache scopedCache = scope.IsolatedCaches.GetOrCreate(typeof(ILanguage));
                Assert.AreNotSame(globalCache, scopedCache);

                // Use IsMandatory of isocode to ensure publishedContent cache is not also rebuild
                lang.IsMandatory = true;
                service.Save(lang);

                // scoped cache has been flushed, reload
                var scopeFullCached = (IEnumerable <ILanguage>)scopedCache.Get(GetCacheTypeKey <ILanguage>(), () => null);
                Assert.IsNull(scopeFullCached);
                reload = service.GetLanguageById(lang.Id);

                // scoped cache contains the "new" entity
                scopeFullCached = (IEnumerable <ILanguage>)scopedCache.Get(GetCacheTypeKey <ILanguage>(), () => null);
                Assert.IsNotNull(scopeFullCached);
                ILanguage scopeCached = scopeFullCached.First(x => x.Id == lang.Id);
                Assert.IsNotNull(scopeCached);
                Assert.AreEqual(lang.Id, scopeCached.Id);
                Assert.AreEqual(true, scopeCached.IsMandatory);

                // global cache is unchanged
                globalFullCached = (IEnumerable <ILanguage>)globalCache.Get(GetCacheTypeKey <ILanguage>(), () => null);
                Assert.IsNotNull(globalFullCached);
                globalCached = globalFullCached.First(x => x.Id == lang.Id);
                Assert.IsNotNull(globalCached);
                Assert.AreEqual(lang.Id, globalCached.Id);
                Assert.AreEqual(false, globalCached.IsMandatory);

                if (complete)
                {
                    scope.Complete();
                }
            }

            Assert.IsNull(scopeProvider.AmbientScope);

            globalFullCached = (IEnumerable <ILanguage>)globalCache.Get(GetCacheTypeKey <ILanguage>(), () => null);
            if (complete)
            {
                // global cache has been cleared
                Assert.IsNull(globalFullCached);
            }
            else
            {
                // global cache has *not* been cleared
                Assert.IsNotNull(globalFullCached);
            }

            // get again, updated if completed
            lang = service.GetLanguageById(lang.Id);
            Assert.AreEqual(complete ? true : false, lang.IsMandatory);

            // global cache contains the entity again
            globalFullCached = (IEnumerable <ILanguage>)globalCache.Get(GetCacheTypeKey <ILanguage>(), () => null);
            Assert.IsNotNull(globalFullCached);
            globalCached = globalFullCached.First(x => x.Id == lang.Id);
            Assert.IsNotNull(globalCached);
            Assert.AreEqual(lang.Id, globalCached.Id);
            Assert.AreEqual(complete ? true : false, lang.IsMandatory);
        }