Hi,
Below is an example with caching where you can specify Cache time as input parameter
Below is an example with caching where you can specify Cache time as input parameter
@inherits RazorFunction
@using System.Web.Caching
@functions {
public override string FunctionDescription
{
get { return "Create a gallery of images from a Picasa album RSS feed."; }
}
[FunctionParameter]
public string RssUrl { get; set; }
[FunctionParameter(Label = "Albums count", Help = "Default value is 10", DefaultValue = 10)]
public int AlbumCount { get; set; }
[FunctionParameter(Label = "CacheTime in secons", Help = "Default value is 3600", DefaultValue = 3600)]
public int CacheTimeInSecons { get; set; }
private XName XML_TITLE_ENTRY = XName.Get("title", "http://www.w3.org/2005/Atom");
private XName XML_MEDIAGROUP_ENTRY = XName.Get("group", "http://search.yahoo.com/mrss/");
private XName XML_CONTENT_MEDIAGROUP = XName.Get("content", "http://search.yahoo.com/mrss/");
private XName XML_THUMBNAIL_MEDIAGROUP = XName.Get("thumbnail", "http://search.yahoo.com/mrss/");
private XName XML_ENTRY = XName.Get("entry", "http://www.w3.org/2005/Atom");
private XName XML_LINK = XName.Get("link", "http://www.w3.org/2005/Atom");
private XElement GetFeed(string feed)
{
if (HttpContext.Current.Cache[feed] == null)
{
HttpContext.Current.Cache.Add(feed, XDocument.Load(feed).Root,
null, DateTime.Now.AddSeconds(CacheTimeInSecons),
Cache.NoSlidingExpiration,
CacheItemPriority.Default,
null);
}
return HttpContext.Current.Cache[feed] as XElement;
}
}
@{
var feed = GetFeed(RssUrl);
var entries = feed.Elements(XML_ENTRY).Take(AlbumCount).ToList();
}
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://www.composite.net/ns/function/1.0">
<head>
</head>
<body>
<div>
@foreach (var entry in entries)
{
var media = entry.Element(XML_MEDIAGROUP_ENTRY);
var link = entry.Elements(XML_LINK).FirstOrDefault(el => (string)el.Attribute("rel") == "alternate");
if (media != null && link != null)
{
var imgSrc = media.Element(XML_THUMBNAIL_MEDIAGROUP).Attribute("url").Value;
<a href="@link.Attribute("href").Value"><img src="@imgSrc" alt="@entry.Element(XML_TITLE_ENTRY).Value" class="photogalleryicon" /></a>
}
}
</div>
</body>
</html>