连接redis---->jedis
(1)导入依赖
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
(2)操作redis
package com.wyj.test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import java.util.Set;
public class TestJedis {
public static void main(String[] args) {
Jedis jedis=new Jedis("192.168.213.188",6379);//必须运行远程连接 必须防火墙放行该端口号
//关于字符串
jedis.set("k1","v1");
jedis.set("k8","18");
jedis.mset("k2","v2","k3","v3","k4","v4");
jedis.setnx("k1","12");
jedis.decr("k8");
//操作key
Set<String> keys = jedis.keys("*");
System.out.println("所有的key:"+keys);
jedis.del("k1");
jedis.expire("k2",60);
jedis.ttl("k2");
}
}
springboot整合redis
(1)引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
(2)配置文件application
spring.redis.host=192.168.192.129
spring.redis.port=6379
spring.redis.jedis.pool.max-active=20
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.jedis.pool.max-wait=20000
(3)测试
package com.wyj.springbootredis01;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
@SpringBootTest
class SpringbootRedis01ApplicationTests {
@Autowired
private StringRedisTemplate redisTemplate;
@Test
void contextLoads() {
//操作key
Set<String> keys = redisTemplate.keys("*");
System.out.println(keys);
redisTemplate.delete("k8");
System.out.println(redisTemplate.getExpire("k3"));
redisTemplate.expire("k3", 60, TimeUnit.SECONDS);
System.out.println(redisTemplate.getExpire("k3"));
}
@Test
public void testString(){
ValueOperations<String, String> forValue = redisTemplate.opsForValue();//操作字符串类型
forValue.set("k1","v1");
System.out.println(forValue.get("k1"));
Map<String,String> map=new HashMap<>();
map.put("k9","v9");
map.put("k10","v10");
forValue.multiSet(map);
List<String> keys=new ArrayList<>();
keys.add("k1");
keys.add("k2");
keys.add("k4");
List<String> strings = forValue.multiGet(keys);
System.out.println(strings);
System.out.println(forValue.setIfAbsent("k2", "v2"));
}
}
springboot连接redis
package com.wyj.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;
import java.time.Duration;
@EnableCaching
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@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)) //缓存过期10分钟 ---- 业务需求。
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))//设置key的序列化方式
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) //设置value的序列化
.disableCachingNullValues();
RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
return cacheManager;
}
}
springboot连接redis集群
(1)哨兵
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=192.168.192.129:26379
(2) 去中心化集群
spring.redis.cluster.nodes=192.168.192.129:8001,
192.168.192.129:8002,
192.168.192.129:8003,
192.168.192.129:8004,
192.168.192.129:8005,
192.168.192.129:8006