首页 > 代码库 > 缓存类

缓存类

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"];
            }
 
        }
    }
}

缓存类