ClassRUN_XZ.cs
上传用户:zhangkuixh
上传日期:2013-09-30
资源大小:5473k
文件大小:11k
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections;
- /*
- ' 迅龙中文分类搜索引擎 v0.6
- '
- ' LGPL 许可发行
- '
- ' 宁夏大学 张冬 康彩 zd4004@163.com
- '
- ' 官网 http://blog.163.com/zd4004/
- */
- namespace XunLong.NewWebServer
- {
- /// <summary>
- /// 任务处理器
- /// </summary>
- public static class ClassRUN_XZ
- {
- /// <summary>
- /// 帮定的地址 不含 http://
- /// </summary>
- public static string HTTPURL="000";
- /// <summary>
- /// 设定清除 缓存的阀值 当每次清除以后 仍然很多时 阀值自动增加1
- /// </summary>
- private static int clearNumGate_1 = 1;
- /// <summary>
- /// 设定清除 缓存的阀值 当每次清除以后 仍然很多时 阀值自动增加1
- /// </summary>
- private static int clearNumGate_2 = 1;
- /// <summary>
- /// One 1 的缓存个数
- /// </summary>
- private static int One1_ALL_NUM = 100000;
- /// <summary>
- /// One 2 的缓存个数
- /// </summary>
- private static int One2_ALL_NUM = 50000;
- /// <summary>
- /// 头和属性 one1 缓存 key = md5 url val = dat
- /// </summary>
- private static Hashtable one1Cache = new Hashtable();
- /// <summary>
- /// 数据 one2 缓存 key = md5 url val = dat
- /// </summary>
- private static Hashtable one2Cache = new Hashtable();
- /// <summary>
- /// T 头和属性 one1 缓存 key = md5 url val = dat
- /// </summary>
- private static Hashtable one1Cache_t = new Hashtable();
- /// <summary>
- /// T 数据 one2 缓存 key = md5 url val = dat
- /// </summary>
- private static Hashtable one2Cache_t = new Hashtable();
- /// <summary>
- /// 数据缓存1 缓存清理 Topz 缓存项目设定为 5000 项 key = url val = time 清理时 去掉最小的前 500 个
- /// val 记录系统毫秒数 如果访问 则更新次毫秒数 此种方法可以使最常用的搜索结果得以保留
- /// </summary>
- // private static Hashtable timeouttopzCache = new Hashtable();
- /// <summary>
- /// 数据缓存2 缓存清理 One2 缓存项目设定为 5000 项 key = url val = time 清理时 去掉最小的前 500 个
- /// </summary>
- // private static Hashtable timeoutone2Cache = new Hashtable();
- /// <summary>
- /// 得到HTM数据
- /// </summary>
- private static XunLong.SearchClassLibrary.ClassGetHAST2HTM Hast2Htm = new XunLong.SearchClassLibrary.ClassGetHAST2HTM();
- /// <summary>
- /// 设定 HTTPURL
- /// </summary>
- /// <param name="HTTPURL_1"></param>
- public static void SetHTTPURL(string HTTPURL_1)
- {
- HTTPURL = HTTPURL_1;
- }
- public static void Init(string Config_path)
- {
- // topzCache.Clear();
- one1Cache.Clear();
- one2Cache.Clear();
- one1Cache_t.Clear();
- one2Cache_t.Clear();
- // timeoutone2Cache.Clear();
- // timeouttopzCache.Clear();
- Hast2Htm.init(Config_path);
- Hast2Htm.HTTPURL = HTTPURL;
- }
- /// <summary>
- /// 初始搜索
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- public static string SearchGet(string sWord, string mainType)
- {
- Hast2Htm.HTTPURL = HTTPURL;
- string urlOne1 = "one1?wd="+ sWord + "<nn>1</nn>" + mainType; //根据搜索词 得到第一个显示页面 链接
- string urlOne2 = "one2?wd=" + sWord + mainType; //得到头 链接 /得到 主分类 链接 属性
- // dat =<XL>关键词</XL>;
- string data_source = "";
- int one_a1 = sWord.IndexOf("<xl>");
- int one_a2 = sWord.IndexOf("</xl>");
- if (one_a2 > one_a1 & one_a1 > -1)
- {
- data_source = sWord.Substring(one_a1 + 4, one_a2 - one_a1 - 4);
- }
- string mainTypeOne = mainType.Replace("&czd=", "");
- string a = "";
- a = a + "<html>" + "rn";
- a = a + "<head>" + "rn";
- a = a + "<meta http-equiv="Content-Type" content="text/html; charset=gb2312">" + "rn";
- a = a + "<title>列表网 www.liebiao.net -- "+ mainTypeOne +" >> "+ data_source+"</title>" + "rn";
- a = a + "</head>" + "rn";
- a = a + "<frameset rows="100,*" cols="*" frameborder="NO" border="0" framespacing="0">" + "rn";
- a = a + "<frame src=""+urlOne2+"" name="one2" scrolling="NO" noresize >" + "rn";
- a = a + "<frame src="" +urlOne1+"" name="one1">" + "rn";
- a = a + "</frameset>" + "rn";
- a = a + "<noframes><body>" + "rn";
- a = a + "</body></noframes>" + "rn";
- a = a + "</html>" + "rn";
- return a;
- }
- /// <summary>
- /// 得到分页显示的数据
- /// one1?wd=word&n=1 搜索词和页码
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- public static string GetOne1HTM(string url, string mainType)
- {
- Hast2Htm.HTTPURL = HTTPURL;
- string md5url = getMD5name(url);
- try
- {
- if (one1Cache_t.Contains(md5url) == true)
- {
- Console.WriteLine("READ CACHE 1>> ALL-NUM:" + one1Cache.Count.ToString());
- int v_int = (int)one1Cache_t[md5url];
- one1Cache_t[md5url] = v_int + 1;
- return one1Cache[md5url].ToString();
- }
- }
- catch
- {
- Console.WriteLine("缓存1读取冲突");
- }
- string[] a = url.Split('=');
- string tt = a[1].Trim();
- if (tt.Length == 0)
- {
- return "NO FOUND";
- }
- // dat =<nn>5</nn>;
- string data_num = "";
- int one_a1 = tt.IndexOf("<nn>");
- int one_a2 = tt.IndexOf("</nn>");
- if (one_a2 > one_a1 & one_a1 > -1)
- {
- data_num = tt.Substring(one_a1 + 4, one_a2 - one_a1 - 4);
- tt = tt.Replace("<nn>"+ data_num+"</nn>","");
- }
- else
- {
- data_num = "1";
- }
- int u = Int32.Parse(data_num);
- // 检索得到数据
- string B_K = Hast2Htm.GetData(tt, u, mainType);
- try
- {
- if (one1Cache_t.Contains(md5url) == false)
- {
- ClearCache1();
- one1Cache.Add(md5url, B_K);
- one1Cache_t.Add(md5url, 0);
-
- }
- }
- catch
- {
- Console.WriteLine("缓存1写入冲突");
- }
- return B_K;
- }
- /// <summary>
- /// 得到可选列表数据 one2?wd=word word = 搜索词+主类别 只有搜索词是 得到主类别 有主类别和搜索词时 得到 属性
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- public static string GetOne2HTM(string url, string mainType)
- {
- Hast2Htm.HTTPURL = HTTPURL;
- //得到搜索词
- string[] x = url.Split('=');
- string sWord = x[1];
- string md5url = getMD5name(url);
- try
- {
- if (one2Cache_t.Contains(md5url) == true)
- {
- Console.WriteLine("READ CACHE 2>> ALL-NUM:" + one2Cache.Count.ToString());
- int v_int = (int)one2Cache_t[md5url];
- one2Cache_t[md5url] = v_int + 1;
- return one2Cache[md5url].ToString();
- }
- }
- catch
- {
- Console.WriteLine("缓存2读取冲突");
- }
- // 检索得到数据
- string BK = "";
- BK = Hast2Htm.GetRESLIST(sWord, mainType);
- try
- {
- if (one2Cache.Contains(md5url) == false)
- {
- ClearCache2();
- one2Cache.Add(md5url, BK);
- one2Cache_t.Add(md5url,0);
- }
- }
- catch
- {
- Console.WriteLine("缓存2写入冲突");
- }
- return BK;
- }
- /// <summary>
- /// 清理缓存 1
- /// </summary>
- public static void ClearCache1()
- {
- try
- {
- if (one1Cache_t.Count > One1_ALL_NUM )
- {
- ArrayList na = new ArrayList();
- na.Clear();
- //清理
- foreach (DictionaryEntry aaa in one1Cache_t)
- {
- int aint = (int)aaa.Value;
- if (aint <= clearNumGate_1)
- {
- na.Add(aaa.Key);
- }
- }
- foreach (string aaamd5 in na)
- {
- one1Cache_t.Remove(aaamd5);
- one1Cache.Remove(aaamd5);
- }
- if( one1Cache_t.Count > (One1_ALL_NUM - 10000))
- {
- clearNumGate_1 =clearNumGate_1 +1;
- }
- }
- }
- catch
- {
- Console.WriteLine("清理缓存1 冲突");
- }
-
- }
- /// <summary>
- /// 清理缓存 2
- /// </summary>
- public static void ClearCache2()
- {
- try
- {
- if (one2Cache_t.Count > One2_ALL_NUM)
- {
- ArrayList na = new ArrayList();
- na.Clear();
- //清理
- foreach (DictionaryEntry aaa in one2Cache_t)
- {
- int aint = (int)aaa.Value;
- if (aint <= clearNumGate_2)
- {
- na.Add(aaa.Key);
- }
- }
- foreach (string aaamd5 in na)
- {
- one2Cache_t.Remove(aaamd5);
- one2Cache.Remove(aaamd5);
- }
- if (one2Cache_t.Count > (One2_ALL_NUM - 5000))
- {
- clearNumGate_2 = clearNumGate_2 + 1;
- }
- }
- }
- catch
- {
- Console.WriteLine("清理缓存1 冲突");
- }
- }
- /// <summary>
- /// 得到URL的MD5名
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- private static string getMD5name(string url)
- {
- string strMd5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(url, "md5");
- return strMd5;
- }
- }
- }