基于文件的缓存c#? [英] File Based Cache c# ?

查看:215
本文介绍了基于文件的缓存c#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以提出一些建议,以便在c#中实现基于文件的缓存.我需要为我的对象实现asp样式的缓存,但不是在内存中,而是在文件系统中,因为对象很大.
预先感谢.

Can anybody give some suggestions ow to implement file based cache in c#. I need to implement asp style cache for my objects but not in memory but in file system because object are large.
Thanks in advance.

推荐答案

我不确定这是否是您问题的完整解决方案,因为您没有提供足够的信息,但您是否看过序列化? br/>
csharp-tutorial-serialize-objects-to-a [ ^ ].

.Net中还有其他序列化策略:
XML序列化 [常规概述 [
I''m not sure this is a complete solution to your problem since you have not given enough information but have you looked at serialization?

csharp-tutorial-serialize-objects-to-a-file[^].

There are other serialization strategies in .Net:
XML Serialization[^].
General overview[^].


public static class FileBasedCahce {
    static Dictionary<string, string> _FileMap;
    const string MAPFILENAME = "FileBasedCahceMAP.dat";
    public static string DirectoryLocation = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
    static FileBasedCahce() {
        if (!Directory.Exists(DirectoryLocation))
            throw new ArgumentException("directoryLocation msu exist");
        if (File.Exists(MyMapFileName)) {
            _FileMap = DeSerializeFromBin<Dictionary<string, string>>(MyMapFileName);
        } else
            _FileMap = new Dictionary<string, string>();
    }
    public static T Get<T>(string key) where T:new(){
        if (_FileMap.ContainsKey(key))
            return (T)DeSerializeFromBin<T>(_FileMap[key]);
        else
            throw new ArgumentException("Key not found");
    }
    public static void Insert<T>(string key, T value) {
        if (_FileMap.ContainsKey(key)) {
            SerializeToBin(value, _FileMap[key]);
        } else {
            _FileMap.Add(key, GetNewFileName);
            SerializeToBin(value, _FileMap[key]);
        }
        SerializeToBin(_FileMap, MyMapFileName);
    }
    private static string GetNewFileName {
        get {
            return Path.Combine(DirectoryLocation, Guid.NewGuid().ToString());
        }
    }
    private static string MyMapFileName {
        get {
            return Path.Combine(DirectoryLocation, MAPFILENAME);
        }
    }
    private  static void SerializeToBin(object obj, string filename) {
        Directory.CreateDirectory(Path.GetDirectoryName(filename));
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
            bf.Serialize(fs, obj);
        }
    }
    private static T DeSerializeFromBin<T>(string filename) where T : new() {
        if (File.Exists(filename)) {
            T ret = new T();
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) {
                ret = (T)bf.Deserialize(fs);
            }
            return ret;
        } else
            throw new FileNotFoundException(string.Format("file {0} does not exist", filename));
    }

}


这里是一种可能性:在您的WinForms应用程序 [ ^ ].

尽管名称空间是System.Web.Caching,但是您也可以将其用于其他应用程序类型.

当然,您也可以基于xml文件,数据表等实现其他类型的解决方案,但最后我认为正确的方法取决于特定的要求.
Here''s one possibility: Using Cache in Your WinForms Applications[^].

Although the namespace is System.Web.Caching, you can use it other application types also.

Of course you can also implement other kind of solutions, based on for example xml-files, data tables etc, but in the end I think that the correct way depends on the specific requirements.


这篇关于基于文件的缓存c#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆