Redis
redis(Remote Directionary Server)是一个开源的、基于内存的键值存储系统,常被用作数据库、缓存和消息中间件。支持多种数据结构,每种结构都有对应的命令操作。
一、Redis 的对象类型
Redis 的值(value)可以是以下五种基本数据类型之一,前三种比较常用:
1. String (字符串)
- 最基本的数据类型。
- 可以存储字符串、整数或浮点数(最大 512MB)。
- 常用于缓存简单数据,如用户信息、计数器等。
常用操作:
1 2 3 4 5 6 7 8 9
| SET key value GET key INCR key DECR key INCRBY key increment APPEND key value STRLEN key MSET key1 val1 key2 val2 ... MGET key1 key2 ...
|
Ps. Redis 的 key 总是字符串类型
2. Hash(哈希)
类似二级缓存,【WebSite】【blogSite】 = example.com
常用操作:
1 2 3 4 5 6 7 8 9 10
| HSET key field value HGET key field HMSET key field1 val1 field2 val2 ... HMGET key field1 field2 ... HGETALL key HDEL key field [field...] HEXISTS key field HLEN key HKEYS key HVALS key
|
3. List(列表)
- 有序、可重复的字符串列表,底层是双向链表。
- 常用于消息队列、最新 N 条记录等场景。
常用操作:
1 2 3 4
| LPUSH key value [value...] RPUSH key value [value...] LPOP key RPOP key
|
4. Set(集合)
- 类似 Set,但每个成员关联一个分数(score),按分数排序。
- 常用于排行榜、带权重的任务队列等。
1 2 3 4
| SADD key member [member...] SMEMBERS key SISMEMBER key member SCARD key
|
5. Sorted Set(有序集合 / ZSet)
1 2 3 4
| ZADD key score member [score member...] ZRANGE key start stop [WITHSCORES] ZREVRANGE key start stop [WITHSCORES] ZSCORE key member
|
二、HiRedis
HiRedis 是 C++ 的一种库,C++ 还有其他库(redis-plus-plus)等等。源码这里不去介绍,不过通常我们会对 Redis 提供的操作进行一些封装,比如
1. Connect 操作
1 2 3 4 5 6 7 8
| bool CRedisMgr::Connect(const std::string& host, int port) { this->m_conn_ = redisConnect(host.c_str(), port); if (this->m_conn_ == nullptr) return false; if (this->m_conn_ != nullptr && this->m_conn_->err) { return false; } return true; }
|
2. Auth 操作
1 2 3 4 5 6 7 8 9 10 11
| bool CRedisMgr::Auth(const std::string& password) { this->m_reply_ = (redisReply*)redisCommand(this->m_conn_, "AUTH %s", password.c_str()); if (this->m_reply_->type == REDIS_REPLY_ERROR) { freeReplyObject(this->m_reply_); return false; } else { freeReplyObject(this->m_reply_); return false; } return
|
3. Get 操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| bool CRedisMgr::Get(const std::string& key, std::string& value) { this->m_reply_ = (redisReply*)redisCommand(this->m_conn_, "GET %s", key.c_str()); if (this->m_reply_ == nullptr) { freeReplyObject(this->m_reply_); return false; } if (this->m_reply_->type == REDIS_REPLY_ERROR) { freeReplyObject(this->m_reply_); return false; } value = this->m_reply_->str; freeReplyObject(this->m_reply_); std::cout << "Succeed to execute command [ GET " << key << " ]" << '\n'; return false; }
|
4. HSet 操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| bool CRedisMgr::HSet(const char* key, const char* hkey, const char* hvalue, size_t hvalueLen) { const char* argv[4]; size_t argvlen[4]; argv[0] = "HSET"; argvlen[0] = 4; argv[1] = key; argvlen[1] = strlen(key); argv[2] = hkey; argvlen[2] = strlen(hkey); argv[3] = hvalue; argvlen[3] = hvalueLen; this->m_reply_ = (redisReply*)redisCommandArgv(this->m_conn_, 4, argv, argvlen); if (this->m_reply_ == nullptr || m_reply_->type != REDIS_REPLY_INTEGER) { freeReplyObject(this->m_reply_); return false; } freeReplyObject(this->m_reply_); return true; }
|
上面的操作基本是遵循一个模板:
1 2 3 4 5 6 7 8 9 10 11 12
| bool CRedisMgr::xxx(const std::string& key, std::string& value) { redisContext *context; redisReply *reply = (redisReply*)redisCommand(context, "xxx %s %s", key.c_str(), value.c_str()); if(reply == nullptr || reply == REDIS_REPLY_ERROR) { ~~Debug(); freeReplyObject(reply); return false; } ~~Debug(); freeReplyObject(reply); return true; }
|
不过比较蛋疼的是不同操作如果出错对应的不同种的宏,所以还得自己去查,这里给个大概的分类
- “查询值” 型命令 -> 返回
STRING 或者 NIL(Get, HGet, ZScore)
- “操作计数” 型命令-> 返回
INTEGER(DEL, HSET, RPUSH, EXISTS, INCR)
- “状态反馈” 型命令 -> 返回
STATUS(SET, FLUSHDB, SAVE)
- “集合/列表” 型命令 -> 返回
ARRAY(HGETALL , SMEMBERS)