本文展示了如何通过注释配置将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.8repositories {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配置。

package com.gkatzioura.spring.config;import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {@Beanpublic JedisConnectionFactory redisConnectionFactory() {JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();jedisConnectionFactory.setUsePool(true);return jedisConnectionFactory;}@Beanpublic RedisSerializer redisStringSerializer() {StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();return stringRedisSerializer;}@Bean(name="redisTemplate")public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf,RedisSerializer redisSerializer) {RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();redisTemplate.setConnectionFactory(cf);redisTemplate.setDefaultSerializer(redisSerializer);return redisTemplate;}@Beanpublic CacheManager cacheManager() {return new RedisCacheManager(redisTemplate(redisConnectionFactory(),redisStringSerializer()));}}

下一步是创建我们的缓存界面

package com.gkatzioura.spring.cache;import java.util.Date;
import java.util.List;public interface CacheService {public void addMessage(String user,String message);public List<String> listMessages(String user);}

用户将添加消息,他将能够检索它们。 但是,在我们的实施中,与用户相关的消息将保留一分钟的时间。

我们使用Redis的CacheService实现如下。

package com.gkatzioura.spring.cache.impl;import com.gkatzioura.spring.cache.CacheService;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;@Service("cacheService")
public class RedisService implements CacheService {@Resource(name = "redisTemplate")private ListOperations<String, String> messageList;@Resource(name = "redisTemplate")private RedisOperations<String,String> latestMessageExpiration;@Overridepublic void addMessage(String user,String message) {messageList.leftPush(user,message);ZonedDateTime zonedDateTime = ZonedDateTime.now();Date date = Date.from(zonedDateTime.plus(1, ChronoUnit.MINUTES).toInstant());latestMessageExpiration.expireAt(user,date);}@Overridepublic List<String> listMessages(String user) {return messageList.range(user,0,-1);}}

我们的缓存机制将保留每个用户发送的消息列表。 为此,我们将使用用户作为键来设置ListOperations接口。 RedisOperations界面使我们能够指定密钥的生存时间。 在我们的情况下,它用作用户密钥。

接下来,我们创建一个注入了缓存服务的控制器。

package com.gkatzioura.spring.controller;import com.gkatzioura.spring.cache.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;@RestController
public class MessageController {@Autowiredprivate CacheService cacheService;@RequestMapping(value = "/message",method = RequestMethod.GET)@ResponseBodypublic List<String> greeting(String user) {List<String> messages = cacheService.listMessages(user);return messages;}@RequestMapping(value = "/message",method = RequestMethod.POST)@ResponseBodypublic String saveGreeting(String user,String message) {cacheService.addMessage(user,message);return "OK";}}

最后但并非最不重要的是我们的Application类

package com.gkatzioura.spring;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

为了运行只是问题

gradle bootRun

翻译自: https://www.javacodegeeks.com/2015/08/integrate-redis-to-your-spring-project.html

将Redis集成到您的Spring项目中相关推荐

  1. redis集成spring_将Redis集成到您的Spring项目中

    redis集成spring 本文介绍如何通过注释配置将Redis缓存集成到您的spring项目中. 我们将从Gradle配置开始. 我们将使用jedis驱动程序. group 'com.gkatzio ...

  2. 将MongoDB集成到您的Spring项目中

    本文展示了如何通过注释配置将MongoDB集成到您的spring项目中. 我们将从Gradle配置开始. group 'com.gkatzioura.spring' version '1.0-SNAP ...

  3. 将MongoDB集成到您的Spring项目

    本文展示了如何通过注释配置将MongoDB集成到您的spring项目中. 我们将从Gradle配置开始. group 'com.gkatzioura.spring' version '1.0-SNAP ...

  4. spring 项目中集成 Protocol Buffers 示例

    http://blog.csdn.net/fangzhangsc2006/article/details/8687388 本文适用于了解spring框架,同时想在spring项目中使用Protocol ...

  5. 在Spring项目中集成使用MongoDB

    在Spring项目中集成使用MongoDB 这里使用的是maven工程,所以依赖jar包都是pom.xml配置型式. pom.xml引入依赖 <properties><spring. ...

  6. 【SpringBoot】Spring项目中value注解,@Value不能够读取到配置文件的值,无法成功注入值的问题汇总及解决

    Spring项目中value注解,@Value不能够读取到配置文件的值,无法成功注入值的问题汇总及解决 @Value注解 常规用法示例 我们都知道通过@Value()注解可以取到我们配置文件的内容,之 ...

  7. 在Maven+Spring项目中使用Node.js的Gulp进行前端自动化构建

    摘要: 在Maven+Spring项目中使用Node.js的Gulp进行前端自动化构建 刚开始打算进行前后端分离开发,后来发现在使用JSP或者Freemarker做动态页面时,想发挥这些自动化构建工具 ...

  8. mybatis手动切换数据库_在Spring项目中使用 Mybatis 如何实现动态切换数据源

    在Spring项目中使用 Mybatis 如何实现动态切换数据源 发布时间:2020-11-17 16:20:11 来源:亿速云 阅读:108 作者:Leah 这篇文章将为大家详细讲解有关在Sprin ...

  9. Spring项目中 注解@Aspect无法被引入的原因

    Spring项目中 注解@Aspect无法被引入的原因 解决方式 解决方式    在maven 中引入最新版本的Aspectj依赖,官网显示为 <!-- https://mvnrepositor ...

最新文章

  1. python opencv 峰值信噪比
  2. 2021-01-07 matlab数值分析  矩阵特征值与特征向量的计算 改进乘幂法 反幂法
  3. DataTables中设置某些列不进行排序
  4. 1043:整数大小比较
  5. 【转载保存】lucene正则查询使用注意
  6. OpenGL:关于获取渲染结果的深度信息的问题(二)
  7. CVPR 2020 Oral:北大华为等提出只用加法的神经网络,重磅开源
  8. 服务器如何修复dll,Windows10系统修复KernelBase.dll错误的解决方法
  9. 【网络安全工程师面试合集】—邮件协议是如何被安全人员利用的?
  10. 从零开始编写自己的C#框架(7)——需求分析
  11. Skype国际版最新版及老版本下载
  12. 上海计算机在职专业硕士学校,专业硕士计算机类可以报考上海哪些学校?
  13. 30分钟快速了解云计算
  14. day10、2 - 小小综合实验升级
  15. 命令行运行coppeliasim(vrep)出现/usr/lib/x86_64-linux-gnu/libQt5Core.so.5: version `Qt_5.12‘ not found
  16. 新手小白适合做哪个跨境电商平台?测评自养号能带来哪些收益及优势?
  17. HTML设计一个图书管理网页
  18. 远程桌面全屏无法退出
  19. python聊天机器人源码_Python Google talk聊天机器人源码
  20. angular中安装ng-alain 插件

热门文章

  1. 【最全最详细】使用publiccms实现动态可维护的首页轮播
  2. 最全三大框架整合(使用映射)——IDeptDao.java
  3. 什么是mysql的主从复制?
  4. 2019蓝桥杯省赛---java---A---8(修改数组)
  5. spark submit参数及调优
  6. bean交个spring和new比较区别
  7. 铜仁学院计算机报名,铜仁学院2012年3月全国计算机等级考试报名时间通知
  8. 关于es查询dsl的filter与must,term与match的区别
  9. mybatis_user_guide(6) Java API
  10. 算法正义_正义联盟的Sprint Boot