| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 | <?php/** * GoodsCache.php UTF-8 * 商品缓存 * * @date    : 2018/5/7 22:44 * * @license 这不是一个自由软件,未经授权不许任何使用和传播。 * @author  : wuyonghong <wyh@huosdk.com> * @version : HUOSDK 8.0 */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();    }    /**     * 获取礼包KEY     *     * @param string $goods_id     *     * @return string     */    private function getGoodsKey($goods_id) {        return 'goods_id_key_'.$goods_id;    }    /**     * 通过商品ID 查询商品信息     *     * @param string $goods_id     *     * @return array|bool|mixed     */    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;    }    /**     * 保存礼包cache 数据     *     * @param     $goods_id     * @param     $_goods_data     * @param int $ttl     */    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);    }    /**     * 更新商品信息     *     * @param string $goods_id     * @param array  $goods_data     *     * @return bool     * @throws \think\Exception     * @throws \think\db\exception\DataNotFoundException     * @throws \think\db\exception\ModelNotFoundException     * @throws \think\exception\DbException     */    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;    }    /**     *  删除礼包     *     * @param $goods_id     * @param $goods_data     *     * @return bool     */    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;    }}
 |