Exemplo n.º 1
0
        public static void Save(PairwisePictCache cache, string filename)
        {
            if (filename == null)
            {
                throw new ArgumentNullException("filename");
            }
            lock (mutex)
            {
                try
                {
                    if (cache.Changed)
                    {
                        if (formatter == null)
                        {
                            formatter = new BinaryFormatter();
                        }
                        using (FileStream fs = new FileStream(filename, FileMode.Create))
                        {
                            formatter.Serialize(fs, cache);
                        }

                        PictConstants.Trace("Saved to {0}, entries = {1}", filename, cache.Count);
                        cache.ResetChanged();
                    }
                    else
                    {
                        PictConstants.Trace("Didn't change!");
                    }
                }
                catch (Exception e)
                {
                    PictConstants.Trace("Error saving cache to {0}: {1}", filename, e.Message);
                }
            }
        }
Exemplo n.º 2
0
 public PictRunner(string cacheFileName)
 {
     if (cacheFileName != null)
     {
         this.cacheFileName = cacheFileName;
         this.cache         = PairwisePictCacheHelper.LoadOrCreate(cacheFileName);
     }
 }
Exemplo n.º 3
0
 public static PairwisePictCache LoadOrCreate(string filename)
 {
     if (filename == null)
     {
         throw new ArgumentNullException();
     }
     lock (mutex)
     {
         PairwisePictCache ppc;
         if (loaded.ContainsKey(filename))
         {
             ppc = (PairwisePictCache)loaded[filename];
             PictConstants.Trace("Using cached object instead of loading from {0}", filename);
         }
         else
         {
             if (formatter == null)
             {
                 formatter = new BinaryFormatter();
             }
             try
             {
                 using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate))
                 {
                     ppc = (PairwisePictCache)formatter.Deserialize(fs);
                     Pict.PictConstants.Trace("Loaded cache from {0}", filename);
                 }
             }
             catch (Exception e)
             {
                 Pict.PictConstants.Trace("Error loading cache from {0}: {1}", filename, e.Message);
                 ppc = new PairwisePictCache();
             }
             loaded[filename] = ppc;
         }
         return(ppc);
     }
 }