Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

TimeCacheControlAttribute.cs 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using iiie.CacheControl.Attributes;
  3. using iiie.CacheControl.Business.OutputCache;
  4. using iiie.CacheControl.DBO;
  5. namespace iiie.CacheControl.Business.Attributes
  6. {
  7. /// <summary>
  8. /// A basic cache control based on time
  9. /// </summary>
  10. public class TimeCacheControlAttribute : CacheControlAttribute
  11. {
  12. /// <summary>
  13. /// The number of seconds the cache is valid
  14. /// </summary>
  15. protected int Seconds { get; set; }
  16. /// <summary>
  17. /// Contruct a cache control based on time
  18. /// </summary>
  19. /// <param name="seconds">The number of seconds the cache is valid</param>
  20. /// <param name="mustRevalidate">Indicates if the client can reuse cached data without asking origin server</param>
  21. /// <param name="excludeQueryStringFromCacheKey">Indicates if the query string must be used to control cache</param>
  22. /// <param name="cacheType">Define the cache type used to store cache</param>
  23. public TimeCacheControlAttribute(int seconds, bool mustRevalidate = true,
  24. bool excludeQueryStringFromCacheKey = false, OutputCacheType cacheType = OutputCacheType.Memory)
  25. {
  26. Seconds = seconds;
  27. MustRevalidate = mustRevalidate;
  28. ExcludeQueryStringFromCacheKey = excludeQueryStringFromCacheKey;
  29. CacheType = cacheType;
  30. }
  31. /// <summary>
  32. /// Check if data has been cached for less than Senconds seconds
  33. /// </summary>
  34. /// <param name="data">The cached data</param>
  35. /// <returns>True if cache is still valid</returns>
  36. protected override bool IsValid(CacheDbo data)
  37. {
  38. return data.Date.AddSeconds(Seconds) >= DateTime.Now;
  39. }
  40. }
  41. }