管理Kubernetes资源要注意的5件事!
1、本篇前言
Redis是当下最流行的用于实现缓存机制的NoSQL数据库,其主要通过key-value存储,支持高并发访问。在实际工作中,Redis结合SpringData技术后可以方便地实现序列化对象的存储。SpringBoot很好地支持了Redis,可以在项目中使用SpringData进行Redis数据操作。Redis优势:
基于内存,速度快。数据持久化的支持,可以将内存中的数据异步写入到硬盘中。数据结构丰富string<字符串>list<链表>set<集合>zset<有序集合>Hash<哈希类型>。
SpringBoot整合RedisTemplate操作Redis,RedisTemplate是SpringData提供的Redis操作模板,该操作模板主要以Jedis驱动程序为实现基础,进行数据操作封装,所以可以直接调用Redis中的各种数据处理命令进行数据库操作。
2、SpringBoot2.x中Redis相关变动
SpringBoot2.x 其中对Redis的支持不仅仅是丰富了它的API,更是替换掉底层Jedis的依赖,取而代之换成了Lettuce高级Redis客户端,用于多线程安全同步,异步和响应使用。Lettuce和Jedis的都是连接Redis Server的客户端程序。Jedis在实现上是直连redis server,多线程环境下非线程安全,除非使用连接池JedisPool,为每个Jedis实例增加物理连接。Lettuce基于Netty的连接实例(StatefulRedisConnection),可以在多个线程间并发访问,且线程安全,满足多线程环境下的并发访问,同时它是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。
多线程安全,以前是Jedis+JedisPool组合 ,现在在SpringBoot 2.0应用中直接使用Lettuce客户端的API封装RedisTemplate即可,只要配置好连接池属性,那么SpringBoot就能自动管理连接池。
3、SpringBoot2.x集成Redis
(1)引入依赖
(2)应用配置
SpringBoot2.x中 默认客户端是Lettuce。
server: port: 8080spring: redis: # Redis数据库索引(默认为0) database: 0 # Redis服务器连接端口 port: 6379 # Redis服务器地址 host: 127.0.0.1 # Redis服务器连接密码(默认为空) #password: 123456 # 连接超时时间(毫秒) timeout: 5000 lettuce: # 关闭超时时间 shutdown-timeout: 100 pool: # 连接池最大连接数(使用负值表示没有限制) max-active: 8 # 连接池最大阻塞等待时间(使用负值表示没有限制) max-wait: 10000 # 连接池中的最大空闲连接 max-idle: 8 # 连接池中的最小空闲连接 min-idle: 0
(3)Redis配置类
/** * @Title: RedisCacheConfig * @Description: Redis配置 * @Author Trazen * @Date 2019/6/5下午 11:36 */@Configurationpublic class RedisConfig { /** * RedisTemplate配置 * 注意: 注入的是LettuceConnectionFactory */ @Bean public RedisTemplate redisTemplate(LettuceConnectionFactory factory){ RedisTemplate redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(factory); redisTemplate.setKeySerializer(keySerializer()); redisTemplate.setHashKeySerializer(keySerializer()); redisTemplate.setValueSerializer(valueSerializer()); redisTemplate.setHashValueSerializer(valueSerializer()); return redisTemplate; } /** * key序列化 */ private RedisSerializer keySerializer() { return new StringRedisSerializer(); } /** * value序列化 */ private RedisSerializer
(4)Redis操作类
RedisService接口
public interface RedisService { /** * 指定缓存失效时间 * @param key * @param time * @return */ boolean expire(String key, long time); /** * 根据key 获取过期时间 * @param key * @return */ long getExpire(String key); /** * 判断key是否存在 * @param key * @return */ boolean hasKey(String key); /** * 删除缓存 * @param key */ void del(String... key); /** * 普通缓存获取 * @param key * @return */ Object get(String key); /** * 普通缓存存入 * @param key * @param value * @return */ boolean set(String key, Object value); /** * 普通缓存放入并设置过期时间 * @param key * @param value * @param time * @return */ boolean set(String key, Object value, long time); /** * 递增 * @param key * @param delta * @return */ long incr(String key, long delta); /** * 递减 * @param key * @param delta * @return */ long decr(String key, long delta); /** * HashGet * @param key * @param item * @return */ Object hGet(String key, String item); /** * 获取hashKey对应的所有键值 * @param key * @return */ Map
RedisService接口实现类
@Service@RequiredArgsConstructor@Slf4jpublic class RedisServiceImpl implements RedisService { private final RedisTemplate redisTemplate; @Override public boolean expire(String key, long time) { try { if (time > 0) { redisTemplate.expire(key, time, TimeUnit.SECONDS); } return true; } catch (Exception e) { log.error("redis操作异常"); return false; } } @Override public long getExpire(String key) { return redisTemplate.getExpire(key, TimeUnit.SECONDS); } @Override public boolean hasKey(String key) { try { return Boolean.TRUE.equals(redisTemplate.hasKey(key)); } catch (Exception e) { log.error("redis操作异常"); return false; } } @Override public void del(String... key) { if (key != null && key.length > 0) { if (key.length == 1) { redisTemplate.delete(key[0]); } else { redisTemplate.delete(CollUtil.toList(key)); } } } @Override public Object get(String key) { return key == null ? null : redisTemplate.opsForValue().get(key); } @Override public boolean set(String key, Object value) { try { redisTemplate.opsForValue().set(key, value); return true; } catch (Exception e) { log.error("redis操作异常"); return false; } } @Override public boolean set(String key, Object value, long time) { try { if (time > 0) { redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); } else { set(key, value); } return true; } catch (Exception e) { log.error("redis操作异常"); return false; } } @Override public long incr(String key, long delta) { if (delta < 0) { throw new RuntimeException("递增因子必须大于0"); } return redisTemplate.opsForValue().increment(key, delta); } @Override public long decr(String key, long delta) { if (delta < 0) { throw new RuntimeException("递减因子必须大于0"); } return redisTemplate.opsForValue().increment(key, -delta); } @Override public Object hGet(String key, String item) { return redisTemplate.opsForHash().get(key, item); } @Override public Map hmGet(String key) { return redisTemplate.opsForHash().entries(key); } @Override public boolean hmGet(String key, Map map) { try { redisTemplate.opsForHash().putAll(key, map); return true; } catch (Exception e) { log.error("redis操作异常"); return false; } } @Override public boolean hSet(String key, String item, Object value) { try { redisTemplate.opsForHash().put(key, item, value); return true; } catch (Exception e) { log.error("redis操作异常"); return false; } } @Override public boolean hSet(String key, String item, Object value, long time) { try { redisTemplate.opsForHash().put(key, item, value); if (time > 0) { expire(key, time); } return true; } catch (Exception e) { log.error("redis操作异常"); return false; } } @Override public void hDel(String key, Object... item) { redisTemplate.opsForHash().delete(key, item); } @Override public boolean hHasKey(String key, String item) { return redisTemplate.opsForHash().hasKey(key, item); } @Override public double hIncr(String key, String item, double by) { return redisTemplate.opsForHash().increment(key, item, by); } @Override public double hDecr(String key, String item, double by) { return redisTemplate.opsForHash().increment(key, item, -by); } @Override public Set sGet(String key) { try { return redisTemplate.opsForSet().members(key); } catch (Exception e) { log.error("redis操作异常"); return null; } } @Override public boolean sHasKey(String key, Object value) { try { return redisTemplate.opsForSet().isMember(key, value); } catch (Exception e) { log.error("redis操作异常"); return false; } } @Override public long sSet(String key, Object... values) { try { return redisTemplate.opsForSet().add(key, values); } catch (Exception e) { log.error("redis操作异常"); return 0; } } @Override public long sSetAndTime(String key, long time, Object... values) { try { Long count = redisTemplate.opsForSet().add(key, values); if (time > 0) expire(key, time); return count; } catch (Exception e) { log.error("redis操作异常"); return 0; } } @Override public long sGetSetSize(String key) { try { return redisTemplate.opsForSet().size(key); } catch (Exception e) { log.error("redis操作异常"); return 0; } } @Override public long setRemove(String key, Object... values) { try { return redisTemplate.opsForSet().remove(key, values); } catch (Exception e) { log.error("redis操作异常"); return 0; } } @Override public List lGet(String key, long start, long end) { try { return redisTemplate.opsForList().range(key, start, end); } catch (Exception e) { log.error("redis操作异常"); return null; } } @Override public long lGetListSize(String key) { try { return redisTemplate.opsForList().size(key); } catch (Exception e) { log.error("redis操作异常"); return 0; } } @Override public Object lGetIndex(String key, long index) { try { return redisTemplate.opsForList().index(key, index); } catch (Exception e) { log.error("redis操作异常"); return null; } } @Override public boolean lSet(String key, Object value) { try { redisTemplate.opsForList().rightPush(key, value); return true; } catch (Exception e) { log.error("redis操作异常"); return false; } } @Override public boolean lSet(String key, Object value, long time) { try { redisTemplate.opsForList().rightPush(key, value); if (time > 0) expire(key, time); return true; } catch (Exception e) { log.error("redis操作异常"); return false; } } @Override public boolean lSet(String key, List value) { try { redisTemplate.opsForList().rightPushAll(key, value); return true; } catch (Exception e) { log.error("redis操作异常"); return false; } } @Override public boolean lSet(String key, List value, long time) { try { redisTemplate.opsForList().rightPushAll(key, value); if (time > 0) expire(key, time); return true; } catch (Exception e) { log.error("redis操作异常"); return false; } } @Override public boolean lUpdateIndex(String key, long index, Object value) { try { redisTemplate.opsForList().set(key, index, value); return true; } catch (Exception e) { log.error("redis操作异常"); return false; } } @Override public long lRemove(String key, long count, Object value) { try { Long remove = redisTemplate.opsForList().remove(key, count, value); return remove; } catch (Exception e) { log.error("redis操作异常"); return 0; } }}
(5)单元测试类
@SpringBootTestpublic class RedisServiceTest { @Autowired private RedisService redisService; /** * 普通key value操作 */ @Test public void testKey(){ boolean result = redisService.set("name","Trazen"); Assertions.assertTrue(result); String value = String.valueOf(redisService.get("name")); Assertions.assertEquals(value,"Trazen"); result = redisService.expire("name",2); Assertions.assertTrue(result); long expireTime = redisService.getExpire("name"); Assertions.assertEquals(expireTime,2); redisService.del("name"); } @Test public void testMap(){ redisService.hSet("10001","name","zhangsan"); redisService.hSet("10001","age",20); redisService.hSet("10001","address","安徽省合肥市"); Map objectObjectMap = redisService.hmGet("10001"); objectObjectMap.forEach( (k,v)->{System.out.println(k+" "+v);} ); redisService.hDel("10001","address"); objectObjectMap = redisService.hmGet("10001"); objectObjectMap.forEach( (k,v)->{System.out.println(k+" "+v);} ); redisService.del("10001"); } @Test public void testSet(){ redisService.sSet("dataType","mysql","sqlserver","db2","***"); Set objectSet = redisService.sGet("dataType"); objectSet.forEach(System.out::println); long keyCount = redisService.sGetSetSize("dataType"); System.out.println(keyCount); redisService.setRemove("dataType","db2"); keyCount = redisService.sGetSetSize("dataType"); System.out.println(keyCount); } @Test public void testList(){ redisService.del("runoobkey"); List stringList = Arrays.asList("redis","mongodb","memcache"); redisService.lSet("runoobkey",stringList); long size = redisService.lGetListSize("runoobkey"); System.out.println(size); List resultList = redisService.lGet("runoobkey",0,10); resultList.forEach(System.out::println); }}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。