首页 > 代码库 > 缓存类
缓存类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyBlog.Linq;
using System.Configuration;
using MyBlog.BLL;
namespace MyBlog.UI
{
/// <summary>
/// 缓存类
/// </summary>
public static class CacheData
{
/// <summary>
/// 获取数据上下文,该属性可以保证每个请求仅生成一个数据上下文对象
/// </summary>
private static DBDataContext dc
{
get
{
if (HttpContext.Current.Items["dc"] == null)
{
DBDataContext _dc = new DBDataContext(ConfigurationManager.ConnectionStrings["myblog"].ConnectionString);
HttpContext.Current.Items.Add("dc", _dc);
}
return (DBDataContext)HttpContext.Current.Items["dc"];
}
}
/// <summary>
/// 获取UserManager对象,该属性可以保证每个请求仅生成一个UserManager对象
/// </summary>
public static UserManager UM
{
get
{
if (HttpContext.Current.Items["UM"] == null)
{
HttpContext.Current.Items.Add("UM", new UserManager(dc));
}
return (UserManager)HttpContext.Current.Items["UM"];
}
}
/// <summary>
/// 获取当前登录用户,若没有登录则为null,该属性可以保证每个请求仅生成一个BlogUser对象
/// </summary>
public static BlogUser LoginUser
{
get
{
if (HttpContext.Current.Items["U"] == null)
{
string name = HttpContext.Current.User.Identity.Name;
if (string.IsNullOrEmpty(name))
{
HttpContext.Current.Items.Add("U", "-1");
}
else
{
int id = Convert.ToInt32(name);
HttpContext.Current.Items.Add("U",UM.GetUserByID(id));
}
}
if (HttpContext.Current.Items["U"] is BlogUser)
{
return (BlogUser)HttpContext.Current.Items["U"];
}
else
{
return null;
}
}
}
/// <summary>
/// 获取TypeManager对象,该属性可以保证每个请求仅生成一个TypeManager对象
/// </summary>
public static TypeManager TM
{
get
{
if (HttpContext.Current.Items["TM"] == null)
{
HttpContext.Current.Items.Add("TM", new TypeManager(dc));
}
return (TypeManager)HttpContext.Current.Items["TM"];
}
}
/// <summary>
/// 获取ArticlManager对象,该属性可以保证每个请求仅生成一个ArticlManager对象
/// </summary>
public static ArticlManager AM
{
get
{
if (HttpContext.Current.Items["AM"] == null)
{
HttpContext.Current.Items.Add("AM", new ArticlManager(dc));
}
return (ArticlManager)HttpContext.Current.Items["AM"];
}
}
}
}
缓存类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。