| | |
| | | package com.java110.core.cache; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Qualifier; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.cache.CacheManager; |
| | | import org.springframework.cache.annotation.CachingConfigurerSupport; |
| | | import org.springframework.cache.annotation.EnableCaching; |
| | |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; |
| | | import org.springframework.data.redis.serializer.RedisSerializationContext; |
| | | import org.springframework.util.StringUtils; |
| | | import redis.clients.jedis.JedisPool; |
| | | import redis.clients.jedis.JedisPoolConfig; |
| | | |
| | | import java.time.Duration; |
| | | |
| | | /** |
| | | * Created by wuxw on 2017/7/23. |
| | | */ |
| | | @Configuration |
| | | |
| | | @EnableCaching //开启缓存,默认是rendis缓存,继承CachingConfigurerSupport ,直接重写里面的方法 |
| | | public class Java110RedisConfig extends CachingConfigurerSupport { |
| | | @EnableCaching |
| | | public class Java110RedisConfig extends CachingConfigurerSupport { |
| | | |
| | | public final static String REDIS_EXPIRE_TIME_KEY = "#key_expire_time"; |
| | | |
| | |
| | | |
| | | @Autowired |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | @Bean(name= "jedisPool") |
| | | @Autowired |
| | | public JedisPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config, |
| | | @Value("${jedis.pool.host}")String host, |
| | | @Value("${jedis.pool.port}")int port, |
| | | @Value("${jedis.pool.timeout}")int timeout, |
| | | @Value("${jedis.pool.password}") String password) { |
| | | //没有配置改为默认值 |
| | | if(timeout == 0){ |
| | | timeout = 2000; |
| | | } |
| | | |
| | | if(StringUtils.isEmpty(password)) { |
| | | return new JedisPool(config, host, port,timeout); |
| | | }else{ |
| | | return new JedisPool(config,host,port,timeout,password); |
| | | } |
| | | } |
| | | |
| | | @Bean(name= "jedis.pool.config") |
| | | public JedisPoolConfig jedisPoolConfig (@Value("${jedis.pool.config.maxTotal}")int maxTotal, |
| | | @Value("${jedis.pool.config.maxIdle}")int maxIdle, |
| | | @Value("${jedis.pool.config.maxWaitMillis}")int maxWaitMillis) { |
| | | JedisPoolConfig config = new JedisPoolConfig(); |
| | | config.setMaxTotal(maxTotal); |
| | | config.setMaxIdle(maxIdle); |
| | | config.setMaxWaitMillis(maxWaitMillis); |
| | | |
| | | return config; |
| | | } |
| | | |
| | | |
| | | |
| | | @Override |
| | | @Bean |
| | |
| | | |
| | | return configuration; |
| | | } |
| | | |
| | | |
| | | } |