cachemode.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Package cachemode provides ways to deal with different cache modes
  2. package cachemode
  3. // Setting represents a cache mode setting
  4. type Setting int
  5. // Possible cache modes
  6. const (
  7. Unset = iota // cache mode has not been set
  8. On // cache everything
  9. Development // cache everything, except Amber, Lua, GCSS and Markdown
  10. Production // cache everything, except Amber and Lua
  11. Images // cache images (png, jpg, gif, svg)
  12. Small // only cache small files (<=64KB) // 64 * 1024
  13. Off // cache nothing
  14. Default = On
  15. )
  16. // Names is a map of cache mode setting string representations
  17. var Names = map[Setting]string{
  18. Unset: "unset",
  19. On: "On",
  20. Development: "Development",
  21. Production: "Production",
  22. Images: "Images",
  23. Small: "Small",
  24. Off: "Off",
  25. }
  26. // New creates a CacheModeSetting based on a variety of string options, like "on" and "off".
  27. func New(mode string) Setting {
  28. switch mode {
  29. case "everything", "all", "on", "1", "enabled", "yes", "enable": // Cache everything.
  30. return On
  31. case "production", "prod": // Cache everything, except: Amber and Lua.
  32. return Production
  33. case "images", "image": // Cache images (png, jpg, gif, svg).
  34. return Images
  35. case "small", "64k", "64KB": // Cache only small files (<=64KB), but not Amber and Lua
  36. return Small
  37. case "off", "disabled", "0", "no", "disable": // Disable caching entirely.
  38. return Off
  39. case "dev", "default", "unset": // Cache everything, except: Amber, Lua, GCSS and Markdown.
  40. fallthrough
  41. default:
  42. return Default
  43. }
  44. }
  45. // String returns the name of the cache mode setting, if set
  46. func (cms Setting) String() string {
  47. for k, v := range Names {
  48. if k == cms {
  49. return v
  50. }
  51. }
  52. // Could not find the name
  53. return Names[Unset]
  54. }