• Ubuntu环境下的Redis 配置与C++使用入门


      Redis是一个高性能的key-value数据库。 Redisedis的出现,非常大程度补偿了memcached这类key/value存储的不足,在部分场合能够对关系数据库起到非常好的补充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等client,使用非常方便。

        本文将通过介绍Ubuntu环境下的Redis 配置,介绍C++入门使用的方式,帮助读者高速上手Redis。

    安装配置

    获取源代码、解压、进入源代码文件夹,编译

    <code>$ wget http://download.redis.io/redis-stable.tar.gz
    $ tar xvzf redis-stable.tar.gz
    $ cd redis-stable
    $ make</code>


    測试、安装

    $ make test
    $ sudo make install

    make命令运行完毕后,会在 src 文件夹下生成本个可运行文件,各自是redis-server、redis-cli、redis-benchmark、redis-stat,它们的作用例如以下:

    redis-server         :Redis服务器的daemon启动程序
    redis-cli                :Redis命令行操作工具。当然,你也能够用telnet依据其纯文本协议来操作
    redis-benchmark  :Redis性能測试工具,測试Redis在你的系统及你的配置下的读写性能
    redis-stat              :Redis状态检測工具,能够检測Redis当前状态參数及延迟状况。

    Run Redis with:

    $ src/redis-server

    You can interact with Redis using the built-in client:

    $ src/redis-cli
    redis> set foo bar
    OK
    redis> get foo
    "bar"

    或者參见 http://redis.io/topics/quickstart

    C++配置与入门使用

    配置

    on Ubuntu:

    $ apt-get install libhiredis-dev

    On OSX run:

    $ brew install hiredis

    on other platforms:

    $ git clone https://github.com/antirez/hiredis.git && cd hiredis && make && sudo make install && sudo ldc

    否则,因为缺少hiredis依赖关系,可能出现错误:

    error: hiredis/hiredis.h: No such file or directory

    入门使用

    redisTest.cpp

    #include <stdio.h>
    #include <hiredis/hiredis.h>
    
    int main()
    {
        redisContext* conn = redisConnect("127.0.0.1",6379);
        if(conn->err){
            printf("connection error:%s
    ",conn->errstr);
        }
      
        redisReply* reply = (redisReply*)redisCommand(conn,"set foo 1234");
        freeReplyObject(reply);
    
        reply = (redisReply*)redisCommand(conn,"get foo");
        printf("%s
    ",reply->str);
        freeReplyObject(reply);
    
        redisFree(conn);
        return 0;
    }


    编译命令

    g++ redisTest.cpp -o redisTest -I./deps/hiredis/ -L./deps/hiredis/ -lhiredis


    进阶请看源代码与測试用例: https://github.com/redis/hiredis微笑


  • 相关阅读:
    SIP头域说明
    android.os.NetworkOnMainThreadException异常
    "Only the original thread that created a view hierarchy can touch its views"引发的思考_Handler的使用
    sip命令与音视频rtp通话完整流程分析
    在Windows下搭建Android开发环境(摘自百度经验)
    jquery dialog 打开的时候自动聚焦解决方法
    对于数组(字符串)slice方法的总结
    SWIFT语言中的泛型编程 【GENERIC】【PART 2】
    手动调用playground的XCPCaptureValue展示Swift过程数据
    Swift语言相关资源贴
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4211951.html
Copyright © 2020-2023  润新知