2014-01-30 155 views
1

我打算使用Windows Forms应用程序中.Net 4.0中引入的强大缓存库。到目前为止,MemoryCache做我需要的一切,除了将其内容保存到文件。我想要做的是在应用程序退出时将缓存保存到文件中,然后当应用程序再次打开时,我应该能够从该文件中写入并将其内容写入MemoryCache。我知道我可以简单地将实例序列化到磁盘上的二进制文件,但是再次,我不知道如何将其转换回*MemoryCache*将MemoryCache内容保存到文件中

您的帮助我们非常感谢。

回答

0

我最终实现了自己的Cache项目。希望这可以帮助某个人:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.Text; 

namespace CachingDemo 
{ 
    class CachedMemory 
    { 
     System.Collections.Specialized.OrderedDictionary cache = null; 
     private String persistenceFilePath = null; 
     private int cacheSizeLimit; 
     public static readonly int CACHE_SIZE_NO_LIMIT = -1; 

     public CachedMemory(int initialCapacity, int cacheSizeLimit, string persistenceFilePath) 
     { 

      this.cache = new System.Collections.Specialized.OrderedDictionary(initialCapacity); 
      this.persistenceFilePath = persistenceFilePath; 
      this.cacheSizeLimit = cacheSizeLimit; 

     } 

     public int getCacheSize() 
     { 
      return this.cache.Count; 
     } 

     public CachedMemory(int cacheSizeLimit, string cacheFilePath) 
     { 
      initializeCache(cacheFilePath, cacheSizeLimit); 
     } 

     private void initializeCache(string cacheFilePath, int cacheSizeLimit) 
     { 
      this.cacheSizeLimit = cacheSizeLimit; 
      using (FileStream fileStream = new FileStream(cacheFilePath, FileMode.Open)) 
      { 
       IFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); 
       this.cache = (System.Collections.Specialized.OrderedDictionary)bf.Deserialize(fileStream); 
       fileStream.Close(); 
      } 

      //In case the deserialized OrderedDictionary had more contents than the limit, we need to shrink it to make its size equal to the limit 
      if (this.cacheSizeLimit != CACHE_SIZE_NO_LIMIT && this.cache.Keys.Count > this.cacheSizeLimit) 
      { 
       int difference = this.cache.Keys.Count - this.cacheSizeLimit; 

       for (int i = 0; i < difference; i++) 
       { 
        cache.RemoveAt(0); 
       } 
      } 
     } 

     public string get(string key) 
     { 
      return cache[key] as string; 
     } 

     public string get(int index) 
     { 
      return cache[index] as string; 
     } 


     public void add(string key, string value) 
     { 
      //An ordered dictionary would throw an exception if we try to insert the same key again, so we have to make sure that the newly 
      //introduced key is not a duplicate. 
      if (this.cache.Contains(key)) 
      { 
       this.cache.Remove(key); 

      } 
      else 
       if (this.cacheSizeLimit != CACHE_SIZE_NO_LIMIT && this.cache.Count == this.cacheSizeLimit) 
       { 
        this.cache.RemoveAt(0); 

       } 

      this.cache.Add(key, value); 
     } 

     public void persist() 
     { 
      using (FileStream fileStream = new FileStream(persistenceFilePath, FileMode.Create)) 
      { 
       IFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); 
       bf.Serialize(fileStream, this.cache); 
       fileStream.Close(); 
      } 
     } 
    } 
} 
+0

所以你只是想缓存字符串?你在某处隐藏序列化机制吗? –

+0

上述示例可以用于使用自定义类实例化的对象,因为它们被定义为可序列化的。 –