spring에서 제공하는 RedisTemplate을 함께 사용하는 방법이 있다.
RedisTemplate을 사용하면 redis client 라이브러리의 종류에 상관없이 사용할 수 있어 좋고,
redis가 지원하는 자료구조를 사용하기 좋게 랩핑을 해놓아 좋다.
jedis를 그냥 써도 좋지만, byte[] 타입으로 컨버팅 노가다를 해야 한다.
어려운 길을 가지말고 RedisTemplate을 사용하기로 해보자.
pom.xml에 추가
1
2
3
4
5
6
7
8
9
10
11
12
|
<!-- jedis --> < dependency > < groupId >redis.clients</ groupId > < artifactId >jedis</ artifactId > < version >2.2.1</ version > </ dependency > <!-- spring data redis --> < dependency > < groupId >org.springframework.data</ groupId > < artifactId >spring-data-redis</ artifactId > < version >1.2.0.RELEASE</ version > </ dependency > |
root-context.xml에 추가
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://www.springframework.org/schema/beans > <!-- Root Context: defines shared resources visible to all other web components --> < mvc:annotation-driven conversion-service = "conversionService" /> < bean id = "jedisConnectionFactory" class = "org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool = "true" p:host-name = "172.20.44.209" p:port = "6379" /> <!-- redis template definition --> < bean id = "redisTemplate" class = "org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref = "jedisConnectionFactory" /> </ beans > |
다양하게 사용해 보자
1. 가장 쉽게 사용해 보기
사용할 타입을 RedisTemplate과 자료구조에 지정하여 사용
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
|
/** * Handles requests for the application home page. */ @Controller @RequestMapping (value= "/home" ) public class HomeController { private static final Logger logger = LoggerFactory.getLogger(HomeController. class ); @Autowired RedisTemplate<String, String> redisTemplate; @Resource (name= "redisTemplate" ) private ValueOperations<String, String> valueOps; @RequestMapping (value= "/jedis2" ) public String jedis2(){ valueOps.set( "foo" , "bar" ); logger.info( "Welcome jedis user {}." ,valueOps.get( "foo" )); return "home" ; } } |
2. Custom class 이용해 보기
Serializable 를 구현한 User class를 넣어보자
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
|
public class User implements Serializable { @NotNull String name; @Min ( 0 ) @Max ( 100 ) int id; Level level; public void setLevel(Level _level) { level = _level; } public Level getLevel() { return level; } public void setId( int _id) { id = _id; } public int getId() { return id; } public void setName(String _name) { name = _name; } public String getName() { return name; } } |
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
|
/** * Handles requests for the application home page. */ @Controller @RequestMapping (value= "/home" ) public class HomeController { private static final Logger logger = LoggerFactory.getLogger(HomeController. class ); @Autowired RedisTemplate<String, User> redisTemplate; @Resource (name= "redisTemplate" ) private ValueOperations<String, User> valueOps; @RequestMapping (value= "/jedis2" ) public String jedis2(){ User user = new User(); user.setId( 4 ); user.setName( "warwick" ); user.setLevel(Level.GOLD); valueOps.set( "foo" , user); return "home" ; } @RequestMapping (value= "/jedis3" ) public String jedis3(){ User savedUser = valueOps.get( "foo" ); logger.info( "Welcome jedis user " + savedUser.id + " name : " + savedUser.name + " level : " + savedUser.level); return "home" ; } } |
결과
INFO : com.yakolla.mvctest.HomeController – Welcome jedis user 4 name : warwick level : GOLD
3. valueOperations 없이 사용해보기
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
|
public class HomeController { private static final Logger logger = LoggerFactory.getLogger(HomeController. class ); @Autowired RedisTemplate<String, Object> redisTemplate; @RequestMapping (value= "/jedis2" ) public String jedis2(){ User user = new User(); user.setId( 6 ); user.setName( "warwick2" ); user.setLevel(Level.SILVER); redisTemplate.opsForValue().set( "foo" , user); return "home" ; } @RequestMapping (value= "/jedis3" ) public String jedis3(){ User savedUser = (User)redisTemplate.opsForValue().get( "foo" ); logger.info( "Welcome jedis user " + savedUser.id + " name : " + savedUser.name + " level : " + savedUser.level); return "home" ; } } |
결과
INFO : com.yakolla.mvctest.HomeController – Welcome jedis user 6 name : warwick2 level : SILVER