Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

65 linhas
2.0 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Caching;
  6. /// <summary>
  7. ///CacheHelper 的摘要说明
  8. /// </summary>
  9. ///
  10. namespace Basic
  11. {
  12. public class CacheHelper
  13. {
  14. /// <summary>
  15. /// 创建缓存项的文件依赖
  16. /// </summary>
  17. /// <param name="key">缓存Key</param>
  18. /// <param name="obj">object对象</param>
  19. /// <param name="fileName">文件绝对路径</param>
  20. public static void Insert(string key, object obj, string fileName)
  21. {
  22. //创建缓存依赖项
  23. CacheDependency dep = new CacheDependency(fileName);
  24. //创建缓存
  25. HttpContext.Current.Cache.Insert(key, obj, dep);
  26. }
  27. /// <summary>
  28. /// 创建缓存项过期
  29. /// </summary>
  30. /// <param name="key">缓存Key</param>
  31. /// <param name="obj">object对象</param>
  32. /// <param name="expires">过期时间(分钟)</param>
  33. public static void Insert(string key, object obj, int expires)
  34. {
  35. HttpContext.Current.Cache.Insert(key, obj, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, expires, 0));
  36. }
  37. /// <summary>
  38. /// 获取缓存对象
  39. /// </summary>
  40. /// <param name="key">缓存Key</param>
  41. /// <returns>object对象</returns>
  42. public static object Get(string key)
  43. {
  44. if (string.IsNullOrEmpty(key))
  45. {
  46. return null;
  47. }
  48. return HttpContext.Current.Cache.Get(key);
  49. }
  50. /// <summary>
  51. /// 获取缓存对象
  52. /// </summary>
  53. /// <typeparam name="T">T对象</typeparam>
  54. /// <param name="key">缓存Key</param>
  55. /// <returns></returns>
  56. public static T Get<T>(string key)
  57. {
  58. object obj = Get(key);
  59. return obj == null ? default(T) : (T)obj;
  60. }
  61. }
  62. }