Beispiel #1
0
 /**
  * 从缓存中取得商品的图片集
  *
  * @param int $goods_id
  */
 public static function getGoodsGallery($goods_id)
 {
     // 参数不对,没有东西可以输出
     if ($goods_id <= 0) {
         throw new \InvalidArgumentException('goods_id [' . $goods_id . '] invalid');
     }
     $cacheId = static::makeGoodsGalleryCacheId($goods_id);
     if ($goodsGallery = ShareCache::get($cacheId)) {
         goto out;
     }
     // 优化,别老不断生成新的对象
     static $goodGalleryService = null;
     if (!$goodGalleryService) {
         $goodGalleryService = new GoodsGalleryService();
     }
     $goodsGallery = $goodGalleryService->fetchGoodsGalleryArrayByGoodsId($goods_id);
     // 非空,缓存
     if (!empty($goodsGallery)) {
         // 缓存一天
         ShareCache::set($cacheId, $goodsGallery, 86400);
     }
     out:
     return $goodsGallery;
 }
Beispiel #2
0
 /**
  * 取得当前时间需要执行的一个 Cron Task,注意这里是 load 方法,所以返回的结果是可以做 CRUD 操作的s
  *
  * @param int $currentTime 当前时间
  *
  * @return \Core\Service\Cron\DataMapper|null
  */
 public static function loadNextUnRunCronTask($currentTime)
 {
     $cronTaskInfo = ShareCache::get(self::getCronTaskCacheKey());
     // 缓存表明目前没有任务,所以我们也就没必要再去查询数据库了
     if (self::$cronTaskNoTaskMagic === $cronTaskInfo) {
         return null;
     }
     // 当前任务还没到执行的时候
     if (is_array($cronTaskInfo) && $cronTaskInfo['task_time'] > $currentTime) {
         return null;
     }
     // 从数据库中取得任务
     $cronTaskService = new CronTaskService();
     $cronTask = $cronTaskService->loadNextUnRunCronTask($currentTime);
     // 数据库里面也没有任务,设置缓存为“无任务”就不用老去查找数据库了
     if ($cronTask->isEmpty()) {
         ShareCache::set(self::getCronTaskCacheKey(), self::$cronTaskNoTaskMagic, 3600);
         return null;
     }
     // 这个任务还没到执行的时间,放到缓存里面
     if ($cronTask->task_time > $currentTime) {
         ShareCache::set(self::getCronTaskCacheKey(), $cronTask->toArray(), 3600);
         return null;
     }
     // 清除缓存
     ShareCache::clear(self::getCronTaskCacheKey());
     // 返回需要执行的任务
     return $cronTask;
 }
Beispiel #3
0
 public function fetchOptionValueArray($optionName, $ttl = 0)
 {
     $optionValueArray = array();
     $cacheId = $this->makeCacheId($optionName);
     if ($ttl > 0) {
         // 首先检查缓存
         if ($optionValueArray = ShareCache::get($cacheId)) {
             goto out;
         }
     }
     // 从数据库查询数据
     $metaService = new MetaService();
     $optionArray = $metaService->fetchMetaArrayByTypeAndKey(OptionDbDriver::META_TYPE, $optionName);
     if (!empty($optionArray)) {
         foreach ($optionArray as $optionItem) {
             $optionValueArray[] = array('id' => $optionItem['meta_id'], 'name' => $optionItem['meta_key'], 'value' => $optionItem['meta_data']);
         }
         if ($ttl > 0) {
             ShareCache::set($cacheId, $optionValueArray, $ttl);
         }
     } else {
         global $f3;
         if ($f3->get('DEBUG')) {
             // debug 模式,我们对不存在的 optionName 报错,方便发现错误
             throw new \InvalidArgumentException('optionName [' . $optionName . '] does not exist');
         }
     }
     out:
     return $optionValueArray;
 }