Cache.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. use Yii;
  11. /**
  12. * Cart services. 此部分是缓存配置的读取,各个页面譬如首页,产品,分类页面
  13. * 调用这里的方法读取具体配置,然后来决定缓存的开启和过期时间。
  14. * 整页缓存的具体使用,还是在相应的controller中,譬如 @appfront/modules/Catalog/controllers/CategoryController.php 中 behaviors() 方法中的使用,Yii2是通过行为的方式做绑定的。
  15. * 对于Yii2全页缓存在controller中的使用,可以参看文档:http://www.yiichina.com/doc/guide/2.0/caching-page
  16. * 对于fecshop缓存的使用,可以参看文档:http://www.fecshop.com/doc/fecshop-guide/instructions/cn-1.0/guide-fecshop_cache.html
  17. * @author Terry Zhao <2358269014@qq.com>
  18. * @since 1.0
  19. */
  20. class Cache extends Service
  21. {
  22. // 各个页面cache的配置
  23. public $cacheConfig;
  24. // cache 总开关
  25. protected $enable = false;
  26. // 在store config中的cache配置
  27. protected $_cache_config;
  28. // 各个页面对应的 store cache config的key
  29. public $cacheArr = [
  30. 'category' => 'categoryPageCache',
  31. 'product' => 'productPageCache',
  32. 'home' => 'homePageCache',
  33. 'article' => 'articlePageCache',
  34. ];
  35. /**
  36. * 得到当前的入口对应的cache的配置信息
  37. *
  38. */
  39. public function getCacheConfig()
  40. {
  41. $appName = Yii::$service->helper->getAppName();
  42. $cacheConfig = Yii::$app->store->get($appName.'_cache');
  43. if (!$cacheConfig || !is_array($cacheConfig)) {
  44. return null;
  45. }
  46. if (isset($cacheConfig['allPageCache']) && $cacheConfig['allPageCache'] == Yii::$app->store->enable) {
  47. $this->enable = true;
  48. }
  49. $this->_cache_config = $cacheConfig;
  50. return true;
  51. }
  52. public function init()
  53. {
  54. parent::init();
  55. $this->getCacheConfig();
  56. }
  57. /**
  58. * @param $cacheKey | String , 具体的缓存名字,譬如 product category
  59. * @return boolean, 如果enable为true,则返回为true
  60. * 根据传递的$cacheKey,从配置中读取是否开启cache
  61. */
  62. public function isEnable($cacheKey)
  63. {
  64. $cacheConfigKey = $this->cacheArr[$cacheKey];
  65. if ($this->enable && isset($this->_cache_config[$cacheConfigKey]) &&
  66. $this->_cache_config[$cacheConfigKey] == Yii::$app->store->enable
  67. ) {
  68. return true;
  69. } else {
  70. return false;
  71. }
  72. }
  73. /**
  74. * @param $cacheKey | String , 具体的缓存名字,譬如 product category
  75. * @return int, 如果enable为true,则返回为true
  76. * 得到$cacheKey 对应的超时时间
  77. */
  78. public function timeout($cacheKey)
  79. {
  80. if (isset($this->cacheConfig[$cacheKey]['timeout'])) {
  81. return $this->cacheConfig[$cacheKey]['timeout'];
  82. } else {
  83. return 0;
  84. }
  85. }
  86. /**
  87. * @param $cacheKey | String , 具体的缓存名字,譬如 product category
  88. * @return string, 如果enable为true,则返回为true
  89. */
  90. public function disableUrlParam($cacheKey)
  91. {
  92. if (isset($this->cacheConfig[$cacheKey]['disableUrlParam'])) {
  93. return $this->cacheConfig[$cacheKey]['disableUrlParam'];
  94. } else {
  95. return '';
  96. }
  97. }
  98. /**
  99. * @param $cacheKey | String , 具体的缓存名字,譬如 product category
  100. * @return string, 如果enable为true,则返回为true
  101. * url的参数,哪一些参数作为缓存唯一的依据,譬如p(分页的值)
  102. */
  103. public function cacheUrlParam($cacheKey)
  104. {
  105. if (isset($this->cacheConfig[$cacheKey]['cacheUrlParam'])) {
  106. return $this->cacheConfig[$cacheKey]['cacheUrlParam'];
  107. } else {
  108. return '';
  109. }
  110. }
  111. }