protected RepositoryCachePolicyBase(IAppPolicyCache globalCache, IScopeAccessor scopeAccessor)
 {
     _globalCache   = globalCache ?? throw new ArgumentNullException(nameof(globalCache));
     _scopeAccessor = scopeAccessor ?? throw new ArgumentNullException(nameof(scopeAccessor));
 }
Пример #2
0
        protected virtual TypeLoader GetTypeLoader(IIOHelper ioHelper, ITypeFinder typeFinder, IAppPolicyCache runtimeCache, IHostingEnvironment hostingEnvironment, ILogger <TypeLoader> logger, IProfilingLogger profilingLogger, UmbracoTestOptions.TypeLoader option)
        {
            switch (option)
            {
            case UmbracoTestOptions.TypeLoader.Default:
                return(_commonTypeLoader ?? (_commonTypeLoader = CreateCommonTypeLoader(typeFinder, runtimeCache, logger, profilingLogger, hostingEnvironment)));

            case UmbracoTestOptions.TypeLoader.PerFixture:
                return(_featureTypeLoader ?? (_featureTypeLoader = CreateTypeLoader(ioHelper, typeFinder, runtimeCache, logger, profilingLogger, hostingEnvironment)));

            case UmbracoTestOptions.TypeLoader.PerTest:
                return(CreateTypeLoader(ioHelper, typeFinder, runtimeCache, logger, profilingLogger, hostingEnvironment));

            default:
                throw new ArgumentOutOfRangeException(nameof(option));
            }
        }
Пример #3
0
 protected virtual TypeLoader CreateTypeLoader(IIOHelper ioHelper, ITypeFinder typeFinder, IAppPolicyCache runtimeCache, ILogger <TypeLoader> logger, IProfilingLogger profilingLogger, IHostingEnvironment hostingEnvironment)
 {
     return(CreateCommonTypeLoader(typeFinder, runtimeCache, logger, profilingLogger, hostingEnvironment));
 }
Пример #4
0
        /// <summary>
        /// This is used to configure the file sources with the main file sources shipped with Umbraco and also including supplemental/plugin based
        /// localization files. The supplemental files will be loaded in and merged in after the primary files.
        /// The supplemental files must be named with the 4 letter culture name with a hyphen such as : en-AU.xml
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="cache"></param>
        /// <param name="fileSourceFolder"></param>
        /// <param name="supplementFileSources"></param>
        public LocalizedTextServiceFileSources(
            ILogger <LocalizedTextServiceFileSources> logger,
            AppCaches appCaches,
            DirectoryInfo fileSourceFolder,
            IEnumerable <LocalizedTextServiceSupplementaryFileSource> supplementFileSources)
        {
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }
            if (appCaches == null)
            {
                throw new ArgumentNullException("cache");
            }
            if (fileSourceFolder == null)
            {
                throw new ArgumentNullException("fileSourceFolder");
            }

            _logger = logger;
            _cache  = appCaches.RuntimeCache;

            if (fileSourceFolder.Exists == false)
            {
                _logger.LogWarning("The folder does not exist: {FileSourceFolder}, therefore no sources will be discovered", fileSourceFolder.FullName);
            }
            else
            {
                _fileSourceFolder      = fileSourceFolder;
                _supplementFileSources = supplementFileSources;
            }

            //Create the lazy source for the _xmlSources
            _xmlSources = new Lazy <Dictionary <CultureInfo, Lazy <XDocument> > >(() =>
            {
                var result = new Dictionary <CultureInfo, Lazy <XDocument> >();

                if (_fileSourceFolder == null)
                {
                    return(result);
                }

                foreach (var fileInfo in _fileSourceFolder.GetFiles("*.xml"))
                {
                    var localCopy = fileInfo;
                    var filename  = Path.GetFileNameWithoutExtension(localCopy.FullName).Replace("_", "-");

                    // TODO: Fix this nonsense... would have to wait until v8 to store the language files with their correct
                    // names instead of storing them as 2 letters but actually having a 4 letter culture. So now, we
                    // need to check if the file is 2 letters, then open it to try to find it's 4 letter culture, then use that
                    // if it's successful. We're going to assume (though it seems assuming in the legacy logic is never a great idea)
                    // that any 4 letter file is named with the actual culture that it is!
                    CultureInfo culture = null;
                    if (filename.Length == 2)
                    {
                        //we need to open the file to see if we can read it's 'real' culture, we'll use XmlReader since we don't
                        //want to load in the entire doc into mem just to read a single value
                        using (var fs = fileInfo.OpenRead())
                            using (var reader = XmlReader.Create(fs))
                            {
                                if (reader.IsStartElement())
                                {
                                    if (reader.Name == "language")
                                    {
                                        if (reader.MoveToAttribute("culture"))
                                        {
                                            var cultureVal = reader.Value;
                                            try
                                            {
                                                culture = CultureInfo.GetCultureInfo(cultureVal);
                                                //add to the tracked dictionary
                                                _twoLetterCultureConverter[filename] = culture;
                                            }
                                            catch (CultureNotFoundException)
                                            {
                                                _logger.LogWarning("The culture {CultureValue} found in the file {CultureFile} is not a valid culture", cultureVal, fileInfo.FullName);
                                                //If the culture in the file is invalid, we'll just hope the file name is a valid culture below, otherwise
                                                // an exception will be thrown.
                                            }
                                        }
                                    }
                                }
                            }
                    }
                    if (culture == null)
                    {
                        culture = CultureInfo.GetCultureInfo(filename);
                    }

                    //get the lazy value from cache
                    result[culture] = new Lazy <XDocument>(() => _cache.GetCacheItem <XDocument>(
                                                               string.Format("{0}-{1}", typeof(LocalizedTextServiceFileSources).Name, culture.Name), () =>
                    {
                        XDocument xdoc;

                        //load in primary
                        using (var fs = localCopy.OpenRead())
                        {
                            xdoc = XDocument.Load(fs);
                        }

                        //load in supplementary
                        MergeSupplementaryFiles(culture, xdoc);

                        return(xdoc);
                    }, isSliding: true, timeout: TimeSpan.FromMinutes(10), dependentFiles: new[] { localCopy.FullName }));
                }
                return(result);
            });
        }
Пример #5
0
 // common to all tests = cannot be overriden
 private static TypeLoader CreateCommonTypeLoader(IIOHelper ioHelper, ITypeFinder typeFinder, IAppPolicyCache runtimeCache, IGlobalSettings globalSettings, IProfilingLogger logger)
 {
     return(new TypeLoader(ioHelper, typeFinder, runtimeCache, new DirectoryInfo(globalSettings.LocalTempPath), logger, false, new[]
     {
         Assembly.Load("Umbraco.Core"),
         Assembly.Load("Umbraco.Web"),
         Assembly.Load("Umbraco.Tests")
     }));
 }
 public DefaultRepositoryCachePolicy(IAppPolicyCache cache, IScopeAccessor scopeAccessor, RepositoryCachePolicyOptions options)
     : base(cache, scopeAccessor)
 {
     _options = options ?? throw new ArgumentNullException(nameof(options));
 }
Пример #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeLoader"/> class.
 /// </summary>
 /// <param name="ioHelper"></param>
 /// <param name="typeFinder"></param>
 /// <param name="runtimeCache">The application runtime cache.</param>
 /// <param name="localTempPath">Files storage location.</param>
 /// <param name="logger">A profiling logger.</param>
 /// <param name="assembliesToScan"></param>
 public TypeLoader(IIOHelper ioHelper, ITypeFinder typeFinder, IAppPolicyCache runtimeCache, DirectoryInfo localTempPath, IProfilingLogger logger, IEnumerable <Assembly> assembliesToScan = null)
     : this(ioHelper, typeFinder, runtimeCache, localTempPath, logger, true, assembliesToScan)
 {
 }
Пример #8
0
        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);
        }
Пример #9
0
        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);
        }
Пример #10
0
 public IconService(IGlobalSettings globalSettings, IHtmlSanitizer htmlSanitizer, AppCaches appCaches)
 {
     _globalSettings = globalSettings;
     _htmlSanitizer  = htmlSanitizer;
     _cache          = appCaches.RuntimeCache;
 }
Пример #11
0
 public IconService(IGlobalSettings globalSettings, AppCaches appCaches)
 {
     _globalSettings = globalSettings;
     _cache          = appCaches.RuntimeCache;
 }
 public ContentNodeIconsService(AppCaches appCaches, IScopeProvider scopeProvider)
 {
     _runtimeCache  = appCaches.RuntimeCache;
     _scopeProvider = scopeProvider;
 }
Пример #13
0
 public EmbeddedContentService(IContentTypeService contentTypeService, IDataTypeService dataTypeService, IAppPolicyCache runtimeCache, IPublishedModelFactory publishedModelFactory, ILogger logger)
 {
     _contentTypeService    = contentTypeService;
     _dataTypeService       = dataTypeService;
     _runtimeCache          = runtimeCache;
     _publishedModelFactory = publishedModelFactory;
     _logger = logger;
 }
Пример #14
0
 public DefaultRepositoryCachePolicy(IAppPolicyCache cache, IScopeAccessor scopeAccessor, RepositoryCachePolicyOptions options)
     : base(cache, scopeAccessor) =>
Пример #15
0
 public SingleItemsOnlyRepositoryCachePolicy(IAppPolicyCache cache, IScopeAccessor scopeAccessor, RepositoryCachePolicyOptions options)
     : base(cache, scopeAccessor, options)
 {
 }
Пример #16
0
        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 FullDataSetRepositoryCachePolicy(IAppPolicyCache cache, IScopeAccessor scopeAccessor, Func <TEntity, TId> entityGetId, bool expires)
     : base(cache, scopeAccessor)
 {
     _entityGetId = entityGetId;
     _expires     = expires;
 }
Пример #18
0
 public IRepositoryCachePolicy <TEntity, TId> Scoped(IAppPolicyCache runtimeCache, IScope scope)
 {
     throw new NotImplementedException();
 }
Пример #19
0
 protected virtual TypeLoader CreateTypeLoader(IAppPolicyCache runtimeCache, IGlobalSettings globalSettings, IProfilingLogger logger)
 {
     return(CreateCommonTypeLoader(runtimeCache, globalSettings, logger));
 }
Пример #20
0
 public BenchmarkService(IUmbracoContextAccessor umbracoContextAccessor, IContentService contentService, AppCaches caches)
 {
     _umbracoContextAccessor = umbracoContextAccessor;
     _contentService         = contentService;
     _runtimeCache           = caches.RuntimeCache;
 }
Пример #21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeLoader"/> class.
 /// </summary>
 /// <param name="runtimeCache">The application runtime cache.</param>
 /// <param name="localTempPath">Files storage location.</param>
 /// <param name="logger">A profiling logger.</param>
 public TypeLoader(IAppPolicyCache runtimeCache, string localTempPath, IProfilingLogger logger)
     : this(runtimeCache, localTempPath, logger, true)
 {
 }
        protected override TypeLoader CreateTypeLoader(IIOHelper ioHelper, ITypeFinder typeFinder, IAppPolicyCache runtimeCache, ILogger <TypeLoader> logger, IProfilingLogger profilingLogger, IHostingEnvironment hostingEnvironment)
        {
            var baseLoader = base.CreateTypeLoader(ioHelper, typeFinder, runtimeCache, logger, profilingLogger, hostingEnvironment);

            return(new TypeLoader(typeFinder, runtimeCache, new DirectoryInfo(hostingEnvironment.LocalTempPath), logger, profilingLogger, false,
                                  // this is so the model factory looks into the test assembly
                                  baseLoader.AssembliesToScan
                                  .Union(new[] { typeof(PublishedContentMoreTests).Assembly })
                                  .ToList()));
        }
Пример #23
0
 /// <summary>
 /// Initializes a new instanced based on the specified <paramref name="caches"/>.
 /// </summary>
 /// <param name="caches">A reference to the Umbraco app caches.</param>
 public SpaCacheService(AppCaches caches)
 {
     _runtimeCache = caches.RuntimeCache;
 }
Пример #24
0
 protected virtual TypeLoader CreateTypeLoader(IIOHelper ioHelper, ITypeFinder typeFinder, IAppPolicyCache runtimeCache, IGlobalSettings globalSettings, IProfilingLogger logger)
 {
     return(CreateCommonTypeLoader(ioHelper, typeFinder, runtimeCache, globalSettings, logger));
 }