當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
将Redis集成到您的Spring项目中
生活随笔
收集整理的這篇文章主要介紹了
将Redis集成到您的Spring项目中
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
本文展示了如何通過注釋配置將Redis緩存集成到您的spring項目中。
我們將從Gradle配置開始。 我們將使用jedis驅(qū)動程序。
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()));}}下一步是創(chuàng)建我們的緩存界面
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);}用戶將添加消息,他將能夠檢索它們。 但是,在我們的實施中,與用戶相關(guān)的消息將保留一分鐘的時間。
我們使用Redis的CacheService實現(xiàn)如下。
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);}}我們的緩存機制將保留每個用戶發(fā)送的消息列表。 為此,我們將使用用戶作為鍵來設(shè)置ListOperations接口。 RedisOperations界面使我們能夠指定密鑰的生存時間。 在我們的情況下,它用作用戶密鑰。
接下來,我們創(chuàng)建一個注入了緩存服務(wù)的控制器。
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
總結(jié)
以上是生活随笔為你收集整理的将Redis集成到您的Spring项目中的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电信光猫连接路由器用手机怎么连手机如何桥
- 下一篇: hibernate示例_通过示例Hibe