• 缓存类


    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"];
                }
     
            }
        }
    }
  • 相关阅读:
    Java根据百度API获得经纬度,然后根据经纬度在获得城市信息
    获取鼠标位置的几个通用的JS函数
    java 定时备份数据库
    基于commons-net实现ftp创建文件夹、上传、下载功能
    java自动识别用户上传的文本文件编码
    CSS3实现10种Loading效果
    Java实现拖拽上传
    JAVA 比较两张图片的相似度的代码
    Java jsp页面中jstl标签详解
    mysql去除重复数据
  • 原文地址:https://www.cnblogs.com/superMay/p/4097442.html
Copyright © 2020-2023  润新知