第一步:配置yml文件
redis:
host: 127.0.0.1
port: 6379
password:
# 连接超时时间(毫秒)
timeout: 10000
pool:
max-idle: 20
min-idle: 5
max-active: 20
max-wait: 2
第二步:编写配置类
package org.spring.springcloud.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPoolConfig;
/**
* Created by liyu on 2018/4/27.
*/
@Configuration
public class RedisConfiguration {
//(一)第一步:配置连接池,
/*
@ConfigurationProperties和@Bean和在一起使用
举个例子,我们需要用@Bean配置一个Config对象,Config对象有a,b,c成员变量需要配置,
那么我们只要在yml或properties中定义了a=1,b=2,c=3,然后通过@ConfigurationProperties就能把值注入进Config对象中
*/
@ConfigurationProperties(prefix="redis.pool")
public JedisPoolConfig getRedisConfig(){
JedisPoolConfig config = new JedisPoolConfig();
System.out.println(" max-idle="+config.getMaxIdle());
return config;
}
//第二步:配置工厂类JedisConnectionFactory,并设置ip,端口和连接池
@Bean
@ConfigurationProperties(prefix="redis")
public JedisConnectionFactory getConnectionFactory(){
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setUsePool(true);
JedisPoolConfig config = getRedisConfig();
factory.setPoolConfig(config);
return factory;
}
//第三步:创建StringRedisTemplate操作类,程序中可直接使用该类进行增加,删除等操作
@Bean
public RedisTemplate<?, ?> getRedisTemplate(){
RedisTemplate<?,?> template = new StringRedisTemplate(getConnectionFactory());
return template;
}
第三步:程序插入查询
package org.spring.springcloud.service.impl;
import org.spring.springcloud.dao.CityDao;
import org.spring.springcloud.domain.City;
import org.spring.springcloud.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class CityServiceImpl implements CityService {
@Autowired
private CityDao cityDao;
//存对象,被存对象必须序列化
@Autowired
RedisTemplate redisTemplate;
//存String
@Autowired
StringRedisTemplate template;
//存指定对象,city对象必须序列化
@Resource(name = "redisTemplate")
private RedisTemplate<String,City> cityRedisTemplate;
public City findCityByName(String cityName) {
// City city=cityDao.findByName(cityName);
//设置字符串
//template.opsForValue().set("mykey100", "helloredis");
template.opsForValue().set("hk2","hello hk2");
System.out.println("-----------"+template.opsForValue().get("hk2"));
//设置对象
// ValueOperations<String, City> operations = redisTemplate.opsForValue();
//operations.set("liyu", city);
// 缓存存在
// boolean hasKey = redisTemplate.hasKey("liyu");
// if (hasKey) {
// System.out.println("....>>"+operations.get("liyu"));
// }
return cityDao.findByName(cityName);
}
}
从上面可以看到我们已经成功插入了值