110,539
社区成员
发帖
与我相关
我的任务
分享
public class TemplateRuleBuilder
{
public TemplateRuleBuilder(int LevelID, string Page)
{
this.page = Page;
this.path = string.Format("/Template/{0}/{1}.html", LevelID, Page);
isCache = GetSetting(LevelID.ToString()) == "true" ? true : false;
cacheKey = isCache ? GetSetting(LevelID + CacheKeyPara) + Page : "";
}
public string GetSetting(string key)
{
return ConfigurationManager.AppSettings[key];
}
private const string CacheKeyPara = "CacheKey";
private bool isCache;
/// <summary>
/// 是否需要缓存
/// </summary>
public bool IsCache
{
get { return isCache; }
set { isCache = value; }
}
private string cacheKey;
/// <summary>
/// 缓存键值
/// </summary>
public string CacheKey
{
get { return cacheKey; }
set { cacheKey = value; }
}
private string path;
/// <summary>
/// 模版路径
/// </summary>
public string Path
{
get { return path; }
set { path = value; }
}
private string page;
/// <summary>
/// 模版文件名
/// </summary>
public string Page
{
get { return page; }
set { page = value; }
}
}
public class TemplateRule
{
public TemplateRule(int LevelID, string Page)
{
trb = new TemplateRuleBuilder(LevelID, Page);
}
/// <summary>
/// 模板相关属性
/// </summary>
private TemplateRuleBuilder trb;
/// <summary>
/// 获取模版
/// </summary>
/// <returns></returns>
public string GetTemplate()
{
string template = string.Empty;
if (!File.Exists(HttpContext.Current.Server.MapPath(trb.Path)))
return template;
if (trb.IsCache)
{
if (HttpContext.Current.Cache[trb.CacheKey] == null)
HttpContext.Current.Cache.Insert(trb.CacheKey, ReadTemplate(), null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(20));
template = HttpContext.Current.Cache[trb.CacheKey].ToString();
}
else
template = ReadTemplate();
return template;
}
/// <summary>
/// 读取模版
/// </summary>
/// <returns></returns>
protected string ReadTemplate()
{
string result = string.Empty;
StreamReader sr = null;
try
{
sr = new StreamReader(HttpContext.Current.Server.MapPath(trb.Path), Encoding.GetEncoding("gb2312"));
result = sr.ReadToEnd();
}
catch
{
}
finally
{
if (sr != null)
{
sr.Close();
sr.Dispose();
}
}
return result;
}
}
public class PageFactory
{
private string page;
private string template;
private Model.CompanyTbl model;
public PageFactory(string page, string template, Model.CompanyTbl model)
{
this.page = page;
this.template = template;
this.model = model;
}
public delegate string PageDelegate();
public string GetPage()
{
if (template.Length == 0)
return template;
PageHandler ph = new PageHandler(template, model);
System.Reflection.MethodInfo mi = ph.GetType().GetMethod("Get" + page, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
PageDelegate pd = (PageDelegate)Delegate.CreateDelegate(typeof(PageDelegate), ph, mi, false);
if (pd == null)
return string.Empty;
return pd();
}
}
public class TemplateRule
{
public string CacheKey;
public boll IsCache;
public string Path;
public string Page;
///...
}
public Class TemplateRuleBuilder :TemplateRule
{
public TemplateRuleBuilder(string CacheKey,bool IsCache,string Path,string Page)
{
//....
}
}
public class TemplateRuleBuilder
{
public TemplateRule GetTemplateRule()
{
string page = ...;
string path = string.Format("/Template/{0}/{1}.html", LevelID, Page);
bool isCache = GetSetting(LevelID.ToString()) == "true" ? true : false;
string cacheKey = isCache ? GetSetting(LevelID + CacheKeyPara) + Page : "";
return new TemplateRule(page,path,iscache...)
}
}
//Stopwatch sw = new Stopwatch();
//Program p = new Program();
//MethodInfo mi = p.GetType().GetMethod("Test");
//sw.Start();
//for (int i = 0; i < 1000000; i++)
//{
// if (mi != null)
// mi.Invoke(p, new object[] { 1 });
//}
//sw.Stop();
//Console.WriteLine(sw.ElapsedMilliseconds);
Stopwatch sw = new Stopwatch();
Program p = new Program();
MethodInfo mi = p.GetType().GetMethod("Test");
sw.Start();
MyDelegate my;
for (int i = 0; i < 1000000; i++)
{
my = (MyDelegate)Delegate.CreateDelegate(typeof(MyDelegate), p, mi, false);
my(2);
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
public TemplateRuleBuilder(bool isCache, string CacheKey, string Path, string Page)
{
this.isCache = isCache;
this.cacheKey = CacheKey;
this.path = Path;
this.page = Page;
}
public TemplateRule(TemplateRuleBuilder trb)
{
this.trb = trb;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString["ID"]) && Common.RegexCommon.IsNumber(Request.QueryString["ID"]))
{
//...
TemplateRule tr; string template = string.Empty;
if (model.CompPassed > 0)
tr = new TemplateRule(GetBuilder(model.LevelID, Page));
else
tr = new TemplateRule(GetBuilder(10000, Page));
template = tr.GetTemplate();
PageFactory pf = new PageFactory(Page, template, model);
Response.Write(pf.GetPage());
}
}
}
public TemplateRuleBuilder GetBuilder(int LevelID, string Page)
{
TemplateRuleBuilder trb;
bool isCache = GetSetting(LevelID.ToString()) == "true" ? true : false;
string path = string.Format("/Template/{0}/{1}.html", LevelID, Page);
string cacheKey = isCache ? GetSetting(LevelID + "CacheKey") + Page : "";
trb = new TemplateRuleBuilder(isCache, cacheKey, path, Page);
return trb;
}
public string GetSetting(string key)
{
return ConfigurationManager.AppSettings[key];
}