123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace huo\controller\shop;
- use huo\controller\common\Base;
- use huo\logic\shop\GoodsLogic;
- use huo\model\shop\GoodsModel;
- use think\Cache;
- class GoodsCache extends Base {
- public static function ins() {
- return new static();
- }
-
- private function getGoodsKey($goods_id) {
- return 'goods_id_key_'.$goods_id;
- }
-
- public function getInfoByGoodsId($goods_id) {
- $_key = $this->getGoodsKey($goods_id);
- $_goods_data_json = Cache::get($_key);
- $_goods_data = json_decode($_goods_data_json, true);
- if (!is_array($_goods_data)) {
- $_goods_data = $_goods_data_json;
- if (isset($_goods_data['goods_content'])) {
- $_goods_data['goods_content'] = htmlspecialchars(
- cmf_replace_content_file_url(htmlspecialchars_decode($_goods_data['goods_content']), true)
- );
- }
- }
- if (!is_array($_goods_data) || empty($_goods_data)) {
- $_goods_data = (new GoodsLogic())->getInfoByGoodsId($goods_id);
- if (empty($_goods_data)) {
- return false;
- }
- $this->saveGoodsCache($goods_id, $_goods_data);
- }
- return $_goods_data;
- }
-
- public function saveGoodsCache($goods_id, $_goods_data, $ttl = 3600) {
- $_key = $this->getGoodsKey($goods_id);
- if (isset($_goods_data['goods_content'])) {
- $_goods_data['goods_content'] = cmf_replace_content_file_url(
- htmlspecialchars_decode($_goods_data['goods_content'])
- );
- }
- Cache::set($_key, json_encode($_goods_data), $ttl);
- }
-
- public function updateGoods($goods_id, $goods_data) {
- $_old_data = $this->getInfoByGoodsId($goods_id);
- $_data = $goods_data;
- if (!empty($_old_data)) {
- $_data = array_merge($_old_data, $goods_data);
- $this->saveGoodsCache($goods_id, $_data);
- $_rs = (new GoodsModel())->updateGoods($_data, $goods_id);
- } else {
- $_rs = (new GoodsModel())->addGoods($_data);
- }
- return $_rs;
- }
-
- public function deleteGoods($goods_id, $goods_data) {
- $_key = $this->getGoodsKey($goods_id);
- Cache::rm($_key);
- $_rs = (new GoodsModel())->updateGoods($goods_data, $goods_id);
- return $_rs;
- }
- }
|