ClassRUN_XZ.cs
上传用户:zhangkuixh
上传日期:2013-09-30
资源大小:5473k
文件大小:11k
源码类别:

搜索引擎

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. /*
  6.       '       迅龙中文分类搜索引擎  v0.6
  7.       '
  8.       '        LGPL  许可发行
  9.       '
  10.       '       宁夏大学  张冬 康彩  zd4004@163.com
  11.       ' 
  12.       '        官网 http://blog.163.com/zd4004/
  13.  */
  14. namespace XunLong.NewWebServer
  15. {
  16.     /// <summary>
  17.     /// 任务处理器
  18.     /// </summary>
  19.     public static   class ClassRUN_XZ
  20.     {
  21.         /// <summary>
  22.         /// 帮定的地址 不含 http://
  23.         /// </summary>
  24.         public static string HTTPURL="000";
  25.         /// <summary>
  26.         /// 设定清除 缓存的阀值 当每次清除以后 仍然很多时 阀值自动增加1
  27.         /// </summary>
  28.         private static int clearNumGate_1 = 1;
  29.         /// <summary>
  30.         /// 设定清除 缓存的阀值 当每次清除以后 仍然很多时 阀值自动增加1
  31.         /// </summary>
  32.         private static int clearNumGate_2 = 1;
  33.         /// <summary>
  34.         /// One 1 的缓存个数
  35.         /// </summary>
  36.         private static int One1_ALL_NUM = 100000;
  37.         /// <summary>
  38.         /// One 2 的缓存个数
  39.         /// </summary>
  40.         private static int One2_ALL_NUM = 50000;
  41.         /// <summary>
  42.         ///  头和属性 one1 缓存   key = md5 url  val = dat
  43.         /// </summary>
  44.         private  static   Hashtable   one1Cache = new Hashtable();
  45.         /// <summary>
  46.         ///  数据 one2 缓存   key = md5 url  val = dat
  47.         /// </summary>
  48.         private static Hashtable one2Cache = new Hashtable();
  49.         /// <summary>
  50.         /// T 头和属性 one1 缓存   key = md5 url  val = dat
  51.         /// </summary>
  52.         private static Hashtable one1Cache_t = new Hashtable();
  53.         /// <summary>
  54.         /// T 数据 one2 缓存   key = md5 url  val = dat
  55.         /// </summary>
  56.         private static Hashtable one2Cache_t = new Hashtable();
  57.         /// <summary>
  58.         /// 数据缓存1   缓存清理 Topz 缓存项目设定为 5000 项    key = url  val = time  清理时 去掉最小的前 500 个
  59.         /// val 记录系统毫秒数  如果访问 则更新次毫秒数 此种方法可以使最常用的搜索结果得以保留
  60.         /// </summary>
  61.        //  private  static  Hashtable    timeouttopzCache = new Hashtable();
  62.         /// <summary>
  63.         /// 数据缓存2 缓存清理 One2 缓存项目设定为 5000 项      key = url  val = time  清理时 去掉最小的前 500 个
  64.         /// </summary>
  65.       //  private static Hashtable timeoutone2Cache = new Hashtable();
  66.         /// <summary>
  67.         /// 得到HTM数据
  68.         /// </summary>
  69.        private  static  XunLong.SearchClassLibrary.ClassGetHAST2HTM Hast2Htm = new XunLong.SearchClassLibrary.ClassGetHAST2HTM();
  70.         /// <summary>
  71.        /// 设定 HTTPURL 
  72.         /// </summary>
  73.         /// <param name="HTTPURL_1"></param>
  74.         public static void SetHTTPURL(string HTTPURL_1)
  75.         {
  76.             HTTPURL = HTTPURL_1;
  77.         }
  78.         public static  void Init(string Config_path)
  79.         {
  80.           //  topzCache.Clear();
  81.             one1Cache.Clear();
  82.             one2Cache.Clear();
  83.             one1Cache_t.Clear();
  84.             one2Cache_t.Clear();
  85.          //   timeoutone2Cache.Clear();
  86.          //   timeouttopzCache.Clear();
  87.             Hast2Htm.init(Config_path);
  88.             Hast2Htm.HTTPURL = HTTPURL;
  89.         }
  90.         /// <summary>
  91.         /// 初始搜索 
  92.         /// </summary>
  93.         /// <param name="url"></param>
  94.         /// <returns></returns>
  95.         public static string SearchGet(string sWord, string mainType)
  96.         {
  97.             Hast2Htm.HTTPURL = HTTPURL;
  98.             string urlOne1 = "one1?wd="+ sWord  + "<nn>1</nn>" + mainType; //根据搜索词 得到第一个显示页面  链接
  99.             string urlOne2 = "one2?wd=" + sWord + mainType;     //得到头  链接 /得到 主分类  链接 属性 
  100.             // dat =<XL>关键词</XL>;
  101.             string data_source = "";
  102.             int one_a1 = sWord.IndexOf("<xl>");
  103.             int one_a2 = sWord.IndexOf("</xl>");
  104.             if (one_a2 > one_a1 & one_a1 > -1)
  105.             {
  106.                 data_source = sWord.Substring(one_a1 + 4, one_a2 - one_a1 - 4);
  107.             }
  108.             string mainTypeOne = mainType.Replace("&czd=", "");
  109.             string a = "";
  110.             a = a + "<html>" + "rn";
  111.             a = a + "<head>" + "rn";
  112.             a = a + "<meta http-equiv="Content-Type" content="text/html; charset=gb2312">" + "rn";
  113.             a = a + "<title>列表网  www.liebiao.net  -- "+ mainTypeOne +" >> "+  data_source+"</title>" + "rn";
  114.             a = a + "</head>" + "rn";
  115.             a = a + "<frameset rows="100,*" cols="*" frameborder="NO" border="0" framespacing="0">" + "rn";
  116.             a = a +   "<frame src=""+urlOne2+"" name="one2" scrolling="NO" noresize >" + "rn";
  117.             a = a +   "<frame src="" +urlOne1+""  name="one1">" + "rn";
  118.             a = a + "</frameset>" + "rn";
  119.             a = a + "<noframes><body>" + "rn";
  120.             a = a + "</body></noframes>" + "rn";
  121.             a = a + "</html>" + "rn";
  122.             return a;
  123.         }
  124.         /// <summary>
  125.         /// 得到分页显示的数据
  126.         /// one1?wd=word&n=1  搜索词和页码
  127.         /// </summary>
  128.         /// <param name="url"></param>
  129.         /// <returns></returns>
  130.         public static string GetOne1HTM(string url, string mainType)
  131.         {
  132.             Hast2Htm.HTTPURL = HTTPURL;
  133.             string md5url = getMD5name(url);
  134.             try
  135.             {
  136.                 if (one1Cache_t.Contains(md5url) == true)
  137.                 {
  138.                     Console.WriteLine("READ CACHE 1>> ALL-NUM:" + one1Cache.Count.ToString());
  139.                     int v_int = (int)one1Cache_t[md5url];
  140.                     one1Cache_t[md5url] = v_int + 1;
  141.                     return one1Cache[md5url].ToString();
  142.                 }
  143.             }
  144.             catch
  145.             {
  146.                 Console.WriteLine("缓存1读取冲突");            
  147.             }
  148.             string[] a = url.Split('=');
  149.             string tt = a[1].Trim();
  150.             if (tt.Length == 0)
  151.             {
  152.                 return "NO FOUND";
  153.             }
  154.             // dat =<nn>5</nn>;
  155.             string data_num = "";
  156.             int one_a1 = tt.IndexOf("<nn>");
  157.             int one_a2 = tt.IndexOf("</nn>");
  158.             if (one_a2 > one_a1 & one_a1 > -1)
  159.             {
  160.                 data_num = tt.Substring(one_a1 + 4, one_a2 - one_a1 - 4);
  161.                 tt = tt.Replace("<nn>"+ data_num+"</nn>","");
  162.             }
  163.             else
  164.             {
  165.                 data_num = "1";
  166.             }
  167.             int u = Int32.Parse(data_num);
  168.             // 检索得到数据
  169.             string B_K = Hast2Htm.GetData(tt, u, mainType);
  170.             try
  171.             {
  172.                 if (one1Cache_t.Contains(md5url) == false)
  173.                 {
  174.                     ClearCache1();
  175.                     one1Cache.Add(md5url, B_K);
  176.                     one1Cache_t.Add(md5url, 0);
  177.                   
  178.                 }
  179.             }
  180.             catch
  181.             {
  182.                 Console.WriteLine("缓存1写入冲突");
  183.             }
  184.             return B_K;
  185.         }
  186.         /// <summary>
  187.         /// 得到可选列表数据   one2?wd=word  word = 搜索词+主类别  只有搜索词是 得到主类别 有主类别和搜索词时 得到 属性
  188.         /// </summary>
  189.         /// <param name="url"></param>
  190.         /// <returns></returns>
  191.         public static string GetOne2HTM(string url, string mainType)
  192.         {
  193.             Hast2Htm.HTTPURL = HTTPURL;
  194.             //得到搜索词
  195.             string[] x = url.Split('=');
  196.             string sWord = x[1];
  197.             string md5url = getMD5name(url);
  198.             try
  199.             {
  200.                 if (one2Cache_t.Contains(md5url) == true)
  201.                 {
  202.                     Console.WriteLine("READ CACHE 2>> ALL-NUM:" + one2Cache.Count.ToString());
  203.                     int v_int = (int)one2Cache_t[md5url];
  204.                     one2Cache_t[md5url] = v_int + 1;
  205.                     return one2Cache[md5url].ToString();
  206.                 }
  207.             }
  208.             catch
  209.             {
  210.                 Console.WriteLine("缓存2读取冲突");  
  211.             }
  212.             // 检索得到数据
  213.             string BK = "";
  214.             BK = Hast2Htm.GetRESLIST(sWord, mainType);
  215.             try
  216.             {
  217.                 if (one2Cache.Contains(md5url) == false)
  218.                 {
  219.                     ClearCache2();
  220.                     one2Cache.Add(md5url, BK);
  221.                     one2Cache_t.Add(md5url,0);
  222.                 }
  223.             }
  224.             catch
  225.             {
  226.                 Console.WriteLine("缓存2写入冲突");
  227.             }
  228.             return BK;
  229.         }
  230.         /// <summary>
  231.         /// 清理缓存 1
  232.         /// </summary>
  233.         public static  void ClearCache1()
  234.         {
  235.             try
  236.             {
  237.                 if (one1Cache_t.Count >  One1_ALL_NUM )
  238.                 {
  239.                     ArrayList na = new ArrayList();
  240.                     na.Clear();
  241.                     //清理
  242.                     foreach (DictionaryEntry aaa in one1Cache_t)
  243.                     {
  244.                         int aint = (int)aaa.Value;
  245.                         if (aint <= clearNumGate_1)
  246.                         {
  247.                             na.Add(aaa.Key);
  248.                         }                    
  249.                     }
  250.                     foreach (string aaamd5 in na)
  251.                     {
  252.                         one1Cache_t.Remove(aaamd5);
  253.                         one1Cache.Remove(aaamd5);                
  254.                     }
  255.                     if( one1Cache_t.Count >  (One1_ALL_NUM - 10000))
  256.                     {
  257.                        clearNumGate_1 =clearNumGate_1 +1;
  258.                     }
  259.                 }
  260.             }
  261.             catch
  262.             {
  263.                 Console.WriteLine("清理缓存1 冲突");
  264.             }   
  265.    
  266.         }
  267.         /// <summary>
  268.         /// 清理缓存 2
  269.         /// </summary>
  270.         public static void ClearCache2()
  271.         {
  272.             try
  273.             {
  274.                 if (one2Cache_t.Count > One2_ALL_NUM)
  275.                 {
  276.                     ArrayList na = new ArrayList();
  277.                     na.Clear();
  278.                     //清理
  279.                     foreach (DictionaryEntry aaa in one2Cache_t)
  280.                     {
  281.                         int aint = (int)aaa.Value;
  282.                         if (aint <= clearNumGate_2)
  283.                         {
  284.                             na.Add(aaa.Key);
  285.                         }
  286.                     }
  287.                     foreach (string aaamd5 in na)
  288.                     {
  289.                         one2Cache_t.Remove(aaamd5);
  290.                         one2Cache.Remove(aaamd5);
  291.                     }
  292.                     if (one2Cache_t.Count > (One2_ALL_NUM - 5000))
  293.                     {
  294.                         clearNumGate_2 = clearNumGate_2 + 1;
  295.                     }
  296.                 }
  297.             }
  298.             catch
  299.             {
  300.                 Console.WriteLine("清理缓存1 冲突");
  301.             }
  302.         }
  303.         /// <summary>
  304.         /// 得到URL的MD5名
  305.         /// </summary>
  306.         /// <param name="url"></param>
  307.         /// <returns></returns>
  308.         private static string getMD5name(string url)
  309.         {
  310.             string strMd5 = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(url, "md5");
  311.             return strMd5;
  312.         }
  313.     }
  314. }