C# 将MemoryCache内容持久化到文件
我打算在Windows窗体应用程序中使用.Net 4.0中引入的强大缓存库。到目前为止,C# 将MemoryCache内容持久化到文件,c#,.net,caching,C#,.net,Caching,我打算在Windows窗体应用程序中使用.Net 4.0中引入的强大缓存库。到目前为止,MemoryCache除了将其内容持久化到文件之外,还完成了我所需要的一切。我试图做的是在应用程序退出时将缓存持久化到一个文件中,然后当应用程序再次打开时,我应该能够从文件中写入内容,并将其内容放入内存缓存中。我知道我可以简单地将实例序列化为磁盘上的二进制文件,但我不知道如何将其转换回*MemoryCache* 非常感谢您的帮助。我最终实现了自己的缓存项目。希望这能在某个地方对某人有所帮助: using Sy
MemoryCache
除了将其内容持久化到文件之外,还完成了我所需要的一切。我试图做的是在应用程序退出时将缓存持久化到一个文件中,然后当应用程序再次打开时,我应该能够从文件中写入内容,并将其内容放入内存缓存中。我知道我可以简单地将实例序列化为磁盘上的二进制文件,但我不知道如何将其转换回*MemoryCache*
非常感谢您的帮助。我最终实现了自己的缓存项目。希望这能在某个地方对某人有所帮助:
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();
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Runtime.Serialization;
使用系统文本;
名称空间CachingDemo
{
类CachedMemory
{
System.Collections.Specialized.OrderedDictionary缓存=null;
私有字符串persistenceFilePath=null;
私有int cacheSizeLimit;
公共静态只读int缓存\u大小\u无\u限制=-1;
公共CachedMemory(int initialCapacity、int cacheSizeLimit、字符串persistenceFilePath)
{
this.cache=new System.Collections.Specialized.OrderedDictionary(initialCapacity);
this.persistenceFilePath=persistenceFilePath;
this.cacheSizeLimit=cacheSizeLimit;
}
public int getCacheSize()
{
返回this.cache.Count;
}
公共CachedMemory(int cacheSizeLimit,string cacheFilePath)
{
初始化缓存(cacheFilePath,cacheSizeLimit);
}
私有void initializeCache(字符串cacheFilePath,int cacheSizeLimit)
{
this.cacheSizeLimit=cacheSizeLimit;
使用(FileStream FileStream=newfilestream(cacheFilePath,FileMode.Open))
{
IFormatter bf=new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
this.cache=(System.Collections.Specialized.OrderedDictionary)bf.Deserialize(fileStream);
fileStream.Close();
}
//如果反序列化的OrderedDictionary的内容超过限制,我们需要将其缩小,使其大小等于限制
if(this.cacheSizeLimit!=CACHE\u SIZE\u NO\u LIMIT&&this.CACHE.Keys.Count>this.cacheSizeLimit)
{
int difference=this.cache.Keys.Count-this.cacheSizeLimit;
for(int i=0;i
所以您只需要缓存字符串?您是否在某处隐藏了序列化机制?上面的示例可以用于使用自定义类实例化的对象,因为它们被定义为可序列化的。