我打算使用在.NET 4.0中引入的Windows窗体应用程序的功能强大的缓存库。到目前为止,的MemoryCache
做我需要的一切,除了坚持其内容保存到一个文件中。我正在试图做的是持续的高速缓存上的应用程序退出的文件,然后应用程序被再次打开时,我应该可以从文件中写入,并把它的内容在的MemoryCache
。我知道,我可以简单的序列化实例为二进制磁盘上,但还是那句话,我不知道如何将它转换回一个 *的MemoryCache *
。
您帮助我们很多AP preciated。
解决方案我最终实现自己的高速缓存的项目。希望这会帮助别人的地方:
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq的;
使用System.Runtime.Serialization;
使用System.Text;
命名空间CachingDemo
{
类CachedMemory
{
System.Collections.Specialized.OrderedDictionary缓存= NULL;
私人字符串persistenceFilePath = NULL;
私人诠释cacheSizeLimit;
公共静态只读INT CACHE_SIZE_NO_LIMIT = -1;
公共CachedMemory(INT参数:initialCapacity,诠释cacheSizeLimit,串persistenceFilePath)
{
this.cache =新System.Collections.Specialized.OrderedDictionary(参数:initialCapacity);
this.persistenceFilePath = persistenceFilePath;
this.cacheSizeLimit = cacheSizeLimit;
}
公众诠释getCacheSize()
{
返回this.cache.Count;
}
公共CachedMemory(INT cacheSizeLimit,串cacheFilePath)
{
initializeCache(cacheFilePath,cacheSizeLimit);
}
私人无效initializeCache(字符串cacheFilePath,INT cacheSizeLimit)
{
this.cacheSizeLimit = cacheSizeLimit;
使用(的FileStream FILESTREAM =新的FileStream(cacheFilePath,FileMode.Open))
{
IFormatter BF =新System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
this.cache =(System.Collections.Specialized.OrderedDictionary)bf.Deserialize(FILESTREAM);
fileStream.Close();
}
//如果反序列化OrderedDictionary具有比限制的详细内容,我们需要收缩它做它的大小等于极限
如果(this.cacheSizeLimit = CACHE_SIZE_NO_LIMIT和放大器;!&安培; this.cache.Keys.Count> this.cacheSizeLimit)
{
INT差= this.cache.Keys.Count - this.cacheSizeLimit;
的for(int i = 0; I<区别;我++)
{
cache.RemoveAt(0);
}
}
}
公共字符串获取(字符串键)
{
返回高速缓存[重点]作为串;
}
公共字符串GET(INT指数)
{
返回高速缓存[指数]的字符串;
}
公共无效添加(字符串键,字符串值)
{
//有序的,如果我们试图再次插入相同的密钥字典中会抛出异常,所以我们必须确保新
//引进关键是不要重复。
如果(this.cache.Contains(键))
{
this.cache.Remove(键);
}
其他
如果(this.cacheSizeLimit = CACHE_SIZE_NO_LIMIT和放大器;!&安培; this.cache.Count == this.cacheSizeLimit)
{
this.cache.RemoveAt(0);
}
this.cache.Add(键,值);
}
公共无效坚持()
{
使用(的FileStream FILESTREAM =新的FileStream(persistenceFilePath,FileMode.Create))
{
IFormatter BF =新System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bf.Serialize(FILESTREAM,this.cache);
fileStream.Close();
}
}
}
}
I'm intending to use the powerful caching libraries that are introduced in .Net 4.0 in a Windows Forms application. So far, MemoryCache
does everything I need except for persisting its contents to a file. What I'm trying to do is persisting the cache to a file on application exit, and then when the application is opened again, I should be able to write from the file and put its contents in the MemoryCache
. I know that I can simply serialize the instance into binary on disk, but then again, I wouldn't know how to convert it back to a *MemoryCache*
.
Your help us much appreciated.
解决方案I ended up implementing my own Cache project. Hopefully this will help someone somewhere:
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();
}
}
}
}