Beispiel #1
0
        public void should_getallkeys()
        {
            // Arrange
            CacheMock           cache    = new CacheMock();
            ApplicationSettings settings = new ApplicationSettings()
            {
                UseObjectCache = true
            };

            List <string> tagCacheItems1 = new List <string>()
            {
                "1", "2"
            };
            List <string> tagCacheItems2 = new List <string>()
            {
                "a", "b"
            };
            ListCache listCache = new ListCache(settings, cache);

            // Act
            listCache.Add("all.tags1", tagCacheItems1);
            listCache.Add("all.tags2", tagCacheItems2);

            // Assert
            List <string> keys = listCache.GetAllKeys().ToList();

            Assert.That(keys, Contains.Item(CacheKeys.ListCacheKey("all.tags1")));
            Assert.That(keys, Contains.Item(CacheKeys.ListCacheKey("all.tags2")));
        }
        public void ClearPages_Should_Set_TempData_Message_And_Clear_Cache_And_Clear_All_Pages()
        {
            // Arrange
            _repository.AddNewPage(new Page()
            {
                Id = 1
            }, "text", "admin", DateTime.UtcNow);
            _repository.AddNewPage(new Page()
            {
                Id = 2
            }, "text", "admin", DateTime.UtcNow);

            _pageCache.Add(1, new PageViewModel());
            _listCache.Add("list.somekey", new List <string>());
            _siteCache.AddMenu("should not be cleared");

            // Act
            RedirectToRouteResult result = _toolsController.ClearPages() as RedirectToRouteResult;

            // Assert
            Assert.That(result, Is.Not.Null, "RedirectToRouteResult");
            Assert.That(result.RouteValues["action"], Is.EqualTo("Index"));

            Assert.That(_toolsController.TempData["SuccessMessage"], Is.EqualTo(SiteStrings.SiteSettings_Tools_ClearDatabase_Message));
            Assert.That(_cache.Count(), Is.EqualTo(1));
            Assert.That(_repository.AllPages().Count(), Is.EqualTo(0));
        }
Beispiel #3
0
        public void should_not_add_if_cache_disabled()
        {
            // Arrange
            CacheMock           cache    = new CacheMock();
            ApplicationSettings settings = new ApplicationSettings()
            {
                UseObjectCache = false
            };

            List <string> tagCacheItems = new List <string>()
            {
                "1", "2"
            };
            ListCache listCache = new ListCache(settings, cache);

            // Act
            listCache.Add("all.tags", tagCacheItems);

            // Assert
            var tags = listCache.Get <string>("all.tags");

            Assert.That(tags, Is.Null);

            IEnumerable <string> keys = listCache.GetAllKeys();

            Assert.That(keys.Count(), Is.EqualTo(0));
        }
Beispiel #4
0
        /// <summary>
        /// Retrieves a list of all pages in the system.
        /// </summary>
        /// <returns>An <see cref="IEnumerable{PageViewModel}"/> of the pages.</returns>
        /// <exception cref="DatabaseException">An databaseerror occurred while retrieving the list.</exception>
        public IEnumerable <PageViewModel> AllPages(bool loadPageContent = false)
        {
            try
            {
                string cacheKey = "";
                IEnumerable <PageViewModel> pageModels;

                if (loadPageContent)
                {
                    cacheKey   = CacheKeys.AllPagesWithContent();
                    pageModels = _listCache.Get <PageViewModel>(cacheKey);

                    if (pageModels == null)
                    {
                        IEnumerable <Page> pages = Repository.AllPages().OrderBy(p => p.Title);
                        pageModels = from page in pages
                                     select new PageViewModel(Repository.GetLatestPageContent(page.Id), _markupConverter);

                        _listCache.Add <PageViewModel>(cacheKey, pageModels);
                    }
                }
                else
                {
                    cacheKey   = CacheKeys.AllPages();
                    pageModels = _listCache.Get <PageViewModel>(cacheKey);

                    if (pageModels == null)
                    {
                        IEnumerable <Page> pages = Repository.AllPages().OrderBy(p => p.Title);
                        pageModels = from page in pages
                                     select new PageViewModel()
                        {
                            Id = page.Id, Title = page.Title
                        };

                        _listCache.Add <PageViewModel>(cacheKey, pageModels);
                    }
                }

                return(pageModels);
            }
            catch (DatabaseException ex)
            {
                throw new DatabaseException(ex, "An error occurred while retrieving all pages from the database");
            }
        }
Beispiel #5
0
		public void Should_GetAllKeys()
		{
			// Arrange
			CacheMock cache = new CacheMock();
			ApplicationSettings settings = new ApplicationSettings() { UseObjectCache = true };

			List<string> tagCacheItems1 = new List<string>() { "1", "2" };
			List<string> tagCacheItems2 = new List<string>() { "a", "b" };
			ListCache listCache = new ListCache(settings, cache);

			// Act
			listCache.Add("all.tags1", tagCacheItems1);
			listCache.Add("all.tags2", tagCacheItems2);

			// Assert
			List<string> keys = listCache.GetAllKeys().ToList();
			Assert.That(keys, Contains.Item(CacheKeys.ListCacheKey("all.tags1")));
			Assert.That(keys, Contains.Item(CacheKeys.ListCacheKey("all.tags2")));
		}
        public void index_should_return_viewmodel_with_filled_properties()
        {
            // Arrange
            _applicationSettings.UseObjectCache = true;
            _pageCache.Add(1, new PageViewModel());
            _listCache.Add <string>("test", new List <string>());
            _siteCache.AddMenu("menu");

            // Act
            ViewResult result = _cacheController.Index() as ViewResult;

            // Assert
            Assert.That(result, Is.Not.Null, "ViewResult");

            CacheViewModel model = result.ModelFromActionResult <CacheViewModel>();

            Assert.NotNull(model, "Null model");
            Assert.That(model.IsCacheEnabled, Is.True);
            Assert.That(model.PageKeys.Count(), Is.EqualTo(1));
            Assert.That(model.ListKeys.Count(), Is.EqualTo(1));
            Assert.That(model.SiteKeys.Count(), Is.EqualTo(1));
        }
Beispiel #7
0
		public void Should_Add_Item()
		{
			// Arrange
			CacheMock cache = new CacheMock();
			ApplicationSettings settings = new ApplicationSettings() { UseObjectCache = true };

			List<string> tagCacheItems = new List<string>() { "1", "2" };
			ListCache listCache = new ListCache(settings, cache);

			// Act
			listCache.Add("all.tags", tagCacheItems);

			// Assert
			Assert.That(cache.CacheItems.Count, Is.EqualTo(1));
		}
        public static void InsertCache(SiteInfo siteInfo, ChannelInfo channelInfo, ContentInfo contentInfo)
        {
            if (contentInfo.SourceId == SourceManager.Preview)
            {
                return;
            }

            ListCache.Add(channelInfo, contentInfo);

            var dict = ContentCache.GetContentDict(contentInfo.ChannelId);

            dict[contentInfo.Id] = contentInfo;

            var tableName = ChannelManager.GetTableName(siteInfo, channelInfo);

            CountCache.Add(tableName, contentInfo);

            StlContentCache.ClearCache();
        }
        public void Edit_POST_Should_Save_Setting_Values_To_Repository_From_Model_And_Clear_SiteCache()
        {
            // Arrange
            _pageViewModelCache.Add(1, new PageViewModel());             // dummmy items
            _listCache.Add("a key", new List <string>()
            {
                "1", "2"
            });

            TextPluginStub plugin = new TextPluginStub();

            plugin.Repository  = _repository;
            plugin.PluginCache = _siteCache;
            plugin.Settings.SetValue("name1", "default-value1");
            plugin.Settings.SetValue("name2", "default-value2");

            _pluginFactory.RegisterTextPlugin(plugin);

            PluginViewModel model = new PluginViewModel();

            model.Id            = plugin.Id;
            model.SettingValues = new List <SettingValue>();
            model.SettingValues.Add(new SettingValue()
            {
                Name = "name1", Value = "new-value1"
            });
            model.SettingValues.Add(new SettingValue()
            {
                Name = "name2", Value = "new-value2"
            });

            // Act
            ViewResult result = _controller.Edit(model) as ViewResult;

            // Assert
            List <SettingValue> values = _repository.TextPlugins[0].Settings.Values.ToList();

            Assert.That(values[0].Value, Is.EqualTo("new-value1"));
            Assert.That(values[1].Value, Is.EqualTo("new-value2"));

            Assert.That(_memoryCache.Count(), Is.EqualTo(0));
        }
        /// <summary>
        /// 读取缓存的ids(缺省读取前1000个)
        /// </summary>
        /// <param name="bean"></param>
        /// <param name="orderby"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public long[] GetCacheIds(T bean, string orderby = null, uint count = 1000)
        {
            if (bean == null)
            {
                throw new Exception("bean 不能为 NULL");
            }

            long[] ids = null;
            string ck  = "ids_" + CombineCacheKey(bean, orderby, count);

            if (ListCache.Get(ck, ref ids))
            {
                return(ids);
            }

            if (!_lock.Add(ck))
            {
                System.Threading.Thread.Sleep(5);
                if (ListCache.Get(ck, ref ids))
                {
                    return(ids);
                }
            }
            try
            {
                ids = GetIds(bean, orderby, count);
                if (ids == null)
                {
                    ListCache.Add(ck, null, 5);
                }
                else
                {
                    ListCache.Add(ck, ids);
                }
            }
            finally
            {
                _lock.Remove(ck);
            }
            return(ids);
        }
Beispiel #11
0
        public void should_add_item()
        {
            // Arrange
            CacheMock           cache    = new CacheMock();
            ApplicationSettings settings = new ApplicationSettings()
            {
                UseObjectCache = true
            };

            List <string> tagCacheItems = new List <string>()
            {
                "1", "2"
            };
            ListCache listCache = new ListCache(settings, cache);

            // Act
            listCache.Add("all.tags", tagCacheItems);

            // Assert
            Assert.That(cache.CacheItems.Count, Is.EqualTo(1));
        }
Beispiel #12
0
        /// <summary>
        /// Finds all pages with the given tag.
        /// </summary>
        /// <param name="tag">The tag to search for.</param>
        /// <returns>A <see cref="IEnumerable{PageViewModel}"/> of pages tagged with the provided tag.</returns>
        /// <exception cref="DatabaseException">An database error occurred while getting the list.</exception>
        public IEnumerable <PageViewModel> FindByTag(string tag)
        {
            try
            {
                string cacheKey = string.Format("pagesbytag.{0}", tag);

                IEnumerable <PageViewModel> models = _listCache.Get <PageViewModel>(cacheKey);
                if (models == null)
                {
                    IEnumerable <Page> pages = Repository.FindPagesContainingTag(tag).OrderBy(p => p.Title);
                    models = from page in pages
                             select new PageViewModel(Repository.GetLatestPageContent(page.Id), _markupConverter);

                    _listCache.Add <PageViewModel>(cacheKey, models);
                }

                return(models);
            }
            catch (DatabaseException ex)
            {
                throw new DatabaseException(ex, "An error occurred finding the tag '{0}' in the database", tag);
            }
        }
Beispiel #13
0
        public void UpdateLinksToPage_Should_Clear_Cache()
        {
            // Arrange
            _container.ClearCache();
            _repository.AddNewPage(new Page()
            {
                Id = 1, Title = "Homepage"
            }, "This is a link to [[About page title|About]]", "editor", DateTime.UtcNow);
            _repository.AddNewPage(new Page()
            {
                Id = 2, Title = "About page title"
            }, "This is a link to [[Homepage|Back home]]", "editor", DateTime.UtcNow);

            _pageViewModelCache.Add(1, new PageViewModel());
            _pageViewModelCache.Add(2, new PageViewModel());
            _listCache.Add("somekey", new List <string>());

            // Act
            _pageService.UpdateLinksToPage("About page title", "New title");

            // Assert
            Assert.That(_pageViewModelCache.GetAllKeys().Count(), Is.EqualTo(0));
            Assert.That(_listCache.GetAllKeys().Count(), Is.EqualTo(0));
        }
        /// <summary>
        /// 读取缓存的ids(缺省读取前1000个)
        /// </summary>
        /// <param name="orderby"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public long[] GetCacheIds(string orderby = null, uint count = 1000)
        {
            long[] ids = null;
            string ck  = "ids_" + CombineCacheKey(orderby, count);

            if (ListCache.Get(ck, ref ids))
            {
                return(ids);
            }

            if (!_lock.Add(ck))
            {
                System.Threading.Thread.Sleep(5);
                if (ListCache.Get(ck, ref ids))
                {
                    return(ids);
                }
            }
            try
            {
                ids = GetIds(orderby, count);
                if (ids == null)
                {
                    ListCache.Add(ck, null, 5);
                }
                else
                {
                    ListCache.Add(ck, ids);
                }
            }
            finally
            {
                _lock.Remove(ck);
            }
            return(ids);
        }
        /// <summary>
        /// 读取缓存的ids(缺省读取前1000个)
        /// </summary>
        /// <param name="cond"></param>
        /// <param name="from"></param>
        /// <returns></returns>
        public long[] GetCacheIds(Sql cond = null)
        {
            long[] ids = null;
            string ck  = "ids_" + CombineCacheKey(cond);

            if (ListCache.Get(ck, ref ids))
            {
                return(ids);
            }

            if (!_lock.Add(ck))
            {
                System.Threading.Thread.Sleep(5);
                if (ListCache.Get(ck, ref ids))
                {
                    return(ids);
                }
            }
            try
            {
                ids = GetIds(cond);
                if (ids == null)
                {
                    ListCache.Add(ck, null, 5);
                }
                else
                {
                    ListCache.Add(ck, ids);
                }
            }
            finally
            {
                _lock.Remove(ck);
            }
            return(ids);
        }
Beispiel #16
0
		public void Should_Not_Add_If_Cache_Disabled()
		{
			// Arrange
			CacheMock cache = new CacheMock();
			ApplicationSettings settings = new ApplicationSettings() { UseObjectCache = false };
			
			List<string> tagCacheItems = new List<string>() { "1", "2" };			
			ListCache listCache = new ListCache(settings, cache);

			// Act
			listCache.Add("all.tags", tagCacheItems);

			// Assert
			var tags = listCache.Get<string>("all.tags");
			Assert.That(tags, Is.Null);

			IEnumerable<string> keys = listCache.GetAllKeys();
			Assert.That(keys.Count(), Is.EqualTo(0));
		}