本文展示了如何通过注解配置将Redis缓存集成到你的spring项目中。
我们将从 Gradle 配置开始。我们将使用 jedis 驱动程序。
group 'com.gkatzioura.spring'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile 'org.slf4j:slf4j-api:1.6.6'
compile 'ch.qos.logback:logback-classic:1.0.13'
compile 'redis.clients:jedis:2.7.0'
compile 'org.springframework.data:spring-data-redis:1.5.0.RELEASE'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
将使用 spring 注释继续进行 Redis 配置。
group 'com.gkatzioura.spring'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile 'org.slf4j:slf4j-api:1.6.6'
compile 'ch.qos.logback:logback-classic:1.0.13'
compile 'redis.clients:jedis:2.7.0'
compile 'org.springframework.data:spring-data-redis:1.5.0.RELEASE'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
下一步是创建我们的缓存接口
group 'com.gkatzioura.spring'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile 'org.slf4j:slf4j-api:1.6.6'
compile 'ch.qos.logback:logback-classic:1.0.13'
compile 'redis.clients:jedis:2.7.0'
compile 'org.springframework.data:spring-data-redis:1.5.0.RELEASE'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
用户将添加消息并且他将能够检索它们。
然而,在我们的实施中,与用户相关的消息将有一分钟的生存时间。
我们使用 Redis 实现 CacheService 如下。
group 'com.gkatzioura.spring'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile 'org.slf4j:slf4j-api:1.6.6'
compile 'ch.qos.logback:logback-classic:1.0.13'
compile 'redis.clients:jedis:2.7.0'
compile 'org.springframework.data:spring-data-redis:1.5.0.RELEASE'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
我们的缓存机制将保留每个用户发送的消息列表。为此,我们将使用用户作为键来使用 ListOperations 接口。
RedisOperations 接口使我们能够指定键的生存时间。在我们的例子中,它用于用户密钥。
接下来我们创建一个注入缓存服务的控制器。
group 'com.gkatzioura.spring'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile 'org.slf4j:slf4j-api:1.6.6'
compile 'ch.qos.logback:logback-classic:1.0.13'
compile 'redis.clients:jedis:2.7.0'
compile 'org.springframework.data:spring-data-redis:1.5.0.RELEASE'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
最后但同样重要的是我们的应用程序类
group 'com.gkatzioura.spring'
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")
}
}
jar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile 'org.slf4j:slf4j-api:1.6.6'
compile 'ch.qos.logback:logback-classic:1.0.13'
compile 'redis.clients:jedis:2.7.0'
compile 'org.springframework.data:spring-data-redis:1.5.0.RELEASE'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
为了运行公正的问题