|
源代码网推荐
大家可以看到,其中的 IConfigInfo接口没有任何属性和方法,是一个“空接口”,这主要是为
了提供统一的向上转型需要。当然,如果以后有需要还是会添加一些方法和属性的。
为了减少篇幅,下面只对其中的 GeneralConfig进行相应介绍,一是因为它是核心的配置类,有
关它的使用在项目中不胜枚举。另外它也是这个项目中比较标准的“宝贝”,从一开始,只要有新的
功能往往都会对这个配置类有所“关照”。下面就是通过反射后得到的代码,相关的说明见注释即可:
论坛全局配置管理类(GeneralConfigFileManager):
1/**//// <summary> 2 /// 全局配置设置管理类 3 /// </summary> 4 class GeneralConfigFileManager : Discuz.Config.DefaultConfigFileManager 5 { 6 private static GeneralConfigInfo m_configinfo; 7 8 9 /**//// <summary> 10 /// 文件修改时间 11 /// </summary> 12 private static DateTime m_fileoldchange; 13 14 15 /**//// <summary> 16 /// 初始化文件修改时间和对象实例 17 /// </summary> 18 static GeneralConfigFileManager() 19 { 20 m_fileoldchange = System.IO.File.GetLastWriteTime(ConfigFilePath); 21 22 try 23 { 24 m_configinfo = (GeneralConfigInfo)DefaultConfigFileManager.DeserializeInfo(ConfigFilePath, typeof(GeneralConfigInfo)); 25 } 26 catch 27 { 28 if (File.Exists(ConfigFilePath)) 29 { 30 ReviseConfig(); 31 m_configinfo = (GeneralConfigInfo)DefaultConfigFileManager.DeserializeInfo(ConfigFilePath, typeof(GeneralConfigInfo)); 32 } 33 } 34 } 35 36 public new static IConfigInfo ConfigInfo 37 { 38 get { return m_configinfo; } 39 set { m_configinfo = (GeneralConfigInfo) value; } 40 } 41 42 /**//// <summary> 43 /// 配置文件所在路径 44 /// </summary> 45 public static string filename = null; 46 47 48 /**//// <summary> 49 /// 获取配置文件所在路径 50 /// </summary> 51 public new static string ConfigFilePath 52 { 53 get 54 { 55 if (filename == null) 56 { 57 filename = Utils.GetMapPath(BaseConfigs.GetForumPath + "config/general.config"); 58 } 59 60 return filename; 61 } 62 63 } 64 65 /**//// <summary> 66 /// 返回配置类实例 67 /// </summary> 68 /// <returns></returns> 69 public static GeneralConfigInfo LoadConfig() 70 { 71 72 try 73 { 74 ConfigInfo = DefaultConfigFileManager.LoadConfig(ref m_fileoldchange, ConfigFilePath, ConfigInfo, true); 75 } 76 catch 77 { 78 ReviseConfig(); 79 ConfigInfo = DefaultConfigFileManager.LoadConfig(ref m_fileoldchange, ConfigFilePath, ConfigInfo, true); 80 } 81 return ConfigInfo as GeneralConfigInfo; 82 } 83 84 /**//// <summary> 85 /// 保存配置类实例 86 /// </summary> 87 /// <returns></returns> 88 public override bool SaveConfig() 89 { 90 return base.SaveConfig(ConfigFilePath, ConfigInfo); 91 } 92 93 论坛全局配置描述类(GeneralConfigInfo):
Code
1/**//// <summary> 2 /// 论坛全局配置描述类, 加[Serializable]标记为可序列化 3 /// </summary> 4 [Serializable] 5 public class GeneralConfigInfo : IConfigInfo 6 { 7 私有字段#region 私有字段 8 9 private string m_forumtitle = "Discuz!NT"; //论坛名称 10 private string m_forumurl = "forumindex.aspx"; //论坛url地址 11 private string m_webtitle = "Discuz!NT"; //网站名称 12 private string m_weburl = ""; //论坛网站url地址 13 private int m_licensed = 1; //是否显示商业授权链接 14 private string m_icp = ""; //网站备案信息 15 private int m_closed = 0; //论坛关闭 16 private string m_closedreason = ""; //论坛关闭提示信息 17 private int m_isframeshow = 0; //是否以框架方式显示 1是 0不是 18 private int m_admintools = 0; //是否使用管理员客户端工具,0=不使用,1=仅论坛创始人可用,2=管理员可用 19 private int m_indexpage = 0; //首页类型, 0=论坛首页, 1=聚合首页 20 private string m_linktext = "<a href="http://nt.discuz.net" title="The Official Discuz!NT Site" target="_blank">Discuz!NT</a>"; //外部链接html 21 22 private string m_passwordkey = "1234567890"; //用户密码Key 23 24 private int m_regstatus = 1; //是否允许新用户注册 25 private int m_regadvance = 1; //注册时候是否显示高级选项 26 private int m_realnamesystem = 0; //注册时是否启用实名制 27 private string m_censoruser = "admin"; //用户信息保留关键字 28 private int m_doublee = 0; //允许同一 Email 注册不同用户 29 private int m_regverify = 0; //新用户注册验证 30 private string m_accessemail = ""; //Email允许地址 31 private string m_censoremail = ""; //Email禁止地址 32 private int m_hideprivate = 1; //隐藏无权访问的论坛 33 private int m_regctrl = 0; //IP 注册间隔限制(小时) 34 private string m_ipregctrl = ""; //特殊 IP 注册限制 35 private string m_ipdenyaccess = ""; //IP禁止访问列表 36 private string m_ipaccess = ""; //IP访问列表 37 private string m_adminipaccess = ""; //管理员后台IP访问列表 38 private int m_newbiespan = 0; //新手见习期限(单位:分钟) 39 private int m_welcomemsg = 1; //发送欢迎短消息 40 private string m_welcomemsgtxt = "Welcome to visit this forum!"; //欢迎短消息内容 41 private int m_rules = 1; //是否显示注册许可协议 42 private string m_rulestxt = ""; //许可协议内容 43 private int m_secques = 0; //是否启用用户登录安全提问 44 45 private int m_templateid = 1; //默认论坛风格 46 private int m_hottopic = 20; //热门话题最低贴数 47 private int m_starthreshold = 2; //星星升级阀值 48 private int m_visitedforums = 20; //显示最近访问论坛数量 49 private int m_maxsigrows = 0; //最大签名高度(行) 50 private int m_moddisplay = 0; //版主显示方式 0=平面显示 1=下拉菜单 51 private int m_subforumsindex = 1; //首页是否显示论坛的下级子论坛 52 private int m_stylejump = 1; //显示风格下拉菜单 53 private int m_fastpost = 1; //快速发帖 54 private int m_showsignatures = 1; //是否显示签名 55 private int m_showavatars = 1; //是否显示头像 56 private int m_showimages = 1; //是否在帖子中显示图片 57 private int m_smiliesmax = 30; //帖子中最大允许的表情符数量 58 59 private int m_archiverstatus = 1; //启用 Archiver 60 private string m_seotitle = ""; //标题附加字 61 private string m_seokeywords = ""; //Meta Keywords 62 private string m_seodescription = ""; //Meta Description 63 private string m_seohead = "<meta name="generator" content="Discuz!NT" />"; //其他头部信息 64 65 private int m_rssstatus = 1; //rssstatus 66 private int m_rssttl = 60; //RSS TTL(分钟) 67 private int m_sitemapstatus = 1; //Sitemap是否开启 68 private int m_sitemapttl = 12; //Sitemap TTL(小时) 69 private int m_nocacheheaders = 0; //禁止浏览器缓冲 70 private int m_fullmytopics = 1; //我的话题全文搜索 0=只搜索用户是主题发表者的主题 1=搜索用户是主题发表者或回复者的主题 71 private int m_debug = 1; //显示程序运行信息 72 private string m_rewriteurl = ""; //伪静态url的替换规则 73 private string m_extname = ".aspx"; //伪静态url的扩展名 74 75 private int m_whosonlinestatus = 3; //显示在线用户 0=不显示 1=仅在首页显示 2=仅在分论坛显示 3=在首页和分论坛显示 76 private int m_maxonlinelist = 300; //最多显示在线人数 77 private int m_userstatusby = 1; //衡量并显示用户头衔 78 private int m_forumjump = 1; //显示论坛跳转菜单 79 private int m_modworkstatus = 1; //论坛管理工作统计 80 private int m_maxmodworksmonths = 3; //管理记录保留时间(月) 81 82 private string m_seccodestatus = "register.aspx,login.aspx"; //使用验证码的页面列表,用","分隔 例如:register.aspx,login.aspx 83 private int m_guestcachepagetimeout = 0; // 缓存游客页面的失效时间, 为0则不缓存, 大于0则缓存该值的时间(单位:分钟) 84 private int m_topiccachemark = 0; //缓存游客查看主题页面的权重, 为0则不缓存, 范围0 - 100 85 private int m_maxonlines = 5000; //最大在线人数 86 private int m_postinterval = 0; //发帖灌水预防(秒) 87 private int m_searchctrl = 0; //搜索时间限制(秒) 88 private int m_maxspm = 5; //60 秒最大搜索次数 89 90 private string m_visitbanperiods = ""; //禁止访问时间段 91 private string m_postbanperiods = ""; //禁止发帖时间段 92 private string m_postmodperiods = ""; //发帖审核时间段 93 private string m_attachbanperiods = ""; //禁止下载附件时间段 94 private string m_searchbanperiods = ""; //禁止全文搜索时间段 95 96 private int m_memliststatus = 1; //允许查看会员列表 97 private int m_dupkarmarate = 0; //允许重复评分 98 private int m_minpostsize = 1; //帖子最小字数(字节) 99 private int m_maxpostsize = 10000; //帖子最大字数(字节) 100 private int m_tpp = 26; //每页主题数 101 private int m_ppp = 16; //每页帖子数 102 private int m_maxfavorites = 30; //收藏夹容量 103 private int m_maxavatarsize = 20480; //头像最大尺寸(字节) 104 private int m_maxavatarwidth = 120; //头像最大宽度(像素) 105 private int m_maxavatarheight = 120; //头像最大高度(像素) 106 private int m_maxpolloptions = 10; //投票最大选项数 107 private int m_maxattachments = 10; //最大允许的上传附件数 108 109 private int m_attachimgpost = 1; //帖子中显示图片附件 110 private int m_attachrefcheck = 1; //下载附件来路检查 111 private int m_attachsave = 0; //附件保存方式 0=按年/月/日存入不同目录 1=按年/月/日/论坛存入不同目录 2=按论坛存入不同目录 3=按文件类型存入不同目录 112 private int m_watermarkstatus = 3; //图片附件添加水印 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下 113 private int m_watermarktype = 0; //图片附件添加何种水印 0=文字 1=图片 114 private int m_watermarktransparency = 5; //图片水印透明度 取值范围1--10 (10为不透明) 115 private string m_watermarktext = "DiscuzNT"; //图片附件添加文字水印的内容 {1}表示论坛标题 {2}表示论坛地址 {3}表示当前日期 {4}表示当前时间, 例如: {3} {4}上传于{1} {2} 116 private string m_watermarkpic = "watermark.gif"; //使用的水印图片的名称 117 private string m_watermarkfontname = "Tahoma"; //图片附件添加文字水印的字体 118 private int m_watermarkfontsize = 12; //图片附件添加文字水印的大小(像素) 119 private int m_showattachmentpath = 0; //图片附件如果直接显示, 地址是否直接使用图片真实路径 120 private int m_attachimgquality = 80; //是否是高质量图片 取值范围0--100 121 private int m_attachimgmaxheight = 0; //附件图片最大高度 122 private int m_attachimgmaxwidth = 0; //附件图片最大宽度 123 124 private int m_reasonpm = 0; //是否将管理操作的理由短消息通知作者 125 private int m_moderactions = 1; //是否在主题查看页面显示管理操作 126 private int m_karmaratelimit = 4; //评分时间限制(小时) 127 private int m_losslessdel = 200; //删贴不减积分时间期限(天) 128 private int m_edittimelimit = 10; //编辑帖子时间限制(分钟) 129 private int m_editedby = 1; //编辑帖子附加编辑记录 130 private int m_defaulteditormode = 1; //默认的编辑器模式 0=ubb代码编辑器 1=可视化编辑器 131 private int m_allowswitcheditor = 1; //是否允许切换编辑器模式 132 private int m_smileyinsert = 1; //显示可点击表情 133 private string m_cookiedomain = "";//身份验证Cookie域 134 135 private int m_passwordmode = 0; //密码模式, 0为默认(32位md5), 1为动网兼容模式(16位md5) 136 private int m_bbcodemode = 0; //UBB模式, 0为默认(标准Discuz!NT代码), 1为动网UBB代码兼容模式 137 private int m_fulltextsearch = 0; //是否启用SQLServer全文检索, 0为不使用, 1为使用 138 private int m_cachelog = 0; //是否使用缓存日志, 0为不使用, 1为使用 139 private int m_onlinetimeout = 10; //多久无动作视为离线 140 141 private int m_topicqueuestats = 0; //是否开启主题统计队列功能 142 private int m_topicqueuestatscount = 20; //主题统计队列长度(浏览量) 143 144 private int m_displayratecount = 100; //评分记录现实的最大数量 145 146 147 private string m_reportusergroup = "1"; //举报报告用户组 148 private string m_photomangegroups = ""; //图片管理用户组 149 150 private int m_silverlight = 0; //是否开启Silverlight功能 151 private int m_enablespace = 1; //是否启用个人空间服务,默认是启用 152 private int m_enablealbum = 1; //是否启用相册服务,默认是启用 153 154 private string m_spacename = "空间"; //空间名称 155 private string m_albumname = "相册"; //相册名称 156 private string m_spaceurl = "spaceindex.aspx"; //空间URL地址 157 private string m_albumurl = "albumindex.aspx"; //相册URL地址 158 private int m_browsecreatetemplate = 0; //浏览时如果模板不存在自动生成否,0为不使用,1为使用 159 160 private string m_ratevalveset = "1,50,200,600,800"; //评分阀值; 161 private int m_topictoblog = 1;//开通空间是转移过去的主题数 162 163 private int m_aspxrewrite = 1; //是否使用伪aspx, 如:showforum-1.aspx等. 164 private int m_viewnewtopicminute = 120; //设置前台"查看新帖"的分钟数 165 166 private int m_htmltitle = 0; //是否使用html标题 167 private string m_htmltitleusergroup = ""; //可以使用html标题的用户组 168 169 private bool m_specifytemplate = false; //版块是否指定模板 170 #endregion 171 172 相应的属性声明和绑定就不再罗列了 论坛全局配置类(GeneralConfigs):
1 /**//// <summary> 2 /// 论坛全局配置类 3 /// </summary> 4 public class GeneralConfigs 5 { 6 private static object lockHelper = new object(); 7 8 private static System.Timers.Timer generalConfigTimer = new System.Timers.Timer(15000); 9 10 private static GeneralConfigInfo m_configinfo; 11 12 /**//// <summary> 13 /// 静态构造函数初始化相应实例和定时器 14 /// </summary> 15 static GeneralConfigs() 16 { 17 m_configinfo = GeneralConfigFileManager.LoadConfig(); 18 19 generalConfigTimer.AutoReset = true; 20 generalConfigTimer.Enabled = true; 21 generalConfigTimer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed); 22 generalConfigTimer.Start(); 23 } 24 25 private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 26 { 27 ResetConfig(); 28 } 29 30 31 /**//// <summary> 32 /// 重设配置类实例 33 /// </summary> 34 public static void ResetConfig() 35 { 36 m_configinfo = GeneralConfigFileManager.LoadConfig(); 37 } 38 39 public static GeneralConfigInfo GetConfig() 40 { 41 return m_configinfo; 42 } 43 44 45 46 /**//// <summary> 47 /// 获取默认模板id 48 /// </summary> 49 /// <returns></returns> 50 public static int GetDefaultTemplateID() 51 { 52 return GetConfig().Templateid; 53 } 54 55 56 57 /**//// <summary> 58 /// 获得设置项信息 59 /// </summary> 60 /// <returns>设置项</returns> 61 public static bool SetIpDenyAccess(string denyipaccess) 62 { 63 bool result; 64 65 lock(lockHelper) 66 { 67 try 68 { 69 GeneralConfigInfo configInfo = GeneralConfigs.GetConfig(); 70 configInfo.Ipdenyaccess = configInfo.Ipdenyaccess + "n" + denyipaccess; 71 GeneralConfigs.Serialiaze(configInfo, Utils.GetMapPath(BaseConfigs.GetForumPath + "config/general.config")); 72 result = true; 73 } 74 catch 75 { 76 return false; 77 } 78 79 } 80 return result; 81 82 } 83 84 85 Helper#region Helper 86 87 /**//// <summary> 88 /// 序列化配置信息为XML 89 /// </summary> 90 /// <param name="configinfo">配置信息</param> 91 /// <param name="configFilePath">配置文件完整路径</param> 92 public static GeneralConfigInfo Serialiaze(GeneralConfigInfo configinfo, string configFilePath) 93 { 94 lock(lockHelper) 95 { 96 SerializationHelper.Save(configinfo, configFilePath); 97 } 98 return configinfo; 99 } 100 101 102 public static GeneralConfigInfo Deserialize(string configFilePath) 103 { 104 return (GeneralConfigInfo)SerializationHelper.Load(typeof(GeneralConfigInfo), configFilePath); 105 } 106 107 #endregion 108 } 109 110 通过对general系列配置类的介绍,再看其它的配置类会很清楚了。这里就不再多做解释了,相信2.0开源后
大家看到源码和注释后会有所感悟的。
另外就是之前因为用到了“序列化”,所以可能会出现所谓的“内存碎片”的问题,详见下面链接:
而微软官方所推荐的方式如下(http://support.microsoft.com/kb/886385/en-us):
1. Create one instance of the XmlSerializer class, and put that instance in the cache by using
the caching APIs. For example, for a .dll file that is named HighSchool, the following code
caches one instance of the XmlSerializer class:
1 XmlSerializer mySerializer = new XmlSerializer(typeof(HighSchool.MyClass), attrOverrides, 2 extraTypes, root, "http://www.microsoft.com"); 3 Cache["HighSchoolSerializer"] = mySerializer
2. Use the instance of the XmlSerializer class that you put in the cache instead of creating a
new XmlSerializer object every time.
Use the following XmlSerializer class constructors. These class constructors cache the
assemblies:
In the .NET Framework version 1.0
1public XmlSerializer(Type); 2
In the .NET Framework version 1.1
1 public XmlSerializer(Type type); 2 public XmlSerializer(Type type, string defaultNamespace); 3
3. Declare the XmlSerializer object to be a static member of a class.
(这种方式是我们项目中所采用的方案)
如果大家对这个话题感兴趣,恰恰园子里就有朋友做这方面的探索,链接如下:
http://www.cnblogs.com/lixiong/archive/2007/10/26/938430.html
好了,今天的内容到此就要结束了。希望大家能够支持和关注我们这个本土开源项目。祝大家工作顺利,
生活幸福,谢谢大家:)
源代码网供稿. |