Redis直接用
2021/7/24 2:13:21
本文主要是介绍Redis直接用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
配置文件
package cn.deltalpha.framework.admin.redis; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * * @Author: bai * @DATE: 2019/12/23 14:52 **/ @Configuration public class RedisConfig { @Bean(name = "jedisPool") @Primary public JedisPool getJedisPool() { return new JedisPool(jedisPoolConfig(), host, port, 0, password); } public JedisPoolConfig jedisPoolConfig() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setTimeBetweenEvictionRunsMillis(5000); jedisPoolConfig.setMinEvictableIdleTimeMillis(10000); jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(10000); jedisPoolConfig.setTestWhileIdle(true); jedisPoolConfig.setJmxEnabled(true); jedisPoolConfig.setJmxNamePrefix(""); jedisPoolConfig.setBlockWhenExhausted(false); return jedisPoolConfig; } @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.password}") private String password; }
工具类
package cn.deltalpha.ybdj.admin.functiontask.delayingqueue.utils; import org.springframework.stereotype.Component; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import javax.annotation.Resource; import java.util.List; import java.util.Set; /** * * @Author: bai * @DATE: 2019/12/23 14:52 **/ @Component public class JedisUtils { @Resource private JedisPool jedisPool; /******************************以下是对key的操作*********************************/ /** * 获取值 * @param key * @param defaultVal * @return */ public String get(String key, String defaultVal) { Jedis jedis = null; try { jedis = jedisPool.getResource(); String val = jedis.get(key); return val == null ? defaultVal : val; } catch (Exception e) { e.printStackTrace(); } finally { jedis.close(); } return defaultVal; } /** * 设置kv ,并制定过期时间 * @param key * @param val * @param seconds 有效期(秒) * @return */ public boolean setex(String key, String val, int seconds) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.setex(key, seconds, val); return true; } catch (Exception e) { e.printStackTrace(); } finally { jedis.close(); } return false; } /** * 删除key * @param key * @return */ public boolean del(String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.del(key); return true; } catch (Exception e) { e.printStackTrace(); } finally { jedis.close(); } return false; } /******************************以下是zset操作*********************************/ /** * 向zset添加元素 * @param key * @param val * @param score * @return */ public boolean zadd(String key, long score, String val) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.zadd(key, score, val); //相同的key,也只会做更新操作 return true; } catch (Exception e) { e.printStackTrace(); } finally { jedis.close(); } return false; } /** * 删除zset元素 * @param key * @param val * @return */ public boolean zdel(String key, String... val) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.zrem(key, val) > 0; } catch (Exception e) { e.printStackTrace(); } finally { jedis.close(); } return false; } /** * 获取优先队列元素 * @param key * @param startRange * @param endRange * @param orderByDesc 是否降序 * @return */ public Set<String> getSoredSetByRange(String key, int startRange, int endRange, boolean orderByDesc) { Jedis jedis = null; try { jedis = jedisPool.getResource(); if (orderByDesc) { return jedis.zrevrange(key, startRange, endRange); //降序 } else { return jedis.zrange(key, startRange, endRange); //升序 按照score进行排序 } } catch (Exception e) { e.printStackTrace(); } finally { jedis.close(); } return null; } /** * 获取评分 * @param key * @param member * @return */ public Double getScore(String key, String member) { Jedis jedis = null; try { jedis = jedisPool.getResource(); return jedis.zscore(key, member); } catch (Exception e) { e.printStackTrace(); } finally { jedis.close(); } return null; } /******************************以下是List操作*********************************/ /** * 获取list长度 * @param key * @return */ public long countList(String key) { if (key == null) { return 0; } Jedis jedis = null; try { jedis = jedisPool.getResource(); //获取列表长度 return jedis.llen(key); } catch (Exception e) { e.printStackTrace(); } finally { jedis.close(); } return 0L; } /** * 添加元素到list(使用右进) * @param key * @param val * @return */ public boolean insertList(String key, String... val) { if (key == null || val == null) { return false; } Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.rpush(key, val); return true; } catch (Exception e) { e.printStackTrace(); } finally { jedis.close(); } return false; } /** * 获取list元素(采用左出方式) 先进先出队列 * @param key * @param start * @param end * @return */ public List<String> rangeList(String key, long start, long end) { if (key == null || key.equals("")) { return null; } Jedis jedis = null; try { jedis = jedisPool.getResource(); //返回列表中所有值 return jedis.lrange(key, start, end); } catch (Exception e) { e.printStackTrace(); } finally { jedis.close(); } return null; } /** * 删除list数据 * @param key * @param count * @param value * @return */ public boolean removeListValue(String key, long count, String value) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.lrem(key, count, value); return true; } catch (Exception e) { e.printStackTrace(); } finally { jedis.close(); } return false; } public int removeListValue(String key, long count, List<String> values) { int result = 0; if (values != null && values.size() > 0) { for (String value : values) { if (removeListValue(key, count, value)) { result++; } } } return result; } public int removeListValue(String key, List<String> values) { return removeListValue(key, 1, values); //默认只删除一个元素 重头开始搜索 } }
这篇关于Redis直接用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-07Redis高并发入门详解
- 2024-12-07Redis缓存入门:新手必读指南
- 2024-12-07Redis缓存入门:新手必读教程
- 2024-12-07Redis入门:新手必备的简单教程
- 2024-12-07Redis入门:新手必读的简单教程
- 2024-12-06Redis入门教程:从安装到基本操作
- 2024-12-06Redis缓存入门教程:轻松掌握缓存技巧
- 2024-12-04Redis入门:简单教程详解
- 2024-11-29Redis开发入门教程:从零开始学习Redis
- 2024-11-27Redis入门指南:快速掌握Redis基础操作