redis简单使用 发表于 2018-11-25 | 分类于 redis 目的:检查登陆账号密码是否存在于redis缓存中,如果没有则从数据库中寻找,如果有,则不通过数据库直接从redis中取出 123456789101112131415161718192021// 缓存接口public interface CacheManage { // 新增 <V> void set(String key, V value); // 新增 // timeSpan 单位:毫秒 <V> void set(String key, V value, Long timeSpan); // 获取 <V> V get(String key); // 移除 void remove(String key); // 判断是否存在 boolean exist(String key); // 续期 boolean expire(String key,Long timeSpan);} 12345678910111213141516171819202122232425262728293031323334353637383940@Componentpublic class RedisCacheManageImpl implements CacheManage { @Autowired RedisTemplate redisTemplate; @Override public <V> void set(String key, V value) { redisTemplate.opsForValue().set(key,value); } @Override public <V> void set(String key, V value, Long timeSpan) { redisTemplate.opsForValue().set(key,value,timeSpan,TimeUnit.MILLISECONDS); } @Override public <V> V get(String key) { ValueOperations<String,V> connect = redisTemplate.opsForValue(); return connect.get(key); } @Override public void remove(String key) { redisTemplate.delete(key); } @Override public boolean exist(String key) { return redisTemplate.hasKey(key); } @Override public boolean expire(String key, Long timeSpan) { return redisTemplate.expire(key, timeSpan,TimeUnit.MILLISECONDS); }} 123456789101112131415161718192021222324252627/** * 解决了springboot结合redis 添加key时产生的\xac\xed\x00\x05t\x00\b乱码 */@Configuration@EnableCachingpublic class RedisConfig { @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值 Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper mapper = new ObjectMapper(); mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); serializer.setObjectMapper(mapper); template.setValueSerializer(serializer); //使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); template.afterPropertiesSet(); return template; }} 123456789101112131415161718192021222324252627282930//service层 @Override public JSONObject login(String username, String password) { JSONObject jsonObject = new JSONObject(); boolean isu = cacheManage.get("username").equals(username); boolean isp = cacheManage.get("password").equals(password); if (isp && isu) { String u = cacheManage.get("username"); String p = cacheManage.get("password"); Role role = roleDao.login(u, p); if (role != null) { jsonObject.put("data","ss"); } } else { Role r1 = roleDao.login(username, password); if (r1 != null) { cacheManage.set("username", username); cacheManage.set("password", password); jsonObject.put("data","ss"); } else { return null; } } return jsonObject; } 赏个🍗吧 打赏 微信支付 支付宝 本文作者: Keeep 本文链接: http://Keeep.coding.me/blog/redis简单使用/ 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!