public function incrementViewCounter()
 {
     // Only increment the view counter if the current user is a subscriber
     if (Auth::isSubscriber()) {
         $modelName = (new ReflectionClass($this))->getShortName();
         $modelKey = strtolower($modelName) . '_id';
         // Only increment the view counter if the user has not visited in 1 hour
         if (!Redis::exists($modelName . 'ViewByUser:'******':' . Auth::id())) {
             // Increment
             Redis::hincrby($modelName . ':' . $this->{$modelKey}, 'views', 1);
             // Add user to recent views
             Redis::setex($modelName . 'ViewByUser:'******':' . Auth::id(), 3600, true);
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($url)
 {
     Redis::setex(env('CACHE_PREFIX') . base64_decode($url), 1, '');
     return response()->json(['status' => 'ok']);
 }
Ejemplo n.º 3
0
 public function process()
 {
     $startTime = microtime(true);
     if (!($url = Input::get('url'))) {
         return self::errResponse('no url in request', $startTime);
     }
     $cacheKey = env('CACHE_PREFIX') . $url;
     if ($cacheResult = self::getCache($cacheKey, $startTime)) {
         return response()->json($cacheResult);
     }
     $client = new \GuzzleHttp\Client(['base_uri' => $url, 'verify' => false]);
     try {
         $page = $client->get('')->getBody()->getContents();
     } catch (GuzzleException $e) {
         return self::errResponse('unable to get page', $startTime);
     }
     $document = new DOMDocument();
     libxml_use_internal_errors(true);
     if (!$document->loadHTML($page)) {
         return self::errResponse('invalid html from ' . $url, $startTime);
     }
     $errors = [];
     $xpath = new DOMXPath($document);
     $imagesQueue = self::getCssImages($xpath->query('//link[@rel=\'stylesheet\']'), $client, $errors);
     foreach ($xpath->query('//img') as $imgNode) {
         if ($src = $imgNode->getAttribute('src')) {
             array_push($imagesQueue, self::cleanUrl($src));
         }
     }
     list($urlSrcImages, $inlineSrcImages) = self::separateInlineImages($imagesQueue);
     $imgSizes = array_map(function ($imgSrc) {
         return strlen($imgSrc);
     }, $inlineSrcImages);
     $promises = self::makePromisesFromQueue(array_unique($urlSrcImages), $client, $imgSizes, $errors);
     foreach ($promises as $promise) {
         $promise->wait();
     }
     $response = ['url' => $url, 'imageCount' => count($imgSizes), 'imageSize' => array_sum($imgSizes)];
     Redis::setex($cacheKey, env('CACHE_TTL'), json_encode($response));
     if ($errors) {
         $response['errors'] = array_unique(array_values($errors));
     }
     $response['status'] = 'ok';
     $response['timestamp'] = self::timestamp($startTime);
     $response['time'] = microtime(true) - $startTime;
     return response()->json($response);
 }
Ejemplo n.º 4
0
 public function testRedis()
 {
     $config = Config::get('cms.comment');
     dump($config);
     Redis::setex('web2', 10, 'www.tantengvip.com');
 }