Linux下C++操作Redis
介绍 Redis的全称是Remote Dictonary Server(远程字典服务器),redis是由Salvatore Sanfilippo写的一个高性能key-value存储系统,Redis有如下优点: 1. 高性能 - Redis能支持超过100K+每秒的读写频率 2. 丰富的数据类型 - Redis支持Strings、Lists、Hashes、S
介绍
Redis的全称是Remote Dictonary Server(远程字典服务器),redis是由Salvatore Sanfilippo写的一个高性能key-value存储系统,Redis有如下优点:
1. 高性能 - Redis能支持超过100K+每秒的读写频率
2. 丰富的数据类型 - Redis支持Strings、Lists、Hashes、Sets及Ordered Sets等数据类型
3. 原子性 - Redis的所有操作都是原子性的,同时Redis还支持对几个操作合并后的原子操作
4. 丰富的特性 - Redis还支持发布/订阅、事务、key过期等特性
安装Redis
打开Redis官网,进入下载页面,选择一个适合自己电脑的版本下载即可,下载飞机票http://redis.io/download,下载完成后解压、编译、安装,依次在终端下执行如下命令:
<code> tar -zxvf redis-2.8.7.tar.gz cd redis-2.8.7 sudo apt-get install tcl(redis测试程序需要tcl版本至少为8.5) make 32bit(64位系统直接使用make即可) sudo make install(将编译生成的可执行文件拷贝到/usr/local/bin目录下) make test(用于确认安装正确与否) </code>
编译生成的可执行文件有:
1. redis-server redis服务器
2. redis-cli redis客户端
3. redis-benchmark redis性能测试工具
4. redis-check-aof aof文件修复工具
5. redis-check-dump rdb文件检查工具
6. redis-sentinel redis集群管理工具
编译、安装完成后,在终端中输入redis-server
以最简单的方式启动redis服务端,然后在另一个终端中输入redis-cli
来连接redis服务端,接下来可以尝试各种命令了,可以在http://try.redis.io预习下redis的各种命令,还可以在redis官网查看redis支持的命令。
安装hiredis
需要使用C/C++操作Redis,就需要安装C/C++ Redis Client Library,这里我使用的是hiredis,这是官方使用的库,而且用得人比较多,在终端下依次执行下列命令进行下载、安装:
<code> git clone https://github.com/redis/hiredis cd hiredis make sudo make install(复制生成的库到/usr/local/lib目录下) sudo ldconfig /usr/local/lib </code>
C/C++操作Redis
所有的准备工作已经做完了,接下来测试下如何使用C/C++操作Redis,代码如下:
#include <hiredis/hiredis.h> #include <iostream> #include <string> int main(int argc, char **argv) { struct timeval timeout = {2, 0}; //2s的超时时间 //redisContext是Redis操作对象 redisContext *pRedisContext = (redisContext*)redisConnectWithTimeout("127.0.0.1", 6379, timeout); if ( (NULL == pRedisContext) || (pRedisContext->err) ) { if (pRedisContext) { std::cout << "connect error:" << pRedisContext->errstr << std::endl; } else { std::cout << "connect error: can't allocate redis context." << std::endl; } return -1; } //redisReply是Redis命令回复对象 redis返回的信息保存在redisReply对象中 redisReply *pRedisReply = (redisReply*)redisCommand(pRedisContext, "INFO"); //执行INFO命令 std::cout << pRedisReply->str << std::endl; //当多条Redis命令使用同一个redisReply对象时 //每一次执行完Redis命令后需要清空redisReply 以免对下一次的Redis操作造成影响 freeReplyObject(pRedisReply); return 0; }
保存退出,执行g++ OperatorRedis.cpp -o OperatorRedis -lhiredis
进行编译,编译完成后执行./OperatorRedis
运行程序(在运行程序前需要启动redis服务端,否则会得到connect error:Connection refused
这样的错误),不出意外的话会看到输出的redis服务器信息~
好了,C++操作Redis先进行到这里了,我这边封装了一个C++操作Redis的类,等完善后会放出来...
Redis学习链接
- http://redis.io/:Redis官网
- http://redis.cn/:Redis中文官网
- http://try.redis.io/:在线体验Redis
- https://github.com/antirez/redis:Redis开发版本源码
- http://www.redisdoc.com/en/latest/:Redis命令参考
- http://blog.nosqlfan.com/topics/redis:Redis系类文章
- http://redisbook.readthedocs.org/en/latest/:Redis设计与实现
- https://github.com/huangz1990/annotated_redis_source:注释版Redis源码
作者:hahaya
出处:http://hahaya.github.com/operator-redis-under-linux
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
分类: blog 标签: Redis
Linux下安装git>>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

The steps to start Apache are as follows: Install Apache (command: sudo apt-get install apache2 or download it from the official website) Start Apache (Linux: sudo systemctl start apache2; Windows: Right-click the "Apache2.4" service and select "Start") Check whether it has been started (Linux: sudo systemctl status apache2; Windows: Check the status of the "Apache2.4" service in the service manager) Enable boot automatically (optional, Linux: sudo systemctl

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information

To restart the Apache server, follow these steps: Linux/macOS: Run sudo systemctl restart apache2. Windows: Run net stop Apache2.4 and then net start Apache2.4. Run netstat -a | findstr 80 to check the server status.

This guide will guide you to learn how to use Syslog in Debian systems. Syslog is a key service in Linux systems for logging system and application log messages. It helps administrators monitor and analyze system activity to quickly identify and resolve problems. 1. Basic knowledge of Syslog The core functions of Syslog include: centrally collecting and managing log messages; supporting multiple log output formats and target locations (such as files or networks); providing real-time log viewing and filtering functions. 2. Install and configure Syslog (using Rsyslog) The Debian system uses Rsyslog by default. You can install it with the following command: sudoaptupdatesud

Apache cannot start because the following reasons may be: Configuration file syntax error. Conflict with other application ports. Permissions issue. Out of memory. Process deadlock. Daemon failure. SELinux permissions issues. Firewall problem. Software conflict.

The Syslog service of the Debian system is responsible for recording and managing system logs and is an important tool for diagnosing system failures. By analyzing the logs, you can effectively troubleshoot hardware problems, software errors and security events. The following steps and commands will guide you how to use DebianSyslog for troubleshooting: 1. View the system log in real time to view the latest log: Use the tail-f/var/log/syslog command to monitor the real-time update of the system log, which is convenient for observing system events and error information. View kernel logs: Use the dmesg command to view the detailed log information of the kernel, which helps to discover problems with the underlying hardware or drivers. Use journalctl(systemd

In Debian systems, the resource usage of GitLab is an important consideration, especially when performing high-load operations such as CI/CD. The following is a detailed analysis and suggestions on GitLab's resource occupancy: Resource occupancy profile CPU usage: After GitLab is running, the average CPU usage may remain around 30%, but under high load conditions, such as frequent CI/CD operations, CPU usage may soar. Memory usage: Memory usage usually remains around 75%, once someone starts to access GitLab pages to operate
