Cache.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services;
  10. /**
  11. * Cart services. 此部分是缓存配置的读取,各个页面譬如首页,产品,分类页面
  12. * 调用这里的方法读取具体配置,然后来决定缓存的开启和过期时间。
  13. * 整页缓存的具体使用,还是在相应的controller中,譬如 @appfront/modules/Catalog/controllers/CategoryController.php 中 behaviors() 方法中的使用,Yii2是通过行为的方式做绑定的。
  14. * 对于Yii2全页缓存在controller中的使用,可以参看文档:http://www.yiichina.com/doc/guide/2.0/caching-page
  15. * 对于fecshop缓存的使用,可以参看文档:http://www.fecshop.com/doc/fecshop-guide/instructions/cn-1.0/guide-fecshop_cache.html
  16. * @author Terry Zhao <2358269014@qq.com>
  17. * @since 1.0
  18. */
  19. class Cache extends Service
  20. {
  21. // 各个页面cache的配置
  22. public $cacheConfig;
  23. // cache 总开关
  24. public $enable;
  25. /**
  26. * @param $cacheKey | String , 具体的缓存名字,譬如 product category
  27. * @return boolean, 如果enable为true,则返回为true
  28. * 根据传递的$cacheKey,从配置中读取是否开启cache
  29. */
  30. public function isEnable($cacheKey)
  31. {
  32. if ($this->enable && isset($this->cacheConfig[$cacheKey]['enable'])) {
  33. return $this->cacheConfig[$cacheKey]['enable'];
  34. } else {
  35. return false;
  36. }
  37. }
  38. /**
  39. * @param $cacheKey | String , 具体的缓存名字,譬如 product category
  40. * @return int, 如果enable为true,则返回为true
  41. * 得到$cacheKey 对应的超时时间
  42. */
  43. public function timeout($cacheKey)
  44. {
  45. if (isset($this->cacheConfig[$cacheKey]['timeout'])) {
  46. return $this->cacheConfig[$cacheKey]['timeout'];
  47. } else {
  48. return 0;
  49. }
  50. }
  51. /**
  52. * @param $cacheKey | String , 具体的缓存名字,譬如 product category
  53. * @return string, 如果enable为true,则返回为true
  54. */
  55. public function disableUrlParam($cacheKey)
  56. {
  57. if (isset($this->cacheConfig[$cacheKey]['disableUrlParam'])) {
  58. return $this->cacheConfig[$cacheKey]['disableUrlParam'];
  59. } else {
  60. return '';
  61. }
  62. }
  63. /**
  64. * @param $cacheKey | String , 具体的缓存名字,譬如 product category
  65. * @return string, 如果enable为true,则返回为true
  66. * url的参数,哪一些参数作为缓存唯一的依据,譬如p(分页的值)
  67. */
  68. public function cacheUrlParam($cacheKey)
  69. {
  70. if (isset($this->cacheConfig[$cacheKey]['cacheUrlParam'])) {
  71. return $this->cacheConfig[$cacheKey]['cacheUrlParam'];
  72. } else {
  73. return '';
  74. }
  75. }
  76. }