Jedis操作单机版RedisJedis jedis = new Jedis(ip,port);//创建一个Jedis对象jedis.set(“test”,“hello world”);//存值String s = jedis.get(“test”);//取值System.out.println(s);jedis.clo...
Jedis操作单机版Redis
Jedis jedis = new Jedis(ip,port);//创建一个Jedis对象
jedis.set(“test”,“hello world”);//存值
String s = jedis.get(“test”);//取值
System.out.println(s);
jedis.close();//一定要记得关闭,因为它不会自动关闭,极为浪费资源
使用连接池
JedisPool jedisPool = new JedisPool(ip,port);
Jedis jedis = jedisPool.getResource();
jedis.set(“test”,“hello world”);//存值
String s = jedis.get(“test”);//取值
System.out.println(s);
jedis.close();//一定要记得关闭,不然连接池无法回收;
jedisPool.close();//系统关闭时关闭连接池、、
Jedis操作集群版Redis//cluster意为集群,一群
Set<HostAndPort> nodes = new HashSet();
nodes.add(new HostA
ndPort(ip,port));//每一个Redis服务器就添加一个节点
JedisCluster jedisCluster = new JedisCluster(nodes);
jedisCluster.set("key","value");
jedisCluster.get("key");
jedisCluster.close();//系统关闭时关闭
通过spring配置
单机版
配置实现类
<bean id = "jedisClientSingle" class=“实现类名”/>
集群版配置
<bean id = ""jedisCluster" class=“redis.clients.h=jedis.JedisCluster”>
<constructor-arg>
<set>
<bean class=""redis.clients.jedis.HostAndPort>
<constructor-arg name = "host" value="ip"/>
<constructor-arg name = "port" value="port"/>
</bean>
<bean class=""redis.clients.jedis.HostAndPort>
<constructor-arg name = "host" value="ip"/>
<constructor-arg name = "port" value="port"/>
</bean>
</set>
</costructor-arg>
</bean>
通过Spring容器获取JedisCluster对象
ApplicationContext applicationContext = newClassPathXmlAppolicationContext(“classpath:applicationContext.xml”);
JedisCluster jedisCluster = applicationContext.getBean(JedisCluster.class);
jedisCluster.set("key","value");
jedisCluster.get("key");
jedisCluster.close();//系统关闭时关闭
本文标题为:Jedis操作Redis


- MySQL8.0.28安装教程详细图解(windows 64位) 2023-07-26
- redis清除数据 2023-09-13
- SQLSERVER调用C#的代码实现 2023-07-29
- Numpy中如何创建矩阵并等间隔抽取数据 2023-07-28
- 基于Python制作一个简单的文章搜索工具 2023-07-28
- Mongodb启动报错完美解决方案:about to fork child process,waiting until server is ready for connections. 2023-07-16
- 搭建单机Redis缓存服务的实现 2023-07-13
- 在阿里云CentOS 6.8上安装Redis 2023-09-12
- SQL Server 2022 AlwaysOn新特性之包含可用性组详解 2023-07-29
- Oracle 删除大量表记录操作分析总结 2023-07-23