<!-- end redis 配置-->
2: 在spring boot *.properties 配置文件中增加连接配置:
spring.redis.hostName=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.pool.maxActive=8
spring.redis.pool.maxWait=-1
spring.redis.pool.maxIdle=8
spring.redis.pool.minIdle=0
spring.redis.timeout=0
3: spring boot 中增加redis 配置连接
/** * Created by Administrator on 2017/2/27.
*/
@Configuration
@EnableAutoConfiguration
public class RedisConfig {
private static Logger logger = LoggerFactory.getLogger(RedisConfig.class);
@Bean
@ConfigurationProperties(prefix="spring.redis")
public JedisPoolConfig getRedisConfig(){
JedisPoolConfig config = new JedisPoolConfig();
return config;
}
@Bean
@ConfigurationProperties(prefix="spring.redis")
public RedisConnectionFactory jedisConnectionFactory(){
return new JedisConnectionFactory(getRedisConfig());
}
@Bean
public RedisTemplate<String, Object> redisTemplate(){
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
//这个缓存key的序列化方式
template.setKeySerializer(new StringRedisSerializer());
//设置redis 转换 value 通过jdk序列化方法,GenericJackson2JsonRedisSerializer 这个是json形式
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(jedisConnectionFactory());
return template;
}
/**
* 这个是redis 多值操作返回模版对象。比如:map,set ,list 等
* @return
*/ @Bean
public ValueOperations<String, Object> valueOperations(){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
//这个缓存key的序列化方式
redisTemplate.setKeySerializer(new StringRedisSerializer());
//这个缓存value的序列化方式,通过有jdk序列化,这个是json 序列化
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setExposeConnection(true);
redisTemplate.setConnectionFactory(jedisConnectionFactory());
redisTemplate.afterPropertiesSet();
return redisTemplate.opsForValue();
}
}
4: RedisTemplate 使用
@Service
public class RedisServiceImpl implements RedisService{
@Autowired
private RedisTemplate redisTemplate;
public boolean expire(final String key, long expire,final TimeUnit unit) {
return redisTemplate.expire(key, expire, unit);
}
@Override
public void setList(String key, List<String> list, long expire, TimeUnit unit) throws Exception {
redisTemplate.delete(key);
redisTemplate.opsForValue().set(key,list,expire,unit);
}
public List<String> getList(String key) {
return (List<String>)redisTemplate.opsForValue().get(key);
}
public void remove(String key){
redisTemplate.delete(key);
}
@Override
public void setKey(String key, String s, long expire, TimeUnit unit) {
redisTemplate.opsForValue().set(key,s,expire,unit);
}
@Override
public String getKey(String key) {
return (String) redisTemplate.opsForValue().get(key);
}
@Override
public Object getKeyForObject(String key) {
return redisTemplate.opsForValue().get(key);
}
@Override
public void setMap(String key, Map<String, Object> map, long expire, TimeUnit unit) throws Exception {
this.expire(key,expire,unit);
redisTemplate.opsForHash().putAll(key,map);
}
@Override
public void removeKeyForMap(String key, String mapKey) {
redisTemplate.opsForHash().delete(key,mapKey);
}
@Override
public void putMapKey(String key, String mapKey, String mapValue) {
redisTemplate.opsForHash().put(key,mapKey,mapValue);
}
public Set<String> keys(String o ){
return redisTemplate.keys(o);
}
}
5:单元测试类
RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class)// 指定spring-boot的启动类
//相当于 --spring.profiles.active=dev
@ActiveProfiles(value = "dev")
public class RedisCacheTest {
protected final Logger logger = Logger.getLogger(this.getClass());
@Autowired
private RedisService redisService;
@Autowired
private RedisTemplate redisTemplate;
@Test
public void CacheTest() throws Exception {
redisService.setKey("111111","222222",100, TimeUnit.SECONDS);
redisService.getKey("111111");
}
@Test
public void SaveObjectForCacheTest()throws Exception{
TargetDB targetDB=new TargetDB();
targetDB.setDbType("111");
targetDB.setPort("22");
targetDB.setUserName("zzzlyr");
redisTemplate.opsForValue().set("key111",targetDB);
TargetDB targetDB12= (TargetDB) redisTemplate.opsForValue().get("key111");
System.out.println(targetDB12.toString());
}
@Test
public void SaveMapForCacheTest()throws Exception {
TargetDB targetDB = new TargetDB();
targetDB.setDbType("111");
targetDB.setPort("22");
targetDB.setUserName("zzzlyr");
List<TargetDB> list = new LinkedList<TargetDB>();
list.add(targetDB);
Map<String, List<TargetDB>> map = new LinkedHashMap<String, List<TargetDB>>();
map.put("111111111111", list);
//向缓存中放入map
redisTemplate.opsForHash().putAll("zzzzz", map);
//从缓存中获取map
Map<String, List<TargetDB>> cacheMap = redisTemplate.opsForHash().entries("zzzzz");
//Map<String,List<String>> cacheMap= (Map<String, List<String>>) redisTemplate.opsForValue().get("platform-app_AppOperation");
System.out.println(cacheMap.toString());
}
}