• redis


    The Little Redis Book

    https://www.openmymind.net/redis.pdf

     

    1、redis服务端

    1)redis编译
    wget http://download.redis.io/releases/redis-4.0.11.tar.gz
    tar xvf redis-4.0.11.tar.gz
    cd redis-4.0.11
    #make MALLOC=libc
    make MALLOC=Jemalloc
    make install PREFIX=/usr/local/redis
    sudo mkdir -p /usr/local/redis/etc
    sudo cp redis.conf /usr/local/redis/etc
    vi /etc/profile
    export PATH=$PATH:/usr/local/redis/bin
    source /etc/profile
    更改redis.conf文件中的daemonize和stop-writes-on-bgsave-error
    daemonize yes
    stop-writes-on-bgsave-error no
    /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
    查看redis服务是否启动
    ps -ef | grep -i redis

    2)redis交叉编译
    tar xvf redis-4.0.11.tar.gz
    cd redis-4.0.11
    export CC=arm-linux-gnueabihf-gcc
    export CXX=arm-linux-gnueabihf-g++
    export LD=arm-linux-gnueabihf-ld
    export RAINLIB=arm-linux-gnueabihf-rainlib
    export AR=arm-linux-gnueabihf-ar
    export LINK=arm-linux-gnueabihf-g++
    #make MALLOC=libc
    make MALLOC=Jemalloc
    make install PREFIX=_install
    mkdir _install/etc
    sudo cp redis.conf _install/etc
    vi /etc/profile
    export PATH=$PATH:/usr/local/xxx/bin
    source /etc/profile
    ./redis-server /usr/local/xxx/redis/etc/redis.conf

    3)简单的使用
    //首先链接客户端
    [root@localhost redis]# redis-cli
    //检查网络是否可以
    127.0.0.1:6379> ping PONG
    //设置一个键值对
    127.0.0.1:6379> set name cheny
    OK
    //获取刚刚设置的键值对
    127.0.0.1:6379> get name
    "cheny"
    //查看所有的键
    127.0.0.1:6379> keys *
    1) "name"
    //删除name这个键
    127.0.0.1:6379> del name
    (integer) 1
    127.0.0.1:6379> keys *
    (empty list or set)
    127.0.0.1:6379>

    更多指令

    https://redis.io/commands/dump

    4) Clients 

    https://redis.io/clients

    2、python client

    sudo apt-get install python-pip
    sudo pip install virtualenv
    virtualenv -p /usr/bin/python env
    source env/bin/activate
    pip install redis

    test.py

    import redis
    client = redis.StrictRedis(host='127.0.0.1', port=6379)
    key = "hello"
    client.set(key, "python-redis")
    value = client.get(key)
    print "key:" + key + ", value:" + value 

    test1.py(watch竞争机制)

    import redis
    from redis import WatchError
    r = redis.StrictRedis(host='127.0.0.1', port=6379)
    
    def redis_set_key(key, data):
        with r.pipeline() as pipe:
            while 1:
                try:
                    # put a WATCH on the key that holds our sequence value
                    pipe.watch(key)
                    # after WATCHing, the pipeline is put into immediate execution
                    # mode until we tell it to start buffering commands again.
                    # this allows us to get the current value of our sequence
                    current_value = pipe.get(key)
                    #next_value = int(current_value, base=10) + 1
                    #next_value = float(current_value) + 1
                    #next_value = unicode(int(current_value) + 1)
                    next_value = data
                    # now we can put the pipeline back into buffered mode with MULTI
                    pipe.multi()
                    pipe.set(key, next_value)
                    current_value = pipe.get(key)
                    # and finally, execute the pipeline (the set command)
                    pipe.execute()
                    # if a WatchError wasn't raised during execution, everything
                    # we just did happened atomically.
                    break
                except WatchError:
                    # another client must have changed 'OUR-SEQUENCE-KEY' between
                    # the time we started WATCHing it and the pipeline's execution.
                    # our best bet is to just retry.
                    continue
        return current_value
    
    redis_set_key('battery',9.99)
    redis_set_key('4g',333)

    3、nodejs client

    npm install redis --save

    test.js

    const redis = require('redis')
    const client = redis.createClient(6379, 'localhost')
    client.set('hello', 'node-redis')
    client.get('hello', function(err, value){ console.log(value) })

     test.js(watch竞争机制)

    const redis = require('redis')
    const client = redis.createClient(6379, 'localhost')
    
    client.watch("battery")
    
    client.get('battery', function(err, value){ console.log(value) })
    client.get('4g', function(err, value){ console.log(value) })
    
    var multi = client.multi()
    
    multi.set("battery",6.66) 
    multi.set("battery",777)
    
    multi.exec(function(err,replies){
        console.log(replies) 
    })
    
    client.get('battery', function(err, value){ console.log(value) })
    client.get('4g', function(err, value){ console.log(value) })
    
    client.quit();

    4、redis c语言客户端

    https://redislabs.com/lp/hiredis/

    1)hiredis编译
    make
    make install PREFIX=_install

    sudo tar xvf _install.tar.gz

    cd /usr/local/

    sudo tar xvf _install.tar.gz

    sudo mv _install hiredis

    export LD_LIBRARY_PATH=/usr/local/hiredis/lib:$LD_LIBRARY_PATH

    example

    https://github.com/redis/hiredis/tree/master/examples

    example.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include <hiredis.h>
    
    const char *hostname = "127.0.0.1";
    const int port = 6379;
    
    redisContext *c;
    redisReply *reply;
    
    int redis_connect(void)
    {
        struct timeval timeout = { 1, 500000 }; // 1.5 seconds
        c = redisConnectWithTimeout(hostname, port, timeout);
        if (c == NULL || c->err) {
            if (c) {
                printf("Connection error: %s
    ", c->errstr);
                redisFree(c);
            } else {
                printf("Connection error: can't allocate redis context
    ");
            }
            return -1;
        }
    
        /* PING server */
        reply = redisCommand(c,"PING");
        printf("PING: %s
    ", reply->str);
        freeReplyObject(reply);
        return 0;
    }
    
    int main() {
       
        if(redis_connect() != 0)
            return -1;
    
        /* Set a key */
        float battery_value = 5.55;
        reply = redisCommand(c,"SET %s %f", "battery",battery_value);
        printf("SET: %s
    ", reply->str);
        freeReplyObject(reply);
    
        /* Set a key */
        int net_signal_value = 666;
        reply = redisCommand(c,"SET %s %d", "4g",net_signal_value);
        printf("SET: %s
    ", reply->str);
        freeReplyObject(reply);
    
        /* Try a GET */
        reply = redisCommand(c,"GET battery");
        printf("GET battery: %s
    ", reply->str);
        freeReplyObject(reply);
    
        /* Try a GET */
        reply = redisCommand(c,"GET 4g");
        printf("GET 4g: %s
    ", reply->str);
        freeReplyObject(reply);
    
    
        /* Disconnects and frees the context */
        redisFree(c);
    
        return 0;
    }

    example.c(watch竞争机制)

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include <hiredis.h>
    
    const char *hostname = "127.0.0.1";
    const int port = 6379;
    
    redisContext *c;
    redisReply *reply;
    
    int redis_connect(void)
    {
        struct timeval timeout = { 1, 500000 }; // 1.5 seconds
        c = redisConnectWithTimeout(hostname, port, timeout);
        if (c == NULL || c->err) {
            if (c) {
                printf("Connection error: %s
    ", c->errstr);
                redisFree(c);
            } else {
                printf("Connection error: can't allocate redis context
    ");
            }
            return -1;
        }
    
        /* PING server */
        reply = redisCommand(c,"PING");
        printf("PING: %s
    ", reply->str);
        freeReplyObject(reply);
        return 0;
    }
    
    int main() {
        if(redis_connect() != 0)
            return -1;   
    
        reply = redisCommand(c, "watch battery");
        printf("watch battery: %s
    ", reply->str);
        freeReplyObject(reply);
    
        reply = redisCommand(c, "multi");
        printf("multi: %s
    ", reply->str);
        freeReplyObject(reply);
    
        float battery_value = 2.222;
        reply = redisCommand(c,"SET %s %f", "battery",battery_value);
        printf("SET: %s
    ", reply->str);
        freeReplyObject(reply);
    
        int net_signal_value = 888;
        reply = redisCommand(c,"SET %s %d", "4g",net_signal_value);
        printf("SET: %s
    ", reply->str);
        freeReplyObject(reply);
    
        reply = redisCommand(c, "exec");
        printf("exec: %s
    ", reply->str);
        freeReplyObject(reply);
    
        reply = redisCommand(c,"GET battery");
        printf("GET battery: %s
    ", reply->str);
        freeReplyObject(reply);
    
        reply = redisCommand(c,"GET 4g");
        printf("GET 4g: %s
    ", reply->str);
        freeReplyObject(reply);
    
        redisFree(c);
    
        return 0;
    }

    gcc -o example example.c -I ./_install/include/hiredis -L ./_install/lib -lhiredis
    ./example

    export LD_LIBRARY_PATH=${pwd}/_install/lib:$LD_LIBRARY_PATH
    gcc -o example example.c -I ./_install/include/hiredis ./_install/lib/libhiredis.a

    2)hiredis交叉编译
    export CC=arm-linux-gnueabihf-gcc
    export CXX=arm-linux-gnueabihf-g++
    export LD=arm-linux-gnueabihf-ld
    export RAINLIB=arm-linux-gnueabihf-rainlib
    export AR=arm-linux-gnueabihf-ar
    export LINK=arm-linux-gnueabihf-g++
    make
    make install PREFIX=_install
    arm-linux-gnueabihf-gcc -o example example.c -I ./_install/include/hiredis -L ./_install/lib -lhiredis
    export LD_LIBRARY_PATH=/usr/local/hiredis/lib:$LD_LIBRARY_PATH
    ./example

    ## rdb config
    https://blog.csdn.net/codeliang20/article/details/107869852
    https://blog.csdn.net/li1325169021/article/details/99243921

    5、redis竞争

    https://blog.csdn.net/happy_wu/article/details/78736641

    网友的总结

    http://www.cnblogs.com/Survivalist/p/8119891.html

  • 相关阅读:
    属性的简单了解
    深入方法(29)- 传址参数不能赋予常量
    深入方法(27)- 递归函数: 简单示例
    MySQL 中文字符集排序
    Yii2 Apache + Nginx 路由重写
    DQL、DML、DDL、DCL的概念与区别
    php获取指定日期的前一天,前一月,前一年日期
    PHP 获取两个时间之间的月份
    PHP 调试工具Xdebug安装配置
    Nginx 反向代理、负载均衡
  • 原文地址:https://www.cnblogs.com/dong1/p/9773384.html
Copyright © 2020-2023  润新知