redis初级使用
2021/6/9 19:27:14
本文主要是介绍redis初级使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
在Java中访问Redis
1. 导入依赖包
- commons-pool2-2.4.3.jar
- jedis-2.9.3.jar
2. 创建Redis工具类
package my.jedis.demo; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class JedisAPI { private static JedisPool jedisPool; static { // 创建并设置连接池配置对象 JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(50); // 连接池最大连接数,默认为8 config.setMaxIdle(10); // 连接池中的最大空闲连接,默认为8 config.setMinIdle(7); // 连接池中的最小空闲连接,默认为0 config.setMaxWaitMillis(1000); // 获取资源的等待时间 config.setTestOnBorrow(true); // 获取资源时是否验证资源的有效性 // 创建Jedis连接池 jedisPool = new JedisPool( config, // 连接池配置对象 "192.168.9.66", // Redis服务器地址 6379, // Redis服务端口 10000, // 连接超时时间,单位毫秒,默认2000ms即2s "123456", // Redis密码 0); // 数据库索引 } public void destroy() { if (!(jedisPool == null || jedisPool.isClosed())) jedisPool.close(); // 关闭连接池 } /** * 新增或修改 * * @param key * @param value * @return */ public boolean set(String key, String value) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.set(key, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } finally { // 返还到连接池 if (jedis != null) jedis.close(); } } /** * 新增/修改 * * @param key * @param seconds 有效期 * @param value * @return */ public boolean set(String key, int seconds, String value) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.setex(key, seconds, value); return true; } catch (Exception e) { e.printStackTrace(); } finally { // 返还到连接池 if (jedis != null) jedis.close(); } return false; } /** * 判断某个key是否存在 * * @param key * @return */ public boolean exist(String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.exists(key); } catch (Exception e) { e.printStackTrace(); } finally { // 返还到连接池 if (jedis != null) jedis.close(); } return false; } /** * 获取数据 * * @param key * @return */ public String get(String key) { String value = null; Jedis jedis = null; try { jedis = jedisPool.getResource(); value = jedis.get(key); } catch (Exception e) { e.printStackTrace(); } finally { // 返还到连接池 if (jedis != null) jedis.close(); } return value; } /** * 查询key的有效期,当 key 不存在时,返回 -2 。 当 key 存在但没有设置剩余生存时间时,返回 -1 。 否则,以秒为单位,返回 key * 的剩余生存时间。 注意:在 Redis 2.8 以前,当 key 不存在,或者 key 没有设置剩余生存时间时,命令都返回 -1 。 * * @param key * @return 剩余多少秒 */ public Long ttl(String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.ttl(key); } catch (Exception e) { e.printStackTrace(); } finally { // 返还到连接池 if (jedis != null) jedis.close(); } return (long) -2; } /** * 删除 * * @param key */ public void delete(String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.del(key); } catch (Exception e) { e.printStackTrace(); } finally { // 返还到连接池 if (jedis != null) jedis.close(); } } }
这篇关于redis初级使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-21Redis资料入门教程:轻松掌握Redis基础知识
- 2024-09-21Redis资料:入门级用户必学教程
- 2024-09-21Redis资料:新手入门教程与实践指南
- 2024-09-20Redis教程:从入门到实践的全面指南
- 2024-09-20Redis教程:初学者快速入门指南
- 2024-09-20Redis教程:新手入门与实践指南
- 2024-09-10Redis高并发教程:入门级实战指南
- 2024-09-10Redis高并发教程:新手入门指南
- 2024-09-10Redis 缓存教程:初学者快速入门指南
- 2024-09-10Redis缓存教程: 从入门到实践