123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Web;
-
- namespace iiie.CacheControl.Business.OutputCache
- {
- public class FileOuputCache : IOutputCache
- {
- public string TempPath { get; set; }
-
- public string GetPath(string key)
- {
- key = HttpUtility.UrlEncode(key);
- return Path.Combine(TempPath, key);
- }
-
- public override object Get(string key)
- {
- key = GetPath(key);
- var formatter = new BinaryFormatter();
- var stream = new FileStream(key, FileMode.Open);
- var obj = formatter.Deserialize(stream);
- stream.Close();
- return obj;
- }
-
- public override void Remove(string key)
- {
- key = GetPath(key);
- File.Delete(key);
- }
-
- public override bool Contains(string key)
- {
- key = GetPath(key);
- return File.Exists(key);
- }
-
- public override void Add(string key, object o)
- {
- key = GetPath(key);
- var formatter = new BinaryFormatter();
- var stream = new FileStream(key, FileMode.Open);
- formatter.Serialize(stream, o);
- stream.Close();
- }
- }
- }
|