首页 > 开发 > PHP > 正文

PHP使用redis实现统计缓存mysql压力的方法

2024-05-04 23:40:23
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了PHP使用redis实现统计缓存mysql压力的方法,涉及PHP操作MySQL数据库及使用Redis统计的相关技巧,需要的朋友可以参考下

本文实例讲述了PHP使用redis实现统计缓存mysql压力的方法。分享给大家供大家参考,具体如下:

 

 
  1. <?php 
  2. header("Content-Type:text/html;charset=utf-8"); 
  3. include 'lib/mysql.class.php'; 
  4. $mysql_obj = mysql::getConn(); 
  5. //redis  
  6. $redis = new Redis(); 
  7. $redis->pconnect('127.0.0.1', 6379); 
  8. if(isset($_SERVER['HTTP_REFERER'])){ 
  9. $url_md5 = md5($_SERVER['HTTP_REFERER']); 
  10. } 
  11. $adve_key = 'adve';  
  12. $adve_key_exists = 'adve_exists'; 
  13. if(!$redis->exists($adve_key_exists)){ 
  14. $list = $mysql_obj->fetch_array("select * from user_online_adve"); 
  15. if($list){ 
  16. foreach ($list as $key => $value) { 
  17. $url_hash = md5($value['adve_url']); 
  18. $adve_hash_key = $adve_key.":".$url_hash; 
  19. $id = $value['id']; 
  20. $redis->set($adve_hash_key,$id); 
  21. $redis->set($adve_key_exists,true); 
  22. //$redis->hmset($adve_hash_key, array('id' =>$id)); 
  23. //print_r($redis->get($adve_hash_key)); 
  24. } 
  25. } 
  26. } 
  27. $adve_new_key = $adve_key.':'.$url_md5; 
  28. if($redis->exists($adve_new_key)){ 
  29. $adve_plus = $adve_new_key.":plus" ; 
  30. if(!$redis->exists($adve_plus)){ 
  31. $redis->set($adve_plus,1);  
  32. }else{ 
  33. $redis->incr($adve_plus); 
  34. $num = $redis->get($adve_plus); 
  35. if($num >10){ 
  36. $id = $redis->get($adve_new_key); 
  37. // insert to sql; 
  38. $mysql_obj->query("update user_online_adve set adve_num=adve_num+$num where id=$id"); 
  39. $redis->set($adve_plus,1); 
  40. } 
  41. } 
  42. } 
  43. header('HTTP/1.0 301 Moved Permanently'); 
  44. header('Location: https://itunes.apple.com/cn/app/san-guo-zhi15-ba-wangno-da-lu/id694974270?mt=8'); 
  45. /* 
  46. if(){ 
  47. $adve_plus = $adve_key.":plus" ; 
  48. if($redis->exists($adve_plus)){ 
  49. $redis->incr($adve_plus); 
  50. }else{ 
  51. $redis->set($adve_plus,1);  
  52. } 
  53. echo $redis->get($adve_plus); 
  54. } 
  55. foreach ($list as $key => $value) { 
  56. $url_hash = md5($value['adve_url']); 
  57. $id = $value['id']; 
  58. $adve_num = $value['adve_num']; 
  59. $adve_plus = $adve_key.":plus" ; 
  60. if($redis->exists($adve_plus)){ 
  61. $redis->incr($adve_plus); 
  62. }else{ 
  63. $redis->set($adve_plus,1);  
  64. } 
  65. echo $redis->get($adve_plus); 
  66. //if($redis->) 
  67. //$redis->hmset($adve_key, array('id' =>$id, 'adve_num'=>$adve_num)); 
  68. //print_r($redis->hmget("adve:$url_hash", array('adve_num'))); 
  69. } 
  70. print_r($list); 
  71. */ 

希望本文所述对大家php程序设计有所帮助。


注:相关教程知识阅读请移步到PHP教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表