ssdb支持 zset, map/hash, list, kv 数据结构,同redis差不多。下面是关于ssdb hsahmap的使用笔记

1.ssdb hashmap的命令

1.hset name key value

设置hashmap中指定key的值

2.hget name key

获取hashmap中指定key的值

3.hdel name key

删除hashmap中指定的key

4.hincr name key [num]

使hashmap中key对应的值增加num

5.hexists name key

判断指定的key是否存在于hashmap中

6.hsize name

返回hashmap中元素的个数

7.hlist name_start name_end limit

列出名字处于区间 (name_start, name_end] 的 hashmap.

8.hrlist name_start name_end limit

逆序

9.hkeys name key_start key_end

列出 hashmap 中处于区间 (key_start, key_end] 的 key 列表.

10.hgetall name

返回整个 hashmap.

11.hscan name key_start key_end limit

列出 hashmap 中处于区间 (key_start, key_end] 的 key-value 列表.

12.hrscan name key_start key_end limit

像 hscan, 逆序.

13.hclear name

删除 hashmap 中的所有 key.

14.multi_hset name key1 value1 key2 value2 ...
批量设置 hashmap 中的 key-value.

15.multi_hget name key1 key2 ...
批量获取 hashmap 中多个 key 对应的权重值.

16.multi_hdel name key1 key2 ...

指删除 hashmap 中的 key.

2.ssdb使用hashmap 存储获取的代码

  这里因为ssdb是完全兼容redis的,所以完全可以用redis的库的接口函数。因为这样的特性,使得使用redis的项目很容易的能迁移到ssdb,作者的原博客也有完整的redis迁移到ssdb的完整教程。

ssdb的连接

redisContext *conn = redisConnect(host,port);
host:连接ssdb的主机ip
port:连接端口 返回值 redisContext结构体 定义如下
/* Context for a connection to Redis */
2 typedef struct redisContext {
3 int err; /* Error flags, 0 when there is no error */
4 char errstr[128]; /* String representation of error when applicable */
5 int fd;
6 int flags;
7 char *obuf; /* Write buffer */
8 redisReader *reader; /* Protocol reader */
9
10 enum redisConnectionType connection_type;
11 struct timeval *timeout;
12
13 struct {
14 char *host;
15 char *source_addr;
16 int port;
17 } tcp;
18
19 struct {
20 char *path;
21 } unix_sock;
22
23 } redisContext; ssdb的命令执行
redisReply* reply = redisCommand(conn,cmd)
conn:redis的连接句柄
cmd:执行的命令
返回值:redisReply结构体 结构体如下:
1 /* This is the reply object returned by redisCommand() */
2 typedef struct redisReply {
3 int type; /* REDIS_REPLY_* */
4 long long integer; /* The integer when type is REDIS_REPLY_INTEGER */
5 size_t len; /* Length of string */
6 char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */
7 size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
8 struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
9 } redisReply; 其中type元素的类型可用来判断放回结果的状态:
REDIS_REPLY_STRING 1   //返回字符串,查看str,len字段
REDIS_REPLY_ARRAY 2    //返回一个数组,查看elements的值(数组个数),通过element[index]的方式访问数组元素,每个数组元素是一个redisReply对象的指针
REDIS_REPLY_INTEGER 3  //返回整数,从integer字段获取值
REDIS_REPLY_NIL 4      //没有数据返回
REDIS_REPLY_STATUS 5   //表示状态,内容通过str字段查看,字符串长度是len字段
REDIS_REPLY_ERROR 6    //表示出错,查看出错信息,如上的str,len字段

redisReply的释放
freeReplyObject(reply); 
用来释放执行命令后所占用的内存 ssdb连接的释放
redisFree(conn);
完整代码如下:

 redisContext*  ssdb_init(const char* host,uint16_t port) {
redisContext *conn = redisConnect(host,port);
if(conn->err) {
printf("connection error:%s\n",conn->errstr);
redisFree(conn);
return NULL;
}
return conn;
} void ssdb_finit(redisContext *conn) {
if (conn)
redisFree(conn);
} redisReply* ssdb_command(redisContext *conn,char *cmd) {
redisReply* reply = NULL;
if(NULL != conn && cmd != NULL) {
reply = redisCommand(conn,cmd);
} else {
printf("redis err\n");
return NULL;
}
return reply;
} int main(){
char buf[] = {};
char name[] = {};
char key[] = {};
char value[] = {};
redisReply* reply = NULL;
redisContext *ssdb_conn = ssdb_init("127.0.0.1",);
if(ssdb_conn == NULL){
printf("ssdb conn err...\n");
return ;
}
memcpy(name,"mymap",);
memcpy(key,"name",);
memcpy(value,"wangwu",);
snprintf(buf,sizeof(buf),"hset %s %s %s",name,key,value);
reply = ssdb_command(ssdb_conn,buf);
if(reply != NULL){
if(reply->type == REDIS_REPLY_ERROR)
printf("commd err:%s\n",reply->str);
freeReplyObject(reply);
} //根据指定key获取value值
memset(buf,,sizeof(buf));
memcpy(buf,"hget mymap name",sizeof(buf));
reply = ssdb_command(ssdb_conn,buf);
if(reply == NULL)
return ;
if(reply->type == REDIS_REPLY_ERROR){
printf("commd err:%s\n",reply->str);
freeReplyObject(reply);
return -;
}
printf("the value this is:%s\n",reply->str);
freeReplyObject(reply); //获取mymap里所有的key-value值
memset(buf,,sizeof(buf));
memcpy(buf,"hgetall mymap",sizeof(buf));
reply = ssdb_command(ssdb_conn,buf);
if(reply == NULL)
return ;
int i = ;
     /*此处对于获取的hashmap来说这个返回的数组每个key-value是占有两个元素的第一个元素为key 后一个元素为前一个元素的值*/
for(i = ;i < reply->elements;i+=){
printf("key:%s values:%s\n",reply->element[i]->str,reply->element[i + ]->str);
} freeReplyObject(reply);
return ;
}

执行效果图如下:

最新文章

  1. php报表使用
  2. 可以这样去理解group by和聚合函数
  3. Qt 官方一键动态发布技能
  4. ida GDB 远程调试
  5. 项目SVN的IP地址发生变化时修改SVN为新的IP地址
  6. Unity3d + UGUI 的多分辨率适配
  7. Mybatis 动态获取字段值(不需要创建javabean)
  8. Spring.NET 的IOC(依赖注入)
  9. haproxy+tomcat集群搭建
  10. Redis Windows版安装详解
  11. Redis入门_上
  12. angularjs中的几种工具方法
  13. express session 和 socketio session关联
  14. 运维rpm语法
  15. DOM1级问题与DOM2级事件
  16. HashMap的底层实现原理
  17. Matplotlib 知识点整理
  18. [转]分布式系统唯一ID生成方案汇总
  19. 剖析管理所有大数据组件的可视化利器:Hue
  20. BZOJ4897: [Thu Summer Camp2016]成绩单【DP of DP】

热门文章

  1. vo优化总结
  2. [算法]Evaluate Reverse Polish Notation
  3. ELK初步指南
  4. 剑指offer之 从上往下打印二叉树
  5. KbmMemTable的简单应用(增删改查示例)
  6. Delphi 实现检测线程类TThread是否结束
  7. poj3352 边-双联通分量
  8. 1.start
  9. kylin_学习_00_资源帖
  10. SQL的CASE表达式用法