You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileOuputCache.cs 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System.IO;
  2. using System.Runtime.Serialization.Formatters.Binary;
  3. using System.Web;
  4. namespace iiie.CacheControl.Business.OutputCache
  5. {
  6. public class FileOuputCache : IOutputCache
  7. {
  8. public string TempPath { get; set; }
  9. public string GetPath(string key)
  10. {
  11. key = HttpUtility.UrlEncode(key);
  12. return Path.Combine(TempPath, key);
  13. }
  14. public override object Get(string key)
  15. {
  16. key = GetPath(key);
  17. var formatter = new BinaryFormatter();
  18. var stream = new FileStream(key, FileMode.Open);
  19. var obj = formatter.Deserialize(stream);
  20. stream.Close();
  21. return obj;
  22. }
  23. public override void Remove(string key)
  24. {
  25. key = GetPath(key);
  26. File.Delete(key);
  27. }
  28. public override bool Contains(string key)
  29. {
  30. key = GetPath(key);
  31. return File.Exists(key);
  32. }
  33. public override void Add(string key, object o)
  34. {
  35. key = GetPath(key);
  36. var formatter = new BinaryFormatter();
  37. var stream = new FileStream(key, FileMode.Open);
  38. formatter.Serialize(stream, o);
  39. stream.Close();
  40. }
  41. }
  42. }