您的位置:首页 > 数据库 > Redis

Redis整合Spring项目搭建实例指导

2015-09-06 10:22 645 查看
本文介绍了如何使用注解的方式,将Redis缓存整合到你的Spring项目。

首先我们将使用jedis驱动,进而开始配置我们的Gradle。

[Bash shell] 纯文本查看 复制代码

01
group
'com.gkatzioura.spring'
02
version
'1.0-SNAPSHOT'
03
apply
plugin:
'java'
04
apply
plugin:
'eclipse'
05
apply
plugin:
'idea'
06
apply
plugin:
'spring-boot'
07
buildscript
{
08
repositories
{
09
mavenCentral()
10
}
11
dependencies
{
12
classpath(
"org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE"
)
13
}
14
}
15
jar
{
16
baseName
=
'gs-serving-web-content'
17
version
=
'0.1.0'
18
}
19
sourceCompatibility
= 1.8
20
repositories
{
21
mavenCentral()
22
}
23
dependencies
{
24
compile
"org.springframework.boot:spring-boot-starter-thymeleaf"
25
compile
'org.slf4j:slf4j-api:1.6.6'
26
compile
'ch.qos.logback:logback-classic:1.0.13'
27
compile
'redis.clients:jedis:2.7.0'
28
compile
'org.springframework.data:spring-data-redis:1.5.0.RELEASE'
29
testCompile
group:
'junit'
,
name:
'junit'
,
version:
'4.11'
30
}
31
task
wrapper(
type
:
Wrapper) {
32
gradleVersion
=
'2.3'
33
}
紧接着我们将使用Spring注解,继续执行Redis装载配置。

[Bash shell] 纯文本查看 复制代码

01
package
com.gkatzioura.spring.config;
02
import
org.springframework.cache.CacheManager;
03
import
org.springframework.cache.annotation.CachingConfigurerSupport;
04
import
org.springframework.cache.annotation.EnableCaching;
05
import
org.springframework.context.annotation.Bean;
06
import
org.springframework.context.annotation.Configuration;
07
import
org.springframework.data.redis.cache.RedisCacheManager;
08
import
org.springframework.data.redis.connection.RedisConnectionFactory;
09
import
org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
10
import
org.springframework.data.redis.core.RedisTemplate;
11
import
org.springframework.data.redis.serializer.RedisSerializer;
12
import
org.springframework.data.redis.serializer.StringRedisSerializer;
13
@Configuration
14
@EnableCaching
15
public
class RedisConfig extends CachingConfigurerSupport {
16
@Bean
17
public
JedisConnectionFactory redisConnectionFactory() {
18
JedisConnectionFactory
jedisConnectionFactory = new JedisConnectionFactory();
19
jedisConnectionFactory.setUsePool(
true
);
20
return
jedisConnectionFactory;
21
}
22
@Bean
23
public
RedisSerializer redisStringSerializer() {
24
StringRedisSerializer
stringRedisSerializer = new StringRedisSerializer();
25
return
stringRedisSerializer;
26
}
27
@Bean(name=
"redisTemplate"
)
28
public
RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf,RedisSerializer redisSerializer) {
29
RedisTemplate<String,
String> redisTemplate = new RedisTemplate<String, String>();
30
redisTemplate.setConnectionFactory(cf);
31
redisTemplate.setDefaultSerializer(redisSerializer);
32
return
redisTemplate;
33
}
34
@Bean
35
public
CacheManager cacheManager() {
36
return
new
RedisCacheManager(redisTemplate(redisConnectionFactory(),redisStringSerializer()));
37
}
38
}
下一步将创建缓存接口CacheService

[Bash shell] 纯文本查看 复制代码

01
package
com.gkatzioura.spring.cache;
02
import
java.util.Date;
03
import
java.util.List;
04
public
interface CacheService {
05
public
void addMessage(String user,String message);
06
public
List<String> listMessages(String user);
07
}
当然用户既可以增加一条消息也能取回一条消息。因此,在实现过程中,用户相关信息的存在时间将默认设为一分钟。

我们用Redis来继承实现CacheService接口。

[Bash shell] 纯文本查看 复制代码

01
package
com.gkatzioura.spring.cache.impl;
02
import
com.gkatzioura.spring.cache.CacheService;
03
import
org.springframework.data.redis.core.ListOperations;
04
import
org.springframework.data.redis.core.RedisOperations;
05
import
org.springframework.data.redis.core.SetOperations;
06
import
org.springframework.stereotype.Service;
07
import
javax.annotation.Resource;
08
import
java.
time
.ZonedDateTime;
09
import
java.
time
.temporal.ChronoUnit;
10
import
java.util.Date;
11
import
java.util.List;
12
@Service(
"cacheService"
)
13
public
class RedisService implements CacheService {
14
@Resource(name
=
"redisTemplate"
)
15
private
ListOperations<String, String> messageList;
16
@Resource(name
=
"redisTemplate"
)
17
private
RedisOperations<String,String> latestMessageExpiration;
18
@Override
19
public
void addMessage(String user,String message) {
20
messageList.leftPush(user,message);
21
ZonedDateTime
zonedDateTime = ZonedDateTime.now();
22
Date
date
=
Date.from(zonedDateTime.plus(1, ChronoUnit.MINUTES).toInstant());
23
latestMessageExpiration.expireAt(user,
date
);
24
}
25
@Override
26
public
List<String> listMessages(String user) {
27
return
messageList.range(user,0,-1);
28
}
29
}
我们的缓存机制将保留每个用户发送的消息列表。为了实现这个功能我们将调用ListOperations接口,同时将每个user作为一个key键值。通过RedisOperations接口,我们可以为key设置特定存在时长。在本例中,主要使用的是 user key。

下一步我们将创建一个controller注入缓存服务。

[Bash shell] 纯文本查看 复制代码

01
package
com.gkatzioura.spring.controller;
02
import
com.gkatzioura.spring.cache.CacheService;
03
import
org.springframework.beans.factory.annotation.Autowired;
04
import
org.springframework.web.bind.annotation.*;
05
import
java.util.List;
06
@RestController
07
public
class MessageController {
08
@Autowired
09
private
CacheService cacheService;
10
@RequestMapping(value
=
"/message"
,method
= RequestMethod.GET)
11
@ResponseBody
12
public
List<String> greeting(String user) {
13
List<String>
messages = cacheService.listMessages(user);
14
return
messages;
15
}
16
@RequestMapping(value
=
"/message"
,method
= RequestMethod.POST)
17
@ResponseBody
18
public
String saveGreeting(String user,String message) {
19
cacheService.addMessage(user,message);
20
return
"OK"
;
21
}
22
}
最后完成类Application的创建。

[Bash shell] 纯文本查看 复制代码

view
source

print?

01
package
com.gkatzioura.spring;
02
import
org.springframework.boot.SpringApplication;
03
import
org.springframework.boot.autoconfigure.SpringBootApplication;
04
@SpringBootApplication
05
public
class Application {
06
public
static void main(String[] args) {
07
SpringApplication.run(Application.class,
args);
08
}
09
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: