String

//
// Created by zhangrongxiang on 2018/3/7 13:48
// File string2
// #include <hiredis/hiredis.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

连接redis服务

int main() {
redisContext *context = redisConnect("127.0.0.1", 6379);
if (context == NULL || context->err) {
if (context) {
printf("%s\n", context->errstr);
} else {
printf("redisConnect error\n");
}
exit(EXIT_FAILURE);
}
printf("-----------------connect success--------------------\n");

SET key value

    char *key = "str";
char *val = "Hello World";
/*SET key value */
redisReply *reply = redisCommand(context, "SET %s %s", key, val);
if (reply->type == REDIS_REPLY_STATUS) {
/*SET str Hello World*/
printf("SET %s %s\n", key, val);
}
freeReplyObject(reply);

GET key

    /*GET key*/
reply = redisCommand(context, "GET %s", key);
if (reply->type == REDIS_REPLY_STRING) {
/*GET str Hello World*/
printf("GET str %s\n", reply->str);
/*GET len 11*/
printf("GET len %d\n", reply->len);
}
freeReplyObject(reply);

APPEND key value

    /*APPEND key value*/
char *append = " I am your GOD";
reply = redisCommand(context, "APPEND %s %s", key, append);
if (reply->type == REDIS_REPLY_INTEGER) {
printf("APPEND %s %s \n", key, append);
}
freeReplyObject(reply);
/*GET key*/
reply = redisCommand(context, "GET %s", key);
if (reply->type == REDIS_REPLY_STRING) {
//GET Hello World I am your GOD
printf("GET %s\n", reply->str);
}
freeReplyObject(reply);

INCR key

    /*INCR key*/
reply = redisCommand(context, "INCR counter");
if (reply->type == REDIS_REPLY_INTEGER) {
printf("INCR counter %lld\n", reply->integer);
}
freeReplyObject(reply);
reply = redisCommand(context, "INCR counter");
if (reply->type == REDIS_REPLY_INTEGER) {
printf("INCR counter %lld\n", reply->integer);
}
freeReplyObject(reply);

DECR key

    /*DECR key*/
reply = redisCommand(context, "DECR counter");
if (reply->type == REDIS_REPLY_INTEGER) {
printf("DECR counter %lld\n", reply->integer);
}
freeReplyObject(reply);
reply = redisCommand(context, "DECR counter");
if (reply->type == REDIS_REPLY_INTEGER) {
printf("DECR counter %lld\n", reply->integer);
}
freeReplyObject(reply);

DECRBY key decrement

    /*DECRBY key decrement*/
reply = redisCommand(context, "DECRBY counter 5");
if (reply->type == REDIS_REPLY_INTEGER) {
printf("DECRBY counter %lld\n", reply->integer);
}
freeReplyObject(reply);
reply = redisCommand(context, "DECRBY counter 5");
if (reply->type == REDIS_REPLY_INTEGER) {
printf("DECRBY counter %lld\n", reply->integer);
}
freeReplyObject(reply);

INCRBY key increment

    /*INCRBY key increment*/
reply = redisCommand(context, "INCRBY counter 5");
if (reply->type == REDIS_REPLY_INTEGER) {
printf("INCRBY counter %lld\n", reply->integer);
}
freeReplyObject(reply);
reply = redisCommand(context, "INCRBY counter 5");
if (reply->type == REDIS_REPLY_INTEGER) {
printf("INCRBY counter %lld\n", reply->integer);
}
freeReplyObject(reply);

ETRANGE key start end

    /*GETRANGE key start end*/
reply = redisCommand(context, "GETRANGE str 0 5");
if (reply->type == REDIS_REPLY_STRING) {
/*GETRANGE str Hello*/
printf("GETRANGE %s %s\n", key, reply->str);
}
freeReplyObject(reply);

GETSET key value

    /*GETSET key value*/
reply = redisCommand(context, "GETSET %s %s", key, val);
if (reply->type == REDIS_REPLY_STRING) {
/*GETSET str Hello World I am your GOD*/
printf("GETSET %s %s\n", key, reply->str);
}
/*INCRBYFLOAT key increment*/
reply = redisCommand(context, "INCRBYFLOAT f 2.1");
if (reply->type == REDIS_REPLY_STRING) {
printf("INCRBYFLOAT counter %s\n", reply->str);
}

MSET key value [key value ...]

    /*MSET key value [key value ...]*/
reply = redisCommand(context, "MSET k1 hello k2 world k3 good");
if (reply->type == REDIS_REPLY_STATUS) {
printf("MSET k1 hello k2 world k3 good\n");
}
freeReplyObject(reply);

MGET key [key ...]

    /*MGET key [key ...]*/
reply = redisCommand(context, "MGET k1 k2 k3");
if (reply->type == REDIS_REPLY_ARRAY) {
printf("MGET k1 k2 k3 \n");
redisReply **pReply = reply->element;
int i = 0;
size_t len = reply->elements;
//hello world good
for (; i < len; ++i) {
printf("%s ", pReply[i]->str);
}
printf("\n");
}
freeReplyObject(reply);

STRLEN key

    /*STRLEN key*/
reply = redisCommand(context, "STRLEN str");
if (reply->type == REDIS_REPLY_INTEGER) {
//1
printf("STRLEN str %lld \n", reply->integer);
}
/*SETEX key seconds value*/
reply = redisCommand(context, "SETEX s 30 30seconds");
if (reply->type == REDIS_REPLY_STATUS) {
printf("SETEX s 30 30seconds\n");
freeReplyObject(reply);
int i = 0;
while (i++ < 32) {
reply = redisCommand(context, "GET s");
if (reply->type == REDIS_REPLY_STRING) {
printf("%d s %s\n", i, reply->str);
} else if (reply->type == REDIS_REPLY_NIL) {
printf("%d s nil\n", i);
}
freeReplyObject(reply);
sleep(1);
/*
* 29 s 30seconds
* 30 s 30seconds
* 31 s nil
* 32 s nil
*/
}
}

redisFree

    redisFree(context);
return EXIT_SUCCESS;
}

struct

#define REDIS_REPLY_STRING 1
#define REDIS_REPLY_ARRAY 2
#define REDIS_REPLY_INTEGER 3
#define REDIS_REPLY_NIL 4
#define REDIS_REPLY_STATUS 5
#define REDIS_REPLY_ERROR 6 /* This is the reply object returned by redisCommand() */
typedef struct redisReply {
int type; /* REDIS_REPLY_* */
long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
int len; /* Length of string */
char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
} redisReply;

All rights reserved

Redis之hiredis API (String)的更多相关文章

  1. Redis C客户端API - God's blog - 博客频道 - CSDN.NET

    Redis C客户端API - God's blog - 博客频道 - CSDN.NET Redis安装步骤: 1.redis server安装 wget http://redis.googlecod ...

  2. ***Redis hash是一个string类型的field和value的映射表.它的添加、删除操作都是O(1)(平均)。hash特别适合用于存储对象

    http://redis.readthedocs.org/en/latest/hash/hset.html HSET HSET key field value   (存一个对象的时候key存) 将哈希 ...

  3. Redis学习笔记(2)-String

    package cn.com; import java.util.List; import redis.clients.jedis.Jedis; public class Redis_String { ...

  4. 常用API String

    Java的API以及Object类 Java的API Java的API(API: Application(应用) Programming(程序) Interface(接口)) Java API就是JD ...

  5. redis 五大数据类型之string篇

    一: 1:string(字符串) string是redis最基本的类型,你可以理解成与Memcached一模一样的类型,一个key对应一个value. string类型是二进制安全的.意思是redis ...

  6. redis - redis数据结构与API

    通用命令 keys:遍历所有的key[keys一般不再生产环境使用],时间复杂度O(n) keys * keys he* keys he[h-l]* keys ph? dbsize:计算key的总数, ...

  7. Redis hash 是一个 string 类型的 field 和 value 的映射表.它的添加、删除操作都是 O(1)(平均)。

    2.3 hashes 类型及操作 Redis hash 是一个 string 类型的 field 和 value 的映射表.它的添加.删除操作都是 O(1)(平均).hash 特别适合用于存储对象.相 ...

  8. Redis启动服务和String常用命令

    Redis启动服务和String常用命令 1. 启动Redis服务 E:\redis>redis-server.exe redis.windows.conf _._ _.-``__ ''-._ ...

  9. Java常用API(String类)

    Java常用API(String类) 概述: java.lang.String 类代表字符串.Java程序中所有的字符串文字(例如 "abc" )都可以被看作是实现此类的实例 1. ...

随机推荐

  1. 我的"gethup"(GitHub)注册之旅

    大家好,我叫张琪琪,来自网络工程143(学号1413042062),.平时喜欢运动,也喜欢看电视尤其是动漫.其实对于自己的编程能力没有多大自信,如果看着题目回忆课本内容写下的程序也算的话,那是敲过不少 ...

  2. EJB3.0 EJB开发消息驱动bean

    (7)EJB3.0 EJB开发消息驱动bean JMS 一: Java消息服务(Java Message Service) 二:jms中的消息 消息传递系统的中心就是消息.一条 Message 由三个 ...

  3. 启动hive命令时指定参数或自定义参数

    启动hive命令时指定参数或自定义参数 在hive启动命令中指定一个参数 hive --hiveconf hive.job.submit.username=fuxin.zhao -e "se ...

  4. Oracle数据泵导出导入(expdp/impdp)

    一.创建表空间 create tablespace atp logging datafile 'D:\oracle\oradata\orcl\atp.dbf' size 50m autoextend ...

  5. 会HTML/CSS就可以轻松创建网站

    网站其本质就是HTML + CSS 外加一些JavaScript构成的.所以基本上只要你会一些前端,就可以开始花样搭网站了. 如果只用HTML/CSS那做出来的网站只能叫静态网站,性能好但维护不方便, ...

  6. ES6——Class 的基本使用

    Class 语法. class 关键字声明一个类,之后以这个类来实例化对象. const Miaov=function(a,b){ this.a=a; this.b=b; return this; } ...

  7. deepin获取root权限

    ctrk+alt+t 打开终端 输入 sudo passwd root mywork@mywork-PC:~$ sudo passwd root[sudo] mywork 的密码: [sudo] 输入 ...

  8. ubuntu 关闭和开启防火墙

    1.关闭ubuntu的防火墙 ufw disable 2开启防火墙 ufw enable 3.卸载了iptables apt-get remove iptables 4.关闭ubuntu中的防火墙的其 ...

  9. JZOJ6096 森林

    题目传送门 Description ​我们定义对一棵树做一次变换的含义为:当以 1 号节点为根时,交换两个互相不为祖先的点的子树: ​一棵树的权值为对它进行至多一次变换能得到的最大直径长度: ​初始时 ...

  10. Python3.5 学习五

    心灵鸡汤电影推荐: 阿甘正传.辛德勒名单.肖申克的救赎.勇敢的心.角斗士.美国丽人.教父.钢琴师.指环王.西雅图不眠夜.廊桥遗梦.可可西里的美丽传说.放牛班的春天.血钻.战争之王.上帝之城.中央车站. ...