public override void RunBackgroundTask() { // this task only works if the RSS feed is configured to be the datasource. if (GlossaryPlaceholderData.DataSource == GlossaryPlaceholderData.GlossaryDataSource.RssFeed) { string dataCacheKey = GlossaryPlaceholderData.getRssDataPersistentVariableName(); string lastRunCacheKey = dataCacheKey + "_LastRun"; CmsPersistentVariable persistedLastRun = CmsPersistentVariable.Fetch(lastRunCacheKey); bool doFetch = false; if (persistedLastRun.Name == "" || persistedLastRun.PersistedValue == null) { doFetch = true; } if (persistedLastRun.Name != "" && !doFetch) { DateTime lastRunTime = (DateTime)persistedLastRun.PersistedValue; if (lastRunTime < DateTime.Now.AddDays(-1)) { doFetch = true; } } if (doFetch) { FetchAndSaveRemoteRSSGlossaryData(); } } } // RunBackgroundTask
} // RunBackgroundTask public bool FetchAndSaveRemoteRSSGlossaryData() { string dataCacheKey = GlossaryPlaceholderData.getRssDataPersistentVariableName(); string lastRunCacheKey = dataCacheKey + "_LastRun"; string rssUrl = GlossaryPlaceholderData.getRssDataSourceUrl(); Rss.RssFeed glossaryRss = Rss.RssFeed.Read(rssUrl); if (glossaryRss.Channels.Count == 0) { // html.Append("<em>Error: could not retrieve Glossary from "+rssUrl+"</em>"); } else { GlossaryData[] items = GlossaryData.FromRSSItems(glossaryRss.Channels[0].Items); CmsPersistentVariable persistedData = CmsPersistentVariable.Fetch(dataCacheKey); persistedData.Name = dataCacheKey; persistedData.PersistedValue = new List <GlossaryData>(items); bool b = persistedData.SaveToDatabase(); if (b) { CmsPersistentVariable persistedLastRun = CmsPersistentVariable.Fetch(lastRunCacheKey); persistedLastRun.PersistedValue = DateTime.Now; persistedLastRun.Name = lastRunCacheKey; return(persistedLastRun.SaveToDatabase()); } } return(false); } // FetchAndSaveRemoteRSSGlossaryData
protected override void Render(HtmlTextWriter writer) { StringBuilder html = new StringBuilder(); string swh = "http://www.sadcwaterhub.org/glossary/feed?lang_tid[0]=2"; string url = CmsControlUtils.getControlParameterKeyValue(CmsContext.currentPage, this, "rssurl", swh); int cacheDuration_hours = CmsControlUtils.getControlParameterKeyValue(CmsContext.currentPage, this, "cacheduration_hours", 12); System.Web.Caching.Cache Cache = System.Web.Hosting.HostingEnvironment.Cache; Rss.RssFeed glossaryRss; // the RSS feed is cached to improve performance. if (cacheDuration_hours >= 0 && Cache[url] != null) { glossaryRss = (Rss.RssFeed)Cache[url]; } else { glossaryRss = Rss.RssFeed.Read(url); // add it to the cache if (cacheDuration_hours >= 0) { Cache.Insert(url, glossaryRss, null, DateTime.Now.AddHours(cacheDuration_hours), System.Web.Caching.Cache.NoSlidingExpiration); } } if (glossaryRss.Channels.Count == 0) { html.Append("<em>Error: could not retrieve Glossary from SADC Water Hub</em>"); } else { GlossaryData[] items = ToGlossaryData(glossaryRss.Channels[0].Items); GlossaryPlaceholderData phData = new GlossaryPlaceholderData(); phData.SortOrder = GlossaryPlaceholderData.GlossarySortOrder.byWord; phData.ViewMode = GlossaryPlaceholderData.GlossaryViewMode.PagePerLetter; string[] charsWithData = getCharsWithData(items); string letterToDisplay = Glossary.getLetterToDisplay(phData); if (letterToDisplay != "") { items = GlossaryData.getItemsStartingWithChar(items, letterToDisplay[0]); } html.Append(Glossary.GetHtmlDisplay(CmsContext.currentPage, items, phData, charsWithData, letterToDisplay)); } // else writer.Write(html.ToString()); }