Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.
Posted

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[^].
 
Share this answer
 
Comments
Wendelius 11-Apr-11 13:58pm    
Good links, my 5.
C#
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));
    }

}
 
Share this answer
 
v2
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900