<?php
/**
 * 小程序商品首页数据cache
* Created by PhpStorm.
* User: kang
* Date: 2021/7/12
* Time: 18:15
*/
namespace JinDouYun\Cache;

use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Factory;

class PageCache
{
    private $cache;
    
    private $enterpriseId;
    
    private $userCenterId;
    
    private $cacheKey = 'Page';
    
    public function __construct($enterpriseId = false, $userCenterId = false)
    {
        $this->enterpriseId = $enterpriseId;
        $this->userCenterId = $userCenterId;
        $this->cache = Factory::cache('default');
    }
    
    /**---------------------------------------------小程序首页------------------------------------------------**/
    
    /**
     * 添加
     * @param $md5Key
     * @param $pageData
     * @return bool
     */
    public function addPage($md5Key, $pageData)
    {
        $data = json_encode($pageData);
        $result = $this->cache->hset($this->cacheKey, $md5Key, $data);
        if(!$result){
            return false;
        }
        return true;
    }
    
    /**
     * 查询
     * @param $md5Key
     * @return array
     */
    public function getPage($md5Key)
    {
        $result = $this->cache->hget($this->cacheKey, $md5Key);
        if (!$result) return [];
        $returnData = json_decode($result, true);
        
        return $returnData;
    }
    
    /**
     * 删除
     * @param $paegId
     */
    public function delPage($paegId = false)
    {
        if($paegId){
            $this->cache->hdel($this->cacheKey, $paegId);
        }else{
            $this->cache->del($this->cacheKey);
        }
    }
    
}