# application properties
spring.redis.database=0
spring.redis.host=192.168.99.100
spring.redis.port=6379
spring.redis.password=*******
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.timeout=0
# Bean definition in configuration class, for manual access redis server
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
# In service classes inject redis templates
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
public void testRedis(){
this.redisTemplate.opsForValue().set("test1", 123);
Object test1 = this.redisTemplate.opsForValue().get("test1");
this.stringRedisTemplate.opsForValue().set("email", "ercanxcan@gmail.com");
String email1 = this.stringRedisTemplate.opsForValue().get("email");
String id = "2017/11/08";
RedisAtomicLong counter = new RedisAtomicLong(id, redisTemplate.getConnectionFactory());
long automicCounter = counter.incrementAndGet();
counter.expire(1, TimeUnit.DAYS);
}
# Caching with Cacheable annotations
@Cacheable(value="sec.user", key = "#userId")
@Caching(evict = {@CacheEvict(value="sec.user", key = "#userId")})
@Caching(put = {@CachePut(value="sec.user", key = "#user.getId()")})
spring.redis.database=0
spring.redis.host=192.168.99.100
spring.redis.port=6379
spring.redis.password=*******
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.timeout=0
# Bean definition in configuration class, for manual access redis server
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
# In service classes inject redis templates
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
public void testRedis(){
this.redisTemplate.opsForValue().set("test1", 123);
Object test1 = this.redisTemplate.opsForValue().get("test1");
this.stringRedisTemplate.opsForValue().set("email", "ercanxcan@gmail.com");
String email1 = this.stringRedisTemplate.opsForValue().get("email");
String id = "2017/11/08";
RedisAtomicLong counter = new RedisAtomicLong(id, redisTemplate.getConnectionFactory());
long automicCounter = counter.incrementAndGet();
counter.expire(1, TimeUnit.DAYS);
}
# Caching with Cacheable annotations
@Cacheable(value="sec.user", key = "#userId")
@Caching(evict = {@CacheEvict(value="sec.user", key = "#userId")})
@Caching(put = {@CachePut(value="sec.user", key = "#user.getId()")})
Comments
Post a Comment