Cache.cs
上传用户:huiyue
上传日期:2022-04-08
资源大小:1429k
文件大小:7k
源码类别:

搜索引擎

开发平台:

ASP/ASPX

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Xml.Serialization;
  5. using cd.net;
  6. namespace Searcharoo.Common
  7. {
  8.     public class CachedFile
  9.     {
  10.         private string url;
  11.         private string[] words;
  12.         private int indexId;
  13.         public int IndexId
  14.         {
  15.             get { return indexId; }
  16.             set { indexId = value; }
  17.         }
  18.         public string Url
  19.         {
  20.             get { return url; }
  21.             set { url = value; }
  22.         }
  23.         
  24.         public string[] Words
  25.         {
  26.             get { return words; }
  27.             set { words = value; }
  28.         }
  29.     }
  30.     /// <summary>
  31.     /// Cache of each Document's text content (for search result 'preview')
  32.     /// </summary>
  33.     public class Cache
  34.     {
  35.         /// <summary>
  36.         /// Internal datastore of Files referencing cached content
  37.         /// </summary>
  38.         /// <remarks>
  39.         /// Hashtable
  40.         /// key    = STRING representation of the Url, 
  41.         /// value  = STRING[] of all words at that Url
  42.         /// </remarks>
  43.         private System.Collections.Hashtable _Index = new System.Collections.Hashtable(); //TODO: implement collection with faster searching
  44.         public bool Add(string[] words, File infile)
  45.         {
  46.             // ### Make sure the Word object is in the index ONCE only
  47.             if (_Index.ContainsKey(infile.Url))
  48.             {
  49.                 // already cached
  50.                 return false;
  51.             }
  52.             else
  53.             {
  54.                 CachedFile cf = new CachedFile();
  55.                 cf.Url = infile.Url;
  56.                 cf.Words = words;
  57.                 _Index.Add(infile.Url, cf);
  58.             }
  59.             return true;
  60.         }
  61.         public bool Contains (string url)
  62.         {
  63.             return _Index.Contains(url.ToLower());
  64.         }
  65.         public void SetIndexId(string url, int index)
  66.         {
  67.             CachedFile cf = (CachedFile)_Index[url.ToLower()];
  68.             cf.IndexId = index;
  69.         }
  70.         public string[] GetDocumentCache(string url)
  71.         {
  72.             CachedFile cf = (CachedFile)_Index[url.ToLower()];
  73.             return cf.Words;
  74.         }
  75.         [XmlElement("f")]
  76.         public CachedFile[] Files
  77.         {
  78.             get
  79.             {
  80.                 List<CachedFile> l = new List<CachedFile>();
  81.                 foreach (object o in _Index.Values)
  82.                 {
  83.                     l.Add((CachedFile)o);
  84.                 }
  85.                 return l.ToArray();
  86.             }
  87.             set
  88.             {
  89.                 foreach (object o in value)
  90.                 { 
  91.                     CachedFile cf = (CachedFile)o;
  92.                     _Index.Add(cf.Url, cf);
  93.                 }
  94.             }
  95.         }
  96.         public bool Save()
  97.         {
  98.             string fileNameBase = System.IO.Path.GetDirectoryName(Preferences.CatalogFileName) + System.IO.Path.DirectorySeparatorChar + System.IO.Path.GetFileNameWithoutExtension(Preferences.CatalogFileName);
  99.             // XML
  100.             if (Preferences.InMediumTrust)
  101.             {
  102.                 // TODO: Maybe use to save as ZIP - save space on disk? http://www.123aspx.com/redir.aspx?res=31602
  103.                 string xmlFileName = fileNameBase + "-cache.xml";
  104.                 Kelvin<Cache>.ToXmlFile(this, xmlFileName);
  105.                 return true;
  106.             }
  107.             // BINARY http://www.dotnetspider.com/technology/kbpages/454.aspx
  108.             System.IO.Stream stream = new System.IO.FileStream(fileNameBase + "-cache.dat", System.IO.FileMode.Create);
  109.             System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  110.             formatter.Serialize(stream, this);
  111.             stream.Close();
  112.             return true;
  113.         }
  114.         public static Cache Load()
  115.         {
  116.             string fileNameBase = System.IO.Path.GetDirectoryName(Preferences.CatalogFileName) + System.IO.Path.DirectorySeparatorChar + System.IO.Path.GetFileNameWithoutExtension(Preferences.CatalogFileName);
  117.             if (Preferences.InMediumTrust)
  118.             {
  119.                 try
  120.                 {
  121.                     string xmlFileName = fileNameBase + "-cache.xml";
  122.                     if (System.IO.File.Exists(xmlFileName))
  123.                     {
  124.                         Cache c1 = Kelvin<Cache>.FromXmlFile(xmlFileName);
  125.                         return c1;
  126.                     }
  127.                     else
  128.                     {
  129.                         throw new Exception("Could not load cache from file " + xmlFileName + " - check that it exists.");
  130.                     }
  131.                 }
  132.                 catch (Exception)
  133.                 {   // [v6] : if cannot load from .DAT or .XML, try to load from compiled resource
  134.                     try
  135.                     {   // http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=75
  136.                         System.Reflection.Assembly a = System.Reflection.Assembly.Load("WebAppCatalogResource");
  137.                         string[] resNames = a.GetManifestResourceNames();
  138.                         if (resNames.Length > 1)
  139.                         {
  140.                             Cache c2 = Kelvin<Cache>.FromResource(a, resNames[1]);
  141.                             return c2;
  142.                         }
  143.                         else 
  144.                         {   // no 'cache' in the resources 
  145.                             throw new Exception("Could not find '-cache' resource in " + a.FullName 
  146.                                 + " - check it was compiled in correctly (ie. the .XML file is marked as embedded resource).");
  147.                         }
  148.                     }
  149.                     catch (Exception e1)
  150.                     {
  151.                         throw new Exception("Searcharoo Cache.Load() " + e1.Message, e1);
  152.                     }
  153.                 }
  154.             }
  155.             else
  156.             {   // hopefully in Full trust
  157.                 // using Binary serialization requires the Binder because of the embedded 'full name'
  158.                 // of the serializing assembly - all the above methods using Xml do not have this requirement
  159.                 if (System.IO.File.Exists(fileNameBase + "-cache.dat"))
  160.                 {
  161.                     object deserializedCacheObject;
  162.                     using (System.IO.Stream stream = new System.IO.FileStream(fileNameBase + "-cache.dat", System.IO.FileMode.Open))
  163.                     {
  164.                         System.Runtime.Serialization.IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
  165.                         //object m = formatter.Deserialize (stream); // This doesn't work, SerializationException "Cannot find the assembly <random name>"
  166.                         formatter.Binder = new CatalogBinder(); // This custom Binder is REQUIRED to find the classes in our current 'Temporary ASP.NET Files' assembly
  167.                         deserializedCacheObject = formatter.Deserialize(stream);
  168.                     } //stream.Close();
  169.                     Cache catalog = deserializedCacheObject as Cache;
  170.                     return catalog;
  171.                 }
  172.                 else
  173.                 {
  174.                     return null;
  175.                 }
  176.             }
  177.         }
  178.     }
  179. }