将Redis集成到您的Spring项目中
本文展示了如何通过注释配置将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项目中相关推荐
- redis集成spring_将Redis集成到您的Spring项目中
redis集成spring 本文介绍如何通过注释配置将Redis缓存集成到您的spring项目中. 我们将从Gradle配置开始. 我们将使用jedis驱动程序. group 'com.gkatzio ...
- 将MongoDB集成到您的Spring项目中
本文展示了如何通过注释配置将MongoDB集成到您的spring项目中. 我们将从Gradle配置开始. group 'com.gkatzioura.spring' version '1.0-SNAP ...
- 将MongoDB集成到您的Spring项目
本文展示了如何通过注释配置将MongoDB集成到您的spring项目中. 我们将从Gradle配置开始. group 'com.gkatzioura.spring' version '1.0-SNAP ...
- spring 项目中集成 Protocol Buffers 示例
http://blog.csdn.net/fangzhangsc2006/article/details/8687388 本文适用于了解spring框架,同时想在spring项目中使用Protocol ...
- 在Spring项目中集成使用MongoDB
在Spring项目中集成使用MongoDB 这里使用的是maven工程,所以依赖jar包都是pom.xml配置型式. pom.xml引入依赖 <properties><spring. ...
- 【SpringBoot】Spring项目中value注解,@Value不能够读取到配置文件的值,无法成功注入值的问题汇总及解决
Spring项目中value注解,@Value不能够读取到配置文件的值,无法成功注入值的问题汇总及解决 @Value注解 常规用法示例 我们都知道通过@Value()注解可以取到我们配置文件的内容,之 ...
- 在Maven+Spring项目中使用Node.js的Gulp进行前端自动化构建
摘要: 在Maven+Spring项目中使用Node.js的Gulp进行前端自动化构建 刚开始打算进行前后端分离开发,后来发现在使用JSP或者Freemarker做动态页面时,想发挥这些自动化构建工具 ...
- mybatis手动切换数据库_在Spring项目中使用 Mybatis 如何实现动态切换数据源
在Spring项目中使用 Mybatis 如何实现动态切换数据源 发布时间:2020-11-17 16:20:11 来源:亿速云 阅读:108 作者:Leah 这篇文章将为大家详细讲解有关在Sprin ...
- Spring项目中 注解@Aspect无法被引入的原因
Spring项目中 注解@Aspect无法被引入的原因 解决方式 解决方式 在maven 中引入最新版本的Aspectj依赖,官网显示为 <!-- https://mvnrepositor ...
最新文章
- python opencv 峰值信噪比
- 2021-01-07 matlab数值分析 矩阵特征值与特征向量的计算 改进乘幂法 反幂法
- DataTables中设置某些列不进行排序
- 1043:整数大小比较
- 【转载保存】lucene正则查询使用注意
- OpenGL:关于获取渲染结果的深度信息的问题(二)
- CVPR 2020 Oral:北大华为等提出只用加法的神经网络,重磅开源
- 服务器如何修复dll,Windows10系统修复KernelBase.dll错误的解决方法
- 【网络安全工程师面试合集】—邮件协议是如何被安全人员利用的?
- 从零开始编写自己的C#框架(7)——需求分析
- Skype国际版最新版及老版本下载
- 上海计算机在职专业硕士学校,专业硕士计算机类可以报考上海哪些学校?
- 30分钟快速了解云计算
- day10、2 - 小小综合实验升级
- 命令行运行coppeliasim(vrep)出现/usr/lib/x86_64-linux-gnu/libQt5Core.so.5: version `Qt_5.12‘ not found
- 新手小白适合做哪个跨境电商平台?测评自养号能带来哪些收益及优势?
- HTML设计一个图书管理网页
- 远程桌面全屏无法退出
- python聊天机器人源码_Python Google talk聊天机器人源码
- angular中安装ng-alain 插件
热门文章
- 【最全最详细】使用publiccms实现动态可维护的首页轮播
- 最全三大框架整合(使用映射)——IDeptDao.java
- 什么是mysql的主从复制?
- 2019蓝桥杯省赛---java---A---8(修改数组)
- spark submit参数及调优
- bean交个spring和new比较区别
- 铜仁学院计算机报名,铜仁学院2012年3月全国计算机等级考试报名时间通知
- 关于es查询dsl的filter与must,term与match的区别
- mybatis_user_guide(6) Java API
- 算法正义_正义联盟的Sprint Boot