1、依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、配置:
#redis
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.timeout=5000
3、RedisConfig.java
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.core.StringRedisTemplate;
@Configuration
@EnableCaching
public class RedisConfig {
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisCacheManager cacheManager = RedisCacheManager.create(factory);
return cacheManager;
}
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
@Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(factory);
return stringRedisTemplate;
}
}
4、UserCacheBean
import java.io.Serializable;
public class UserCacheBean implements Serializable {
private static final long serialVersionUID = 3221700752972709820L;
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public UserCacheBean(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
}
5、UserCacheService
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.wec.light.mq.center.cache.bean.UserCacheBean;
@Service
public class UserCacheService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void set(String key, UserCacheBean user) {
redisTemplate.opsForValue().set(key, user);
}
public UserCacheBean get(String key) {
return (UserCacheBean) redisTemplate.boundValueOps(key).get();
}
public void setCode(String key, String code) {
stringRedisTemplate.opsForValue().set(key, code, 60, TimeUnit.SECONDS);
}
public String getCode(String key) {
return stringRedisTemplate.boundValueOps(key).get();
}
}
6、RedisController
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wec.light.mq.center.cache.bean.UserCacheBean;
import com.wec.light.mq.center.cache.service.UserCacheService;
@RestController
@RequestMapping("redis")
public class RedisController {
@Resource
private UserCacheService userCacheService;
@RequestMapping("set")
public void set() {
userCacheService.set("key1", new UserCacheBean(1, "test", 26));
}
@RequestMapping("get")
public UserCacheBean get() {
return userCacheService.get("key1");
}
@RequestMapping("stringset")
public void stringset() {
userCacheService.setCode("stringkey", "test");
}
@RequestMapping("stringget")
public String stringget() {
return userCacheService.getCode("stringkey");
}
}