在pom.xml引入jar包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> |
application-redis.properties中配置Redis连接信息
|
在application.properties中加redis的配置信息
spring.profiles.active=redis |
新建Redis缓存配置了RedisConfig
import org.springframework.beans.factory.annotation.Autowired; import redis.clients.jedis.JedisPool; /** @Value("${spring.redis.host}") @Value("${spring.redis.port}") @Value("${spring.redis.timeout}") @Value("${spring.redis.password}") @Value("${spring.redis.database}") @Value("${spring.redis.pool.max-idle}") @Value("${spring.redis.pool.min-idle}") // @Value("${spring.redis.pool.max-active}") // @Value("${spring.redis.pool.max-wait}")
@Bean @Autowired @Bean /** |
新建JedisClient缓存客户端类
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import redis.clients.jedis.Jedis; public class JedisClient { @Autowired public Jedis getResource() { public void delete(String key) { public String get(String key) { public void set(String key, String value) { public void set(String key, String value, long seconds) { public void set(String key, String value, int seconds) { /** public void setList(String key, List<?> list, long seconds) { /** public List<?> getList(String key) { public boolean exists(String key) { public void expire(String key, int seconds) { |
序列化工具类
import java.io.ByteArrayInputStream; public class SerializeUtil { public static byte[] serialize(Object object){ public static Object deserialize(byte[] bytes){ |
新建EquipmentService类
@Component
@Autowired
@Autowired
@SuppressWarnings("unchecked") List<Equipment> list; //将listEqui已equipment键存入redis,缓存有效期是10分钟 //根据键从缓存中获取liatEqui } |
运行之后可以在Redis数据库中看到以equipment为key的数据。不过list是序列化后存储的,所以是字节形式