shop--13.升级--Redis缓存技术
集成Redis
1.添加Jedis依赖
2.添加Fastjson依赖
1.安装redis http://www.runoob.com/redis/redis-install.html
1.引入redis依赖
<!--redis客户端:jedis--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.41</version> </dependency> <!--配置springBootConfiguration, 可以使用@ConfigurationProperties(prefix = "redis")--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
2.在application.properties加入redis配置文件
redis.host=127.0.0.1 redis.port=6379 redis.timeout=3 redis.pool.MaxTotal=10 redis.database=0 redis.pool.maxActive=100 redis.pool.maxIdle=20 redis.pool.maxWait=3 redis.pool.testOnBorrow=true
3. 定义redis的类,引入redis配置
@Component @ConfigurationProperties(prefix = "redis") public class RedisConfig { private String host; private int port; private int timeout; private int poolMaxTotal; private int poolMaxIdle; private int poolMaxWait; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public int getTimeout() { return timeout; } public void setTimeout(int timeout) { this.timeout = timeout; } public int getPoolMaxTotal() { return poolMaxTotal; } public void setPoolMaxTotal(int poolMaxTotal) { this.poolMaxTotal = poolMaxTotal; } public int getPoolMaxIdle() { return poolMaxIdle; } public void setPoolMaxIdle(int poolMaxIdle) { this.poolMaxIdle = poolMaxIdle; } public int getPoolMaxWait() { return poolMaxWait; } public void setPoolMaxWait(int poolMaxWait) { this.poolMaxWait = poolMaxWait; } }
4.RedisPoolFactory类
@Service public class RedisPoolFactory { @Autowired RedisConfig redisConfig; @Bean public JedisPool JedisPoolFactory(){ JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(redisConfig.getPoolMaxIdle()); jedisPoolConfig.setMaxTotal(redisConfig.getPoolMaxTotal()); jedisPoolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait() * 1000); JedisPool jedisPool = new JedisPool(jedisPoolConfig, redisConfig.getHost(), redisConfig.getPort(), redisConfig.getTimeout() * 1000); return jedisPool; } }
5.编写RedisService类
@Service public class RedisService { @Autowired JedisPool jedisPool; public <T> T get(String key, Class<T> clazz){ Jedis jedis = null; try{ jedis = jedisPool.getResource(); String str = jedis.get(key); T t = stringToBean(str, clazz); return t; }finally { jedis.close(); } } public <T> boolean set(String key, T value){ Jedis jedis = jedisPool.getResource(); String str = beanToString(value); if(str == null || str.length() <= 0){ return false; } jedis.set(key, str); return true; } private <T> String beanToString(T value) { if(value == null){ return null; } Class<?> clazz = value.getClass(); if(clazz == int.class || clazz == Integer.class){ return "" + value; }else if(clazz == String.class){ return (String) value; } else if(clazz == long.class || clazz == Long.class){ return "" + value; }else{ return JSON.toJSONString(value); } } private <T> T stringToBean(String str, Class<T> clazz) { if (str == null || str.length() <= 0 || clazz == null) { return null; } if (clazz == int.class || clazz == Integer.class) { return (T) Integer.valueOf(str); } else if (clazz == String.class) { return (T) str; } else if (clazz == long.class || clazz == Long.class) { return (T) Long.valueOf(str); } else { return JSON.toJavaObject(JSON.parseObject(str), clazz); } } }
6.controller中测试
@Autowired RedisService redisService; @RequestMapping(value = "/redis/set", method = RequestMethod.GET) @ResponseBody public boolean redisSet(){ boolean value = redisService.set("key2", "redis"); return value; } @RequestMapping(value = "/redis/get", method = RequestMethod.GET) @ResponseBody public Object redisGet(){ String l = redisService.get("key2", String.class); return l; }