redis客户端

本文涉及的产品
云原生内存数据库 Tair,内存型 2GB
云数据库 Redis 版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: Redis 可由yum来安装。 安装后在/usr/bin/下有若干个可执行文件。主要有redis-server、redis-shutdown、redis-cli这三个。依次是启动redis的后台服务、关闭服务、客户端。 redis-benchmark 是Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能。redis-server 文件A  //表示按照文件A的配置启
Redis 可由yum来安装。
安装后在/usr/bin/下有若干个可执行文件。主要有redis-server、redis-shutdown、redis-cli这三个。依次是启动redis的后台服务、关闭服务、客户端。
redis-benchmark 是Redis性能测试工具,测试Redis在你的系统及你的配置下的读写性能。

redis-server 文件A  //表示按照文件A的配置启动redis服务器,当命令后不跟文件名时默认加载/etc/redis.conf。


1.CLI命令

redis-cli:进入命令行交互模式

select x:选择使用x号数据库

2.Jedis

Jedis是redis的java客户端。

2.1 仓库地址

maven
  <dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.8.0</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

2.2 类与方法

redis.clients.jedis.Jedis. Jedis(String host, int port)
构造函数,连接redis服务器。
String redis.clients.jedis.BinaryJedis. auth(String password)
提供正确的密码才能链接。
String redis.clients.jedis.BinaryJedis. select(int index)
指定使用的DB。redis的DB编号从0开始。不同DB之间的key是相互独立的。
String redis.clients.jedis.BinaryJedis. quit()
要求server关掉这个连接。
void redis.clients.jedis.BinaryJedis. close()
关闭连接,释放资源。

2.2.1增

String redis.clients.jedis.Jedis. set(String key, String value)
设置字符串类型的key-value。

2.2.2删


2.2.3改


2.2.4查

String redis.clients.jedis.Jedis. get(String key)
查找string类型的value。若不存在返回null。

2.2.5 其他

Long redis.clients.jedis.Jedis. expire(String key, int seconds)
指定一个key在若干秒后过期。届时此key会被server自动删掉。
返回值1代表成功,0代表失败。若此key已经被设置过超时时间,那么超时时间将得到更新。

2.3 redis连接池

redis.clients.jedis. JedisPoolConfig
类。连接池的配置。
redis.clients.jedis.JedisPoolConfig. JedisPoolConfig()
构造函数。
void org.apache.commons.pool2.impl.GenericObjectPoolConfig. setMaxIdle(int maxIdle)
设置最大空闲连接数。
 void org.apache.commons.pool2.impl.GenericObjectPoolConfig. setMaxTotal(int maxTotal)
设置最大连接数。
void org.apache.commons.pool2.impl.BaseObjectPoolConfig. setTestOnBorrow(boolean testOnBorrow)
从池中获取连接时是否测试有效。
void org.apache.commons.pool2.impl.BaseObjectPoolConfig. setTestOnReturn(boolean testOnReturn)
将连接归还到池中时是否测试有效。
Jedis redis.clients.jedis.JedisPool. getResource()
从连接池中获取连接。
void redis.clients.util.Pool. destroy()
销毁连接池,一般在程序结束时调用。

2.4 示例代码

3.常见报错

微笑:报错信息见下:
(error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.
答:报错信息似乎是说想要保存快照但不能持久化到磁盘上。可使用命令  config set stop-writes-on-bgsave-error no  解决。


相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
27天前
|
缓存 NoSQL Redis
【Azure Redis 缓存】Redission客户端连接Azure:客户端出现 Unable to send PING command over channel
【Azure Redis 缓存】Redission客户端连接Azure:客户端出现 Unable to send PING command over channel
|
27天前
|
NoSQL 网络协议 Linux
【AKS+Redis】AKS中客户端(ioredis)遇见Azure Redis服务Failover后链接中断的可能性
【AKS+Redis】AKS中客户端(ioredis)遇见Azure Redis服务Failover后链接中断的可能性
|
27天前
|
NoSQL 网络协议 Linux
【Azure Redis】Lettuce客户端遇见连接Azure Redis长达15分钟的超时
【Azure Redis】Lettuce客户端遇见连接Azure Redis长达15分钟的超时
|
27天前
|
NoSQL 网络协议 Linux
【Azure Redis】Redis客户端出现15分钟的超时异常
【Azure Redis】Redis客户端出现15分钟的超时异常
|
28天前
|
缓存 监控 NoSQL
【Azure Redis 缓存】Azure Redis出现了超时问题后,记录一步一步的排查出异常的客户端连接和所执行命令的步骤
【Azure Redis 缓存】Azure Redis出现了超时问题后,记录一步一步的排查出异常的客户端连接和所执行命令的步骤
|
30天前
|
缓存 NoSQL Java
【Azure Redis 缓存 Azure Cache For Redis】当使用Jedis客户端连接Redis时候,遇见JedisConnectionException: Could not get a resource from the pool / Redis connection los
【Azure Redis 缓存 Azure Cache For Redis】当使用Jedis客户端连接Redis时候,遇见JedisConnectionException: Could not get a resource from the pool / Redis connection los
|
3月前
|
存储 NoSQL 关系型数据库
Redis系列学习文章分享---第一篇(Redis快速入门之初始Redis--NoSql+安装redis+客户端+常用命令)
Redis系列学习文章分享---第一篇(Redis快速入门之初始Redis--NoSql+安装redis+客户端+常用命令)
103 1
|
2月前
|
JSON NoSQL Java
Redis18的Java客户端-StringRedisTemplate,序列化存在的问题,使用StringRedisTemplate解决序列化的方法
Redis18的Java客户端-StringRedisTemplate,序列化存在的问题,使用StringRedisTemplate解决序列化的方法
|
2月前
|
JSON NoSQL Java
Redis17----Redis的java客户端-RedisTemplte的RedisSerializer,赋值的是虎哥,而取出来的是ROSE的原因,使用keys
Redis17----Redis的java客户端-RedisTemplte的RedisSerializer,赋值的是虎哥,而取出来的是ROSE的原因,使用keys
|
2月前
|
NoSQL Java Redis
Redis15-Redis的java客户端,认识SpringDataRedis
Redis15-Redis的java客户端,认识SpringDataRedis