文章目录
redis 安装
apt install redis
ps -ef | grep redis
netstat -tpnl | grep redis
service redis-server status|start|stop|restart
应用领域
- 缓存(数据库、Web页面、图片、流媒体)
- 数据结构存储
*
- key-value 存储系统
值的数据类型
参考手册:doc.redisfans.com
键是字符串,值是多个类型
- string 字符串(非c字符串,redis自定义字符串)
- set key value [ex seconds|px milliseconds] 设置键值,以及有效
- get key 获取键值
- mset key value [key value…] 设置多个键值
- mget key [key…] 获取多个键值
- del key 删除键
- incr key 自增1| incrby key increment自增increment
- keys pattern 查键
- list 列表 有序号
- lpush/rpush key value [value…]
- lpop/rpop key
- lrange key start stop 获取指定范围的值
- set 集合 无重复值
- sadd key member [member…]
- smembers key 集合成员
- sinter key [key…] 集合交集
- sunion key [key…] 集合并集
- sdiff key [key…] 集合差集
- srandmember key [count] 随机count个成员
- spop key [count] 随机弹出count个成员
- ismember key member 集合成员是否存在
- scard key 集合大小
- hash 哈希表 字典
- hset key field value… 添加
- hget key field 获取
- hmget key field [field…] 批量获取
- sorted list 有序集 优先级队列 排行 投票统计
- zadd key score member [score member…]
- zrange key start stop [withscores] stop=-1取所有值
- zincrby key increment member 自增
- zinterstore key [key…] 集合交集
- zunionstore key [key…] 集合并集
- zremrangebyscore key start stop 取指定分数范围的值
- json
- stream
其它操作:
- expire key seconds 设置过期时间 秒
- pexpire key milliseconds 设置过期时间 毫秒
- persist key 设置永久有效
hiredis C API
https://github.com/redis/hiredis
结构体:
- redisContext redis连接上下文
- redisReply 命令返回的结果
/* 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 */
double dval; /* The double when type is REDIS_REPLY_DOUBLE */
size_t len; /* Length of string */
char *str; /* Used for REDIS_REPLY_ERROR, REDIS_REPLY_STRING
REDIS_REPLY_VERB, and REDIS_REPLY_DOUBLE (in additional to dval). */
char vtype[4]; /* Used for REDIS_REPLY_VERB, contains the null
terminated 3 character content type, such as "txt". */
size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */
struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */
} redisReply;
/*
type 的取值:
- REDIS_REPLY_STRING
- REDIS_REPLY_ARRAY
- REDIS_REPLY_INTEGER
- REDIS_REPLY_NIL
- REDIS_REPLY_STATUS
- REDIS_REPLY_ERROR
- REDIS_REPLY_DOUBLE
*/
操作:
- redisContext *redisConnect(const char *ip, int port);
- redisReply *redisCommand(redisContext *c, const char *format, …);
- void freeReplyObject(void *reply);
- void redisFree(redisContext *c);
- redisReply* redisCommandargv(redisContext *c, int argc, const char **argv, const size_t *argvlen);