缓存篇(Cache)~第一回 使用static静态成员实现服务器端缓存(导航面包屑)

时间:2024-07-26 23:04:50

返回目录

今天写缓存篇的第一篇文章,在写完目录后,得到了一些朋友的关注,这给我之后的写作带来了无穷的力量,在这里,感谢那几位伙伴,哈哈!

书归正传,今天我带来一个Static静态成员的缓存,其实它也不是什么缓存,就是C#语言里的一个特性,静态成员在被初始化后它将不会再被执行,即,他里面的内容只会被执行一次,直到你的网站被重启后(只考虑在单线程情况下)。相信大家都在做网站时,遇到了网站导航面包屑功能点吧,一般,我们把它写死在页面上,这种作法没有任何可扩展性和可维护性,所以,今天我们要改善一下这个功能点,使用的技术就是静态成员缓存和树型结构。

先看一下导航的结构

   /// <summary>
/// 导航面包
/// </summary>
public class BannerBread
{
public string DisplayTitle { get; set; }
public string Url { get; set; }
public int ID { get; set; }
public int ParentID { get; set; }
public BannerBread Parent { get; set; }
}

下面这段代码就是实现导航的核心,它对外提供一个唯一的访问点,所以,和两个公开的方法,一个是GetBanner,用来返回当前URL对应的导航完整对象(包括祖宗对象),GenerateStandardBanner方法返回一个标准的导航,当前,具体的项目,你可以自己

去实现它。

    /// <summary>
/// 导航生产类
/// </summary>
public static class BannerBreadFactory
{
/// <summary>
/// BannerBread单例实例对象
/// </summary>
static readonly List<BannerBread> Instance = new List<BannerBread>(); static BannerBreadFactory()
{
Instance.Add(new BannerBread { ID = , DisplayTitle = "首页", Parent = null, ParentID = , Url = "/" });
Instance.Add(new BannerBread { ID = , DisplayTitle = "关于我们", Parent = null, ParentID = , Url = "/Home/About" });
Instance.Add(new BannerBread { ID = , DisplayTitle = "联系我们", Parent = null, ParentID = , Url = "/Home/Contact" });
}
/// <summary>
/// 得到当前URL的祖宗节点
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static BannerBread GetBanner(string url)
{
var entity = Instance.FirstOrDefault(i => i.Url.ToUpper() == url.ToUpper());
GetFather(entity);
return entity;
}
/// <summary>
/// 标准的导航,程序开发人员可以根据自己的需要,在具体项目中去定义它
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string GenerateStandardBanner(string url)
{
var entity = GetBanner(url);
StringBuilder str = new StringBuilder();
while (entity != null)
{
str.Insert(, "/<span><a href='" + entity.Url + "'>" + entity.DisplayTitle + "</a></span>");
entity = entity.Parent;
}
return str.ToString().StartsWith("/") ? str.ToString().Substring() : "未找到导航项";
}
/// <summary>
/// 得到祖宗
/// </summary>
/// <param name="son"></param>
private static void GetFather(BannerBread son)
{
if (son != null)
{
son.Parent = Instance.FirstOrDefault(i => i.ID == son.ParentID);
GetFather(son.Parent);
}
} }

上面的代码中,对于导航数据的部分,我选择了静态数据,当然,真实的项目中,需要将这块放到数据库里,以便于后期维护,一般地,我们可以在布局页去调用它来实现导航显示的功能,非常方便,下面是运行后的效果截图

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABEwAAABxCAIAAABAyKGNAAAQ+UlEQVR4nO3dOXIbRxsGYB7Ap/l1DfoyTJzzFioGTpAzZzlg8Qxw8QBMVDTKBCVIhkSV8AfYZunu6Rlsg+Hz1JcYHMwKV/WrXuZiAQAAMCAXpz4BAACAfRJyAACAQRFyAACAQRFyAACAQRFyAACAQRFyAID3ZDz69Nvv0/ExDjW/ufz02++fru7fjnE0YEvIAQDegef7ycXlp6v7t8Xj9OLy081j4W+P04vLT7/9Xv4wsoeL0bzw2dvddeKLq5Aj58DRCTkAwFAsc8gyV8TqajS9KmaVyexq+5XJ3SS+93UWCu82EGNWIaeci4AjEHIAgKErhp+L6+nN9SbPrHPI9ey5cS+1Dp/NbtM9OecWcuY3l8mw995M3pp/G7t/pZuDHej5fnLm3Y9CDgAwcKtBZZuOmtXQtdH06rLSezO/uYwPLauFnPEoHZD2GXI6NTrnN9XxdXkms6vLvOC3T/NxcrhgrsfpReIhLhaLxeL5fhaflLX8tVR/FW3mcdX3sG+T2dVovlgsxqPyWU1mV6mA+nZ3XbmK+c1l+LpWgzOP/RvYIyEHABiMdQ4ptuw3/S2Fhm+4A2cZWqKxpB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3kAAICYMwg5OfmhWzL5568//vjrn10O3bhlTn9UMdgIOQAAsKNeh5zM3JJu+sf/2phx9tmD1HjOH8qzdIQcAADopu8hJ+fzVv+59fef/2vIOPvpn0mfVebnDdcCAACs9T3k7JIfgv+5UV9yoEN0aTxKfeeV7YUcAADYr76HnJzPO4Wc2LJq1S9m5or0ZrFgVv+WkAMAADvqe8g5UE9ObVm18NFjX49tHLuE2FkJOQAAsHd9Dzk5nxf/MxEbCtotOZATLXKGq8U+CR5LyAEAgG6GEHLqn6fnzzRmnLyktNMGxZ4cIQcAAPao7yEnZ7jaZsvYHiofppdVa5uscv66iHcNVc5QyAEAgB31NOQk4k1n631Hlxw4xEEP4XiPAQAAzlBPQw4AAEA3Qg4AADAoFy//vrzD+u/bf6e+8+389+2/k980pZRSSimlzqLeaciZfZmdOra0M/syO/lNU0oppZRS6izqnYac1+nrr1+/Tp1ccv369et1+nrym6aUUkoppdRZ1DsNOS//vvz48ePU4SXXjx8/Tn67lFJKKaWUOpd6vyHn27dvpw4vub59+3by26WUUkoppdS51PsNOZ8/fz6LEWu/fv36/PnzyW+XUkoppZRS51LvN+S8/Pvy/fv3U0eYZt+/fz/5jVJKKaWUUuqM6l2HnNnsDNZYm82sq6aUUkoppVSLetch56X3yw9YckAppZRSSqm29d5DztfZ11MHmZSvs68nv0VKKaWUUkqdV733kPPy78v3eU9n5nyfm42jlFJKKaVU6xJyXr58/vLz589TJ5qqnz9/fvn85eQ3RymllFJKqbMrIefl5d+Xb197986cb1+9G0cppZRSSqku9X8f6x0qMn2PdwAAAABJRU5ErkJggg==" alt="" />

对于具体的显示,我们的前台显示可以自己去组装,上面显示的只是一种标准的形式,即由GenerateStandardBanner方法提供的形式。

返回目录