Gradle
compile('org.springframework.boot:spring-boot-starter-data-redis')
compile group: 'redis.clients', name: 'jedis', version: '2.9.0'
application.properties 설정
spring.cache.type=redis
spring.redis.host = 127.0.0.1
spring.redis.password=
spring.redis.port=6379
RedisConfig 클래스 생성
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package com.example.service.redis; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; import redis.clients.jedis.JedisPoolConfig; @Configuration @ComponentScan("com.spring.redis") public class RedisConfig { private @Value("${spring.redis.host}") String redisHost; private @Value("${spring.redis.port}") int redisPort; private @Value("${spring.redis.password}") String password; @Bean public JedisPoolConfig jedisPoolConfig() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(30); jedisPoolConfig.setMinIdle(10); jedisPoolConfig.setTestOnBorrow(true); jedisPoolConfig.setTestOnReturn(true); return jedisPoolConfig; } @SuppressWarnings("deprecation") @Bean public JedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig()); jedisConnectionFactory.setHostName(redisHost); jedisConnectionFactory.setPort(redisPort); jedisConnectionFactory.setPassword(password); jedisConnectionFactory.setUsePool(true); return jedisConnectionFactory; } @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory()); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new StringRedisSerializer()); template.setHashKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(new StringRedisSerializer()); template.setEnableDefaultSerializer(false); template.setEnableTransactionSupport(true); return template; } } | cs |
Redis Client에서 가장 많이 사용되는 오픈소스는 Jedis/lettuce/Redisson 중 Jedis이다.
많이 쓴다는 것은 곧 그만큼 사용하기 쉽고 레퍼런스가 많다는 의미이다.
실제로 Jedis를 통해 손쉽게 사용할 수 있었다.
Spring에서 RedisTemplate을 통해 데이터에 엑세스할 뿐 아니라 ConnectionPool의 자원 관리 또한 알아서 해준다.
=> 실제론 JedisConnectionFactory를 통해서 수행
RedisTemplate을 통해 다음과 같은 데이터 타입을 다룰 수 있다.
String
@Resource(name="redisTemplate")
private ValueOperations<String, String> valueOperations;
Set
@Resource(name="redisTemplate")
private SetOperations<String, String> setOperations;
Sorted Set
@Resource(name="redisTemplate")
private ZSetOperations<String, String> zSetOperations;
Hashes
@Resource(name="redisTemplate")
private HashOperations<String, String, String> hashOperations;
List
@Resource(name="redisTemplate")
private ListOperations<String, String> listOperations;
'Cloud & NoSQL & Middleware > Redis' 카테고리의 다른 글
Redis Library (2) | 2018.11.17 |
---|---|
Single Thread (0) | 2018.11.17 |
Redis 명령어 (0) | 2018.08.04 |
Redis Pub/Sub Model (0) | 2018.07.01 |
Java에서 Redis 연동 테스트 (jedis) (0) | 2018.07.01 |