今天小编给大家分享一下Java操作Redis的方法是什么的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
Jedis操作Redis
创建idea工程
建立一个Maven项目,导入Jedis依赖即可:
<!-- 导入Jedis依赖 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.2.0</version> </dependency>
导入依赖之后创建JedisDemo测试类
package com.vleus.jedis; import redis.clients.jedis.Jedis; /** * @author vleus * @date 2021年07月03日 23:52 */ public class JedisDemo1 { public static void main(String[] args) { //创建Jedis对象 Jedis jedis = new Jedis("192.168.37.139",6379); //测试Jedis客户端是否能够连接上Redis System.out.println(jedis.ping()); } }
输出PONG表示连接成功~
注意:
第一次测试时如果连接不上虚拟机的Redis,报连接不上的异常有两个问题需要解决:
首先,kill掉Redis的进程,然后修改Reids的启动使用的配置文件
注释掉bind的配置;
同时将protected-mode的值改为no
第二步,关掉Linux的防火墙(这里仅是在做学习时使用,生产环境别这么干)
systemctl stop firewalld systemctl disable firewalld
反正我是这么解决的。
Jedis操作Redis
1、创建idea springboot项目,引入依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- spring-boot-redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- spring2.x继承redis需要commons-pool --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.6.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.3</version> </dependency> </dependencies>
2、创建RedisConfig配置类
@EnableCaching @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setConnectionFactory(factory); //key序列化方式 template.setKeySerializer(redisSerializer); //value序列化 template.setValueSerializer(jackson2JsonRedisSerializer); //value hashmap序列化 template.setHashValueSerializer(jackson2JsonRedisSerializer); return template; } @Bean public CacheManager cacheManager(RedisConnectionFactory factory) { RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); //解决查询缓存转换异常的问题 ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); // 配置序列化(解决乱码的问题),过期时间600秒 RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofSeconds(600)) .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)) .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) .disableCachingNullValues(); RedisCacheManager cacheManager = RedisCacheManager.builder(factory) .cacheDefaults(config) .build(); return cacheManager; } }
简单的Controller测试
/** * @author vleus * @date 2021年07月04日 13:06 */ @RestController @RequestMapping(value = "/redisTest") public class RedisTestController { @Resource private RedisTemplate redisTemplate; @GetMapping public String testRedis() { //设置值到redis redisTemplate.opsForValue().set("name","jack"); //从redis中获取值 String name = (String) redisTemplate.opsForValue().get("name"); return name; } }