问题背景: php读取线上redis数据,常常不稳定,数据响应时有时无。

解决方法:多次读取。每次读取全部上一次没读出的数据,直到全部获取。

本文实现用shell进行多次redis数据读取, 每次取出当中的有效值(对于我们的样例中,就是给key,能在redis上取得其value的为有效值。其它无效),并将无效值重跑一遍,以此迭代,直到全部redis数据被取出。PS:redis数据能够由php或C读出,给定接口的话很easy,详细能够參考phpredis。。因为可能涉密,本文中不给出这块的实现。


  1. 方法概述:

    1. 将source中的key分为N个文件。丢入redis并行get value
    2. 实时统计N个文件get value输出结果的总行数,以及当中有效的行数
    3. N个文件统计结束后, 将其全部结果合并,放入result/step/stepx.res
    4. 删除原先并行的的source文件,结果文件
    5. 将result中未获取到的key放入source/step2/contsigns。作为下一轮的输入。再次将其分为N个文件,运行(这里的contsign就是redis的key)
    6. 最后将各个step所得结果都写入final.res文件。cat result/steps/step*.res >> final.res
  2. 项目结构:



    • getredis.php: 实现获取redis数据
    • all.sh: 主程序,并行运行getredis.php;
    • analyze_result.sh: 实时分析redis获取数据运行情况(第2步), 加參数后实现上面的第3-5步(详细见下一节凝视);
    • source/:存储输入数据,当中all/下为原始全部redis的输入。 step2/为这一轮中未获取到的key,将作为下一轮获取redis的输入, 其余(如xaa)为当前这一轮中key分成的N个文件;
    • result/: 存储结果。当中source/包括当前一轮source下全部N个文件的输出;steps/包括各轮输出后的合并结果
  3. 详细实现:




    all.sh :

#Author:Rachel Zhang
#Email: zrqjennifer@sina.com for file in source/*
do
{
echo "processing source $file"
if test -f $file
then
php getredis.php $file > result/$file
fi
echo "$file done..."
}&
done

analyze_result.sh:

#Author:Rachel Zhang
#Email: zrqjennifer@sina.com Filefolder=result/source/*
#Filefolder=source/* echo "##################################"
echo " In Folder $Filefolder"
echo "##################################"
nl=0
hv=0
for file in $Filefolder
do
if test -f $file
then
fline=`wc -l $file | awk '{print $1}'`
nl=$(($nl+$fline))
fvalue=`cat $file |awk 'BEGIN{x=0;}{if($2) x=x+1;}END{print x;}'`
hv=$(($hv+$fvalue))
fi
done echo "totally $nl lines"
echo "$hv lines have tag value" ##################################
#combine results into one file if [ "$#" -gt 0 ]
then
if test -e "result/all.result"
then
mv result/all.result result/all.result.bak
fi for file in $Filefolder
do
if test -f $file
then
cat $file >> result/all.result
fi
done
echo "all the output are write in result/all.result" # put null-value keys into source/step2/contsigns
if test -e source/step2
then
mkdir source/step2
fi
cat result/all.result| awk '{if($2==null) print}' > source/step2/contsigns
Nnull_value=`wc -l source/step2/contsigns | awk '{print $1}'`
echo "remaining ${Nnull_value} keys to be processed" # put has-value key-value into result/steps/step${step_id}.res
step_id=1
while test -e result/steps/step${step_id}.res
do
step_id=$(($step_id+1))
done
cat result/all.result | awk '{if($2) print}' > result/steps/step${step_id}.res
echo "current valid results are writen in result/steps/step${step_id}.res" # remove the current source files, generate new source files and re-run all.sh
echo "remove current source and result files? (y/n)"
read answer
if [ $answer == "y" ]; then
if [ $Nnull_value -gt 10 ]; then
rm source/*
rm result/source/*
cd source && split -l 5000 step2/contsigns && cd ../
echo "now re-run sh ./all.sh? (y/n)"
read answer
if [ $answer == "y" ]; then
sh all.sh
fi
fi
fi
fi

PS: 以上analyze_result.sh 还能够去掉analyze_result.sh中的interactive部分(无需用户输入)。通过crontab进行定时,进一步自己主动化运行。

最新文章

  1. [转]浏览器退出之后php还会继续执行么?
  2. java并发编程(九)死锁
  3. centos网卡配置和防火墙停止和启动
  4. 准备熟悉Kaggle -菜鸟进阶
  5. Multipart to single part feature
  6. 转载:页面加载swf插件:swfobject
  7. 微信变声器(WeChat Voice)会是营销新利器吗
  8. C# winform DataTable 批量数据处理 增、删、改 .
  9. Lucene学习之CURD
  10. HTTP的GET方法模拟
  11. 搭建腾讯云Linux服务器(Centos6)入门教程
  12. C# 简单内存补丁
  13. 利用PowerDesigner15在win7系统下对MySQL 进行反向工程(二)
  14. 扩展Lucas定理
  15. Docker镜像拉不下来?试试这些
  16. pip3 install mysqlclient 报错 “/bin/sh: 1: mysql_config: not found”的解决方法
  17. ---Intel SSD 750 under Linux
  18. 【代码片段】如何使用CSS来快速定义多彩光标
  19. html5界面手机播放mp3
  20. 9 云计算系列之Cinder的安装与NFS作为cinder后端存储

热门文章

  1. PyCharm使用指南及更改Python pip源为国内豆瓣
  2. 在Redux中使用插件createAction之后
  3. dede手机访问网站跳转到手机端模板
  4. win7 中使用NFS共享
  5. Hibernate+Spring整合开发步骤
  6. ThinkPHP---TP功能类之公文管理功能2----------继续完善
  7. 使用JS获取request参数
  8. 02Oracle Database 安装,卸载,系统服务,系统组件及系统表空间
  9. Cash Machine POJ - 1276
  10. Go:值类型、引用类型