将 Redis 集成到 Spring 项目中

一则或许对你有用的小广告

欢迎加入小哈的星球 ,你将获得:专属的项目实战 / Java 学习路线 / 一对一提问 / 学习打卡/ 赠书活动

目前,正在 星球 内带小伙伴们做第一个项目:全栈前后端分离博客项目,采用技术栈 Spring Boot + Mybatis Plus + Vue 3.x + Vite 4手把手,前端 + 后端全栈开发,从 0 到 1 讲解每个功能点开发步骤,1v1 答疑,陪伴式直到项目上线,目前已更新了 204 小节,累计 32w+ 字,讲解图:1416 张,还在持续爆肝中,后续还会上新更多项目,目标是将 Java 领域典型的项目都整上,如秒杀系统、在线商城、IM 即时通讯、权限管理等等,已有 870+ 小伙伴加入,欢迎点击围观

本文展示了如何通过注解配置将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' }

为了运行公正的问题