注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Computer Science

I Pressed My Words Here. Will You Read ?

 
 
 

日志

 
 

redisDo脚本  

2016-10-19 10:55:23|  分类: My Projects |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
redis读写脚本
redis-cli set [key] [value] 就可以实现redis的写入。但是当value超过一定长度时。便会需要 append命令。这时,就需要一个脚本来简化操作。
#!/bin/bash
redisGet(){
        ${reidsCliPath}/redis-cli GET ${keyName};
}
redisGetLen(){
        ## local reidsCliPath=${1:-"${reidsCliPath}"};
        ## local keyName=${2:-"${keyName}"};
        local data=$(${reidsCliPath}/redis-cli GET ${keyName})
        echo ${#data}
}
redisDel(){
        ${reidsCliPath}/redis-cli DEL "${keyName}"
}
redisSet(){
        local value=${2};
        local time=${3-"8640000"};
        ${reidsCliPath}/redis-cli SET "${keyName}" "${value}" EX ${time} NX
}
redisSetFromFile(){
        ## local path=$(cd `dirname $PWD/$0`;pwd);
        ## path=${0%/*};
        local sourceRedisDataFileDefaultName="homepageInternetBar."$(date --date="" +%Y%m%d)".a.data";
        local filePath=${path}/${sourceRedisDataFileName-"${sourceRedisDataFileDefaultName}"};
        filePath=${2-"${filePath}"};
        local time=${3-8640000};
        ${reidsCliPath}/redis-cli SET "${keyName}" "" EX ${time} NX
        local fileContent=$(cat ${filePath})
        # local fileContent="$(cat $path/redisTmp.data)";
        local ifs=$IFS
        IFS="
";
        echo "" > ${lastRedisDataLog}
        for fc in ${fileContent}
        do
                echo ${fc} >> ${lastRedisDataLog}
                ${reidsCliPath}/redis-cli -h 127.0.0.1 -p 6379 APPEND "${keyName}" ${fc}
        done
        IFS=${ifs};
}
redisKeys(){
        ## local reidsCliPath=/usr/local/bin;
        ${reidsCliPath}/redis-cli KEYS "${keyName}"
}
redisSetNew(){
        ## local path=$(cd `dirname $PWD/$0`;pwd);# won't work in crontab
        ## path=${0%/*};# will work in crontab
        ## local logPath=${path}/$(date --date="" +%Y%m%d)_redis.log;
        local sourceRedisDataFileDefaultName="homepageInternetBar."$(date --date="" +%Y%m%d)".a.data";
        local sourceRedisDataFileName=${sourceRedisDataFileName-"${sourceRedisDataFileDefaultName}"};
        if [ -f ${path}/${sourceRedisDataFileName} ]
        then
                redisDel ${@:1} >> ${logPath};
                redisKeys ${@:1} >> ${logPath};
                redisSetFromFile ${@:1} >> ${logPath};
        else
                echo "no source redis data file found."
        fi
        # redisGet ${@:1};
        redisKeys ${@:1} >> ${logPath};
        redisGetLen ${@:1} >> ${logPath};
}
redisDo(){
        local path=$(cd `dirname $PWD/$0`;pwd);# won't work in crontab
        path=${0%/*};# will work in crontab
        local logPath=${path}/$(date --date="" +%Y%m%d)_redis.log;
        local reidsCliPath=/usr/local/bin;
        local keyName="l2_redis_backend_home_advertising";
        keyName=${2-"${keyName}"};
        local lastRedisDataLog=${path}/lastRedisData.log
        local action=${1};
        case "${action}" in
                "del") redisDel "${@:2}" >> ${logPath} ;;
                "getLen") redisGetLen "${@:2}" >> ${logPath} ;;
                "get") redisGet "${@:2}" >> ${logPath} ;;
                "keys") redisKeys "${@:2}" >> ${logPath} ;;
                "set") redisSet "${@:2}" >> ${logPath} ;;
                "setFile") redisSetFromFile "${@:2}" >> ${logPath} ;;
                "setNew") redisSetNew "${@:2}" ;;
                "t0") test0 "${@:2}" ;;
                "t1") test1 "${@:2}" ;;
                "t2") test2 "${@:2}" ;;
                *) echo "del/getLen/get/keys/set/t"
        esac
}
test0(){
        echo "0:"${0}
        echo "1:"${1}
        echo "2:"${2}
        redisDel ${@}
        local value=${2};
        local time=${3-8640000};
        ${reidsCliPath}/redis-cli SET "${keyName}" "${value}" EX ${time} NX
        redisGet ${@}
}
test1(){
        echo $(cd `dirname $PWD/$0`;pwd);
        echo ${0%/*}
        echo "0:"${0}
        echo "1:"${1}
        echo "2:"${2}
}
test2(){
        test1 "${@:1}";
}
redisDo "$@"

./redisDo.sh t0 "tmp_a" "Hello World"

得到:
0:./redisDo.sh
1:tmp_a
2:Hello World
(integer) 1
OK
"Hello World"

Press ENTER or type command to continue
  评论这张
 
阅读(63)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017