PHP uses Redis to implement statistical cache MySQL pressure method _php tips

Source: Internet
Author: User
Tags md5 php programming redis

The example in this article describes how PHP uses Redis to implement statistical caching of MySQL pressure. Share to everyone for your reference, specific as follows:

<?php header ("Content-type:text/html;charset=utf-8");
  Include ' lib/mysql.class.php ';
  $mysql _obj = Mysql::getconn ();
  Redis $redis = new Redis ();
  $redis->pconnect (' 127.0.0.1 ', 6379);
  if (Isset ($_server[' http_referer ')) {$url _MD5 = MD5 ($_server[' http_referer ']); 
  } $adve _key = ' adve ';
  $adve _key_exists = ' adve_exists ';
    if (! $redis->exists ($adve _key_exists)) {$list = $mysql _obj->fetch_array ("SELECT * from User_online_adve");
        if ($list) {foreach ($list as $key => $value) {$url _hash = MD5 ($value [' Adve_url ']);
        $adve _hash_key = $adve _key. ":" $url _hash;
        $id = $value [' id '];
        $redis->set ($adve _hash_key, $id);
        $redis->set ($adve _key_exists,true);
        $redis->hmset ($adve _hash_key, array (' ID ' => $id));
      Print_r ($redis->get ($adve _hash_key));
  }} $adve _new_key = $adve _key. ': '. $url _md5; if ($redis->exists ($adve _new_key)) {$adve _plus = $adve _new_key. ":p LUs" ; 
      if (! $redis->exists ($adve _plus)) {$redis->set ($adve _plus,1);
        }else{$redis->incr ($adve _plus);
        $num = $redis->get ($adve _plus);
          if ($num >10) {$id = $redis->get ($adve _new_key);
          Insert to SQL;
          $mysql _obj->query ("Update user_online_adve set adve_num=adve_num+ $num where id= $id");
        $redis->set ($adve _plus,1);
  } header (' http/1.0 moved Permanently ');
Header (' location:https://itunes.apple.com/cn/app/san-guo-zhi15-ba-wangno-da-lu/id694974270?mt=8 ');
      /* if () {$adve _plus = $adve _key. ":p LUs";
      if ($redis->exists ($adve _plus)) {$redis->incr ($adve _plus); 
      }else{$redis->set ($adve _plus,1);
  echo $redis->get ($adve _plus);
      foreach ($list as $key => $value) {$url _hash = MD5 ($value [' Adve_url ']);
      $id = $value [' id '];
      $adve _num = $value [' Adve_num ']; $adve _plus = $adve _key. ":p LUs";
      if ($redis->exists ($adve _plus)) {$redis->incr ($adve _plus); 
      }else{$redis->set ($adve _plus,1);
      echo $redis->get ($adve _plus);
      if ($redis->)//$redis->hmset ($adve _key, array (' ID ' => $id, ' adve_num ' => $adve _num));
  Print_r ($redis->hmget ("Adve: $url _hash", Array (' Adve_num '));
} print_r ($list);

 */

I hope this article will help you with your PHP programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.