Redis安装及操作指南
redis安装与使用
- 1. 介绍
- 2. 安装
- 2.1 服务端
- 2.2 客户端
- 3. 接口介绍
- 4. 使用
1. 介绍
Redis(Remote Dictionary Server)是一款开源的高性能键值对数据库。它常被当作数据结构服务器来运用,除了基础的键值存储功能外,还支持多种数据结构,例如字符串、哈希、列表、集合、有序集合等,同时还具备范围查询、位图、超日志以及地理空间索引等功能。
以下是Redis的一些关键特性:
- 内存存储特性:Redis将所有数据存储于内存之中,这使得数据的读写速度极为迅速。
- 持久化机制:尽管Redis是内存数据库,但它提供了持久化方案,能够把内存中的数据保存到磁盘上,避免系统故障时数据丢失。
- 数据结构多样性:Redis不仅支持基本的键值对,还支持列表、集合、有序集合等复杂的数据结构。
- 原子操作能力:Redis支持原子操作,意味着多个操作可以作为一个单独的原子步骤来执行,这对并发控制至关重要。
- 发布订阅功能:Redis支持发布订阅模式,允许多个客户端订阅消息,当有消息发布时,所有订阅者都能接收到消息。
- 高可用特性:通过Redis哨兵和Redis集群,Redis能够实现高可用性以及自动故障转移。
- 复制功能:Redis支持主从复制,可提高数据的可用性和读写性能。
- 事务功能:Redis提供事务功能,能够保证一系列操作原子性执行。
- Lua脚本支持:Redis支持使用Lua脚本进行复杂的数据处理,能够在服务器端执行复杂的逻辑。
- 客户端库丰富:Redis拥有丰富的客户端库,支持多种编程语言,像Python、Ruby、Java、C#等。
- 性能监控手段:Redis提供多种监控工具和命令,帮助开发者监控和优化性能。
- 易用性:Redis拥有简单的配置文件和命令行界面,设置和使用起来都很便捷。
Redis被广泛应用于缓存、会话存储、消息队列、排行榜、实时分析等领域。凭借其高性能和灵活性,成为现代应用程序中广受欢迎的数据存储解决方案之一。
总结:Redis是一款内存数据库,用于存储键值对,其中值可以是多种数据类型。
2. 安装
2.1 服务端
通过执行sudo apt install redis -y
命令进行默认安装,默认安装状态下仅支持本地连接,不过可以修改配置以支持远程连接。
支持远程连接
修改/etc/redis/redis.conf
文件:
* 将bind 127.0.0.1
修改为bind 0.0.0.0
* 将protected-mode yes
修改为protected-mode n
启动Redis服务
执行service redis-server start
命令。
停止Redis服务
执行service redis-server stop
命令。
重启Redis服务
执行service redis-server restart
命令。
设置开机启动Redis服务
执行sudo systemctl enable redis-server
命令。
2.2 客户端
有众多C++操作redis的库,我们选用redis-plus-plus,该库功能强大且使用简便,不过它是基于hiredis实现的,hiredis是用C语言实现的redis客户端。所以需要先安装hiredis,直接通过包管理器安装即可。
执行sudo apt install libhiredis-dev
命令。
下载redis-plus-plus源码
执行git clone https://github.com/sewenew/redis-plus-plus.git
命令。
编译/安装redis-plus-plus
使用cmake进行构建:
执行cd redis-plus-plus
,接着执行mkdir build
,再执行cd build
,然后执行cmake -DCMAKE_INSTALL_PREFIX=/usr ..
(将其安装到系统库),之后执行make
,最后执行make install
(这一步需要管理员权限,如果是非root用户,使用sudo make install
执行)。
构建成功后,/usr/local/include/
中会多出sw
目录,内部包含redis-plus-plus的一系列头文件,/usr/local/lib/
中会多出一系列libredis库文件。
3. 接口介绍
redis自身提供了丰富的数据类型键值对接口,此处简单介绍字符串键值对的操作方式。
namespace sw
{
namespace redis
{
struct ConnectionOptions
{
std::string host;//ip
int port = 6379;//port
std::string path;
std::string user = "default";//用户名
std::string password;//密码
int db = 0; // 默认 0 号库
bool keep_alive = false;//长连接/短连接
}
//连接池
struct ConnectionPoolOptions
{
std::size_t size = 1; // 最大连接数量
}
class Redis
{
//直接给Redis服务器ip+port构造,各种配置信息采用默认的进行连接
// uri e.g 'tcp://127.0.0.1:6379'
explicit Redis(const std::string &uri)
//或者自己在ConnectionOptions配置信息进行连接
explicit Redis(const ConnectionOptions &connection_opts,const ConnectionPoolOptions &pool_opts = {})
// 删除当前库中所有数据
void flushdb(bool async = false);
// 删除指定键值对
long long del(const StringView &key);
// 判断指定键值对是否存在
long long exists(const StringView &key);
// 获取一个 string 键值对
OptionalString get(const StringView &key);
// 存放一个 string 键值对,且设置过期时间-毫秒
bool set(const StringView &key,
const StringView &val,
const std::chrono::milliseconds &ttl =
std::chrono::milliseconds(0), // 0 表示不设置超时
UpdateType type = UpdateType::ALWAYS);
void setex(const StringView &key,
long long ttl,
const StringView &val);
// 向一个列表中尾插/头插 string 键值对,它的val是一个列表
long long rpush(const StringView &key, const StringView&val);
long long lpush(const StringView &key, const StringView&val);
long long rpush(const StringView &key,Input first, Input last);
//获取列表数据
// std::vector<std::string> elements;
// redis.lrange("list", 0, -1,std::back_inserter(elements));
void lrange(const StringView &key,long long start, long long stop, Output output);
}
}
}
4. 使用
此处仅进行字符串键值对的增删改查操作以及数据生命周期设置相关操作。
#include<iostream>
#include<sw/redis++/redis++.h>
#include<thread>
void print(sw::redis::Redis& client)
{
auto user1 = client.get("会话1");
if(user1) std::cout<<*user1<<std::endl;
auto user2 = client.get("会话2");
if(user2) std::cout<<*user2<<std::endl;
auto user3 = client.get("会话3");
if(user3) std::cout<<*user3<<std::endl;
auto user4 = client.get("会话4");
if(user4) std::cout<<*user4<<std::endl;
}
void add_string(sw::redis::Redis& client)
{
client.set("会话1","ID1");
client.set("会话2","ID2");
client.set("会话3","ID3");
client.set("会话4","ID4");
client.del("会话2");
//数据已存在则进行修改,不存在则新增
client.set("会话3","ID33333");
print(client);
}
void expired_test(sw::redis::Redis& client)
{
//这次的新增,数据其实已经有了,因此本次是修改
//不仅仅修改了val,而且还给键值对新增了过期时间
client.set("会话1","ID1111",std::chrono::milliseconds(1000));
print(client);
std::cout << "------------休眠2s-----------\n";
std::this_thread::sleep_for(std::chrono::seconds(2));
print(client);
}
void list_test(sw::redis::Redis& client)
{
client.rpush("群聊1","用户1");
client.rpush("群聊1","用户2");
client.rpush("群聊1","用户3");
client.rpush("群聊1","用户4");
std::vector<std::string> users;
client.lrange("群聊1",0,-1,std::back_insert_iterator(users));
for(auto& user : users)
{
std::cout<< user << std::endl;
}
}
int main()
{
//1. 构造连接选项,实例化Redis对象,连接服务器
sw::redis::ConnectionOptions opts;
//库IP地址
opts.host = "127.0.0.1";
//库的端口
opts.port = 6379;
//库的编号:默认0号
opts.db = 0;
//是否进行长连接保活
opts.keep_alive = true;
sw::redis::Redis client(opts);
//2. 添加字符串键值对,删除字符串键值对,获取字符串键值对
add_string(client);
//3. 实践控制数据有效时间的操作
expired_test(client);
//4. 列表的操作,主要实现数据的右插,左获取
std::cout << "--------------------------\n";
list_test(client);
return 0;
}
文章整理自互联网,只做测试使用。发布者:Lomu,转转请注明出处:https://www.it1024doc.com/12840.html