• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.netForums之配置,缓存,多数据访问

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

在上一篇文章中,我谈到了asp.net Forums 之控件研究,这次我们来谈谈asp.net Forums 的配置、缓存和多数据访问。

一、配置

ANF的配置是通过ForumConfiguration类,ForumConfiguration类位于AspNetForums.Configuration命名空间下。

  

代码
#region VSS

/*
* $Header: /HiForums/Components/Configuration/ForumConfiguration.cs 1 05-10-26 15:04 Jacky $
*
* $History: ForumConfiguration.cs $
*
* ***************** Version 1 *****************
* User: Jacky Date: 05-10-26 Time: 15:04
* Created in $/HiForums/Components/Configuration
*
* ***************** Version 1 *****************
* User: Jacky Date: 05-10-21 Time: 14:20
* Created in $/HiForums/Components/Configuration
*
* ***************** Version 3 *****************
* User: Jacky Date: 05-09-20 Time: 19:23
* Updated in $/ASP.NET Forums/Components/Configuration
*
* ***************** Version 2 *****************
* User: Jacky Date: 05-09-20 Time: 1:36
* Updated in $/ASP.NET Forums/Components/Configuration
*/

#endregion

using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Web;
using System.Web.Caching;
using System.Xml;
using System.Configuration;
using System.Xml.Serialization;
using AspNetForums.Components;
using AspNetForums.Enumerations;

namespace AspNetForums.Configuration
{
// *********************************************************************
// ForumConfiguration
//
/// <summary>
/// 读取论坛配置信息,根据CS结构重构.
/// by venjiang 2005/10/25
/// </summary>
///
// ***********************************************************************/
public class ForumConfiguration
{
#region 成员字段
public static readonly string CacheKey = "ForumsConfiguration";
private Hashtable providers = new Hashtable();
private Hashtable extensions = new Hashtable();
private static readonly Cache cache = HttpRuntime.Cache;
private string defaultProvider;
private string defaultLanguage;
private string forumFilesPath;
private bool disableBackgroundThreads = false;
private bool disableIndexing = false;
private bool disableEmail = false;
private string passwordEncodingFormat = "unicode";
private int threadIntervalEmail = 15;
private int threadIntervalStats = 15;
//private SystemType systemType = SystemType.Self;
private short smtpServerConnectionLimit = -1;
private bool enableLatestVersionCheck = true;
private string uploadFilesPath = "/Upload/";
private XmlDocument XmlDoc = null;
#endregion

#region 构造器
public ForumConfiguration(XmlDocument doc)
{
XmlDoc
= doc;
LoadValuesFromConfigurationXml();
}
#endregion

#region 获取XML节点
public XmlNode GetConfigSection(string nodePath)
{
return XmlDoc.SelectSingleNode(nodePath);
}

#endregion
/*
public static ForumConfiguration GetConfig()
{
return (ForumConfiguration) ConfigurationSettings.GetConfig("forums/forums");
}
*/
#region 获取配置信息实例
public static ForumConfiguration GetConfig()
{
ForumConfiguration config
= cache.Get(CacheKey) as ForumConfiguration;
if(config == null)
{
string path;
if(HttpContext.Current != null)
path
= HttpContext.Current.Server.MapPath("~/Forums.config");
else
path
= Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "Forums.config";

XmlDocument doc
= new XmlDocument();
doc.Load(path);
config
= new ForumConfiguration(doc);
cache.Insert(CacheKey,config,
new CacheDependency(path),DateTime.MaxValue,TimeSpan.Zero,CacheItemPriority.AboveNormal,null);

//cache.ReSetFactor(config.CacheFactor);
}
return config;

}
#endregion

#region 加载配置文件设置
internal void LoadValuesFromConfigurationXml()
{
XmlNode node
= GetConfigSection("Forums/Core");
XmlAttributeCollection attributeCollection
= node.Attributes;

// 默认数据提供者
XmlAttribute att = attributeCollection["defaultProvider"];
if(att != null)
defaultProvider
= att.Value;
// 默认语言
att = attributeCollection["defaultLanguage"];
if(att != null)
defaultLanguage
=att.Value;
else
defaultLanguage
= "zh-CN";
// 论坛路径
att = attributeCollection["forumFilesPath"];
if(att != null)
forumFilesPath
= att.Value;
else
forumFilesPath
= "/";
// 禁用后台线程
att = attributeCollection["disableThreading"];
if(att != null)
disableBackgroundThreads
= bool.Parse(att.Value);
else
disableBackgroundThreads
= false;
// 禁止索引
att = attributeCollection["disableIndexing"];
if(att != null)
disableIndexing
= bool.Parse(att.Value);
else
disableIndexing
= false;
// 禁止邮件
att = attributeCollection["disableEmail"];
if(att != null)
disableEmail
= bool.Parse(att.Value);
else
disableEmail
= false;
// 密码加密格式
att = attributeCollection["passwordEncodingFormat"];
if(att != null)
passwordEncodingFormat
= att.Value;
else
passwordEncodingFormat
= "unicode";
// 后中统计状态
att = attributeCollection["threadIntervalStats"];
if(att != null)
threadIntervalStats
= int.Parse(att.Value);
else
threadIntervalStats
= 15;
// 邮件队列发送间隔
att = attributeCollection["threadIntervalEmail"];
if(att != null)
threadIntervalEmail
= int.Parse(att.Value);
else
threadIntervalEmail
= 15;
// SMTP服务器设置
att = attributeCollection["smtpServerConnectionLimit"];
if(att != null)
smtpServerConnectionLimit
= short.Parse(att.Value);
else
smtpServerConnectionLimit
= -1;
// 版本检查
att = attributeCollection["enableLatestVersionCheck"];
if(att != null)
enableLatestVersionCheck
= bool.Parse(att.Value);
// 上传文件路径
att = attributeCollection["uploadFilesPath"];
if(att != null)
uploadFilesPath
= att.Value;

// 读取子节点
foreach (XmlNode child in node.ChildNodes)
{
if (child.Name == "providers")
GetProviders(child, providers);

if (child.Name == "extensionModules")
GetProviders(child, extensions);

}
}

#endregion

#region 获取配置文件节点Provider
internal void GetProviders(XmlNode node, Hashtable table)
{
foreach (XmlNode provider in node.ChildNodes)
{
switch (provider.Name)
{
case "add":
table.Add(provider.Attributes[
"name"].Value, new Provider(provider.Attributes));
break;

case "remove":
table.Remove(provider.Attributes[
"name"].Value);
break;

case "clear":
table.Clear();
break;

}

}

}
#endregion

#region 公有属性
// Properties
//
public string DefaultLanguage
{
get { return defaultLanguage; }
}

public string ForumFilesPath
{
get { return forumFilesPath; }
}

public string DefaultProvider
{
get { return defaultProvider; }
}

public Hashtable Providers
{
get { return providers; }
}

public Hashtable Extensions
{
get { return extensions; }
}

public bool IsBackgroundThreadingDisabled
{
get { return disableBackgroundThreads; }
}

public bool IsIndexingDisabled
{
get { return disableIndexing; }
}

public string PasswordEncodingFormat
{
get { return passwordEncodingFormat; }
}

public string UploadFilesPath
{
get { return uploadFilesPath; }
}

public bool IsEmailDisabled
{
get { return disableEmail; }
}

public int ThreadIntervalEmail
{
get { return threadIntervalEmail; }
}

public int ThreadIntervalStats
{
get { return threadIntervalStats; }
}

public short SmtpServerConnectionLimit
{
get { return smtpServerConnectionLimit; }
}

public bool EnableLatestVersionCheck
{
get { return enableLatestVersionCheck; }
}
#endregion
}

/// <summary>
/// 数据提供者实体类
/// </summary>
public class Provider
{
#region 成员字段
private string name;
private string providerType;
private ExtensionType extensionType;
private NameValueCollection providerAttributes = new NameValueCollection();
#endregion

#region 构造器
public Provider(XmlAttributeCollection attributes)
{
// Set the name of the provider
//
name = attributes["name"].Value;

// Set the extension type
//
try
{
extensionType
= (ExtensionType)Enum.Parse(typeof(ExtensionType), attributes["extensionType"].Value, true);
}
catch
{
// Occassionally get an exception on parsing the extensiontype, so set it to Unknown
extensionType = ExtensionType.Unknown;
}

providerType
= attributes["type"].Value;

// Store all the attributes in the attributes bucket
//
foreach (XmlAttribute attribute in attributes)
{
if ((attribute.Name != "name") && (attribute.Name != "type"))
providerAttributes.Add(attribute.Name, attribute.Value);

}

}
#endregion

#region 公有属性
public string Name
{
get { return name; }
}

public string Type
{
get { return providerType; }
}

public ExtensionType ExtensionType
{
get { return extensionType; }
}


public NameValueCollection Attributes
{
get { return providerAttributes; }
}

#endregion
}
}
// *********************************************************************
// ForumsConfigurationHandler
//
/// <summary>
/// Class used by ASP.NET Configuration to load ASP.NET Forums configuration.
/// </summary>
///
// ***********************************************************************/
/*
internal class ForumsConfigurationHandler : IConfigurationSectionHandler
{
public virtual object Create(Object parent, Object context, XmlNode node)
{
ForumConfiguration config = new ForumConfiguration();
config.LoadValuesFromConfigurationXml(node);
return config;
}

}
*/

 

 

这个配置文件配置了ANF的默认数据提供者、默认语言、论坛路径、是滞禁用后台线程、是滞禁止索引、是否禁止邮件、密码加密格式、线程间隔状态、邮件队列发送间隔、SMTP服务器设置、版本检查
上传文件路径、数据提供者、自定义HttpModule等。主要方法为GetConfig():

 

代码
public static ForumConfiguration GetConfig()
{
ForumConfiguration config
= cache.Get(CacheKey) as ForumConfiguration;
if(config == null)
{
string path;
if(HttpContext.Current != null)
path
= HttpContext.Current.Server.MapPath("~/Forums.config");
else
path
= Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "Forums.config";

XmlDocument doc
= new XmlDocument();
doc.Load(path);
config
= new ForumConfiguration(doc);
cache.Insert(CacheKey,config,
new CacheDependency(path),DateTime.MaxValue,TimeSpan.Zero,CacheItemPriority.AboveNormal,null);

//cache.ReSetFactor(config.CacheFactor);
}
return config;

}

 

 

 

在此方法中,首先从Cache缓存中获取配置信息,如配置信息还未缓存,则读敢配置文件。配置文件位于网站根目录下的Forums.config文件。读取配置文件的内容后插入缓存中以便下次使用。
Forums.config文件如下:

 

代码
<?xml version="1.0" encoding="utf-8" ?>
<Forums>
<Core defaultLanguage="zh-CN"
forumFilesPath
="/"
disableEmail
="false"
disableIndexing
="true"
disableThreading
="false"
threadIntervalStats
="15"
threadIntervalEmail
="15"
smtpServerConnectionLimit
="-1"
passwordEncodingFormat
="unicode"
enableLatestVersionCheck
="false"
uploadFilesPath
="/Upload/"
defaultProvider
="SqlForumsProvider">
<providers>
<clear />
<

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
.NETCore、.NETStandard、ASP.NETCore和.NETFramework有什么不同?发布时间:2022-07-10
下一篇:
asp.net前台javascript与后台c#相互调用发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap