标签:
问题背景: php读取线上redis数据,经常不稳定,数据响应时有时无。 
解决方法:多次读取,每次读取所有上一次没读出的数据,直到全部获取。
本文实现用shell进行多次redis数据读取, 每次取出其中的有效值(对于我们的例子中,就是给key,能在redis上取得其value的为有效值,其他无效),并将无效值重跑一遍,以此迭代,直到所有redis数据被取出。PS:redis数据可以由php或C读出,给定接口的话非常简单,具体可以参考phpredis,,由于可能涉密,本文中不给出这块的实现。 
方法概述: 
项目结构: 
 
具体实现: 
 
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进行定时,进一步自动化执行。
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/abcjennifer/article/details/46993129