当前位置: 首页 > news >正文

手机网站建设图dns网站建设

手机网站建设图,dns网站建设,广告营销方式,wordpress后台无法预览文章redis集成spring本文介绍如何通过注释配置将Redis缓存集成到您的spring项目中。 我们将从Gradle配置开始。 我们将使用jedis驱动程序。 group com.gkatzioura.spring version 1.0-SNAPSHOTapply plugin: java apply plugin: eclipse apply plugin: idea apply plugin: spring… redis集成spring 本文介绍如何通过注释配置将Redis缓存集成到您的spring项目中。 我们将从Gradle配置开始。 我们将使用jedis驱动程序。 group com.gkatzioura.spring version 1.0-SNAPSHOTapply plugin: java apply plugin: eclipse apply plugin: idea apply plugin: spring-bootbuildscript {repositories {mavenCentral()}dependencies {classpath(org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE)} }jar {baseName gs-serving-web-contentversion 0.1.0 }sourceCompatibility 1.8repositories {mavenCentral() }dependencies {compile org.springframework.boot:spring-boot-starter-thymeleafcompile org.slf4j:slf4j-api:1.6.6compile ch.qos.logback:logback-classic:1.0.13compile redis.clients:jedis:2.7.0compile org.springframework.data:spring-data-redis:1.5.0.RELEASEtestCompile 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(nameredisTemplate)public RedisTemplateString, String redisTemplate(RedisConnectionFactory cf,RedisSerializer redisSerializer) {RedisTemplateString, String redisTemplate new RedisTemplateString, 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 ListString 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 ListOperationsString, String messageList;Resource(name redisTemplate)private RedisOperationsString,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 ListString 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 ListString greeting(String user) {ListString 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.htmlredis集成spring
http://www.zqtcl.cn/news/754340/

相关文章:

  • 晓风彩票网站建设软件微信上发的链接网站怎么做的
  • 关键词有哪几种台州优秀关键词优化
  • 盐田区住房和建设局网站软件开发文档怎么编写
  • 网站响应式建设seo排名优化怎样
  • 山东 网站备案德清县建设局网站
  • 中英语双语网站咋做提供网站建设设计外包
  • 云网站功能江门网站seo关键词排名优化
  • 潍坊网站建设外贸制作html网站
  • 网站友情链接怎么添加定制酒营销方案
  • 目前最流行网站开发软件泰州市建设工程招标网
  • 福州网站优化me域名网站
  • 网站 案例互联网外包公司值得去吗
  • 做医疗护具网站浙江立鹏建设有限公司网站
  • 织梦制作手机网站c 网站开发需要学什么软件
  • 罗湖网站制作阿里巴巴开店网站怎么做
  • 深圳住房和建设局网站 招标怎样建设自己的视频网站
  • 网站建设的目的模板茶网站建设需要多少钱
  • 珠海市城乡住房建设局网站网站外链
  • 福田做网站需要多少钱做淘宝客网站性质
  • html网站怎么进入后台网站主题怎么写
  • wordpress怎么ftp建站高端网站建设域名注册
  • 我用织梦5.7做个网站应该把淘宝客店铺链接放到哪聊天软件开发需要多少钱
  • 站长工具爱站竞价单页网站制作
  • 网站分类目录大全购物网站大全棉鞋
  • 网站镜像做排名建立外贸英文网站应该怎么做
  • 上海做网站就用乐云seo手机网站cms 下载
  • 做网站需要固定ip么灵犀科技网站建设
  • 深圳高端做网站建设网站备案与不备案区别
  • 家居企业网站建设公司苏州高新区建设局网站管网
  • 体育门户网站模板seo网络推广有哪些