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

肇庆市网站建设_网站建设公司_网站建设_seo优化

17做网站,高明网站设计服务,百度收录要多久,设计比较有特色的网站spring boot缓存缓存是大多数应用程序的主要组成部分#xff0c;只要我们设法避免磁盘访问#xff0c;缓存就会保持强劲。 Spring对各种配置的缓存提供了强大的支持 。 您可以根据需要简单地开始#xff0c;然后进行更多可定制的操作。 这将是spring提供的最简单的缓存形式… spring boot缓存 缓存是大多数应用程序的主要组成部分只要我们设法避免磁盘访问缓存就会保持强劲。 Spring对各种配置的缓存提供了强大的支持 。 您可以根据需要简单地开始然后进行更多可定制的操作。 这将是spring提供的最简单的缓存形式的示例。 Spring默认带有一个内存缓存它很容易设置。 让我们从gradle文件开始。 group com.gkatzioura version 1.0-SNAPSHOTbuildscript {repositories {mavenCentral()}dependencies {classpath(org.springframework.boot:spring-boot-gradle-plugin:1.4.2.RELEASE)} }apply plugin: java apply plugin: idea apply plugin: org.springframework.bootrepositories {mavenCentral() }sourceCompatibility 1.8 targetCompatibility 1.8dependencies {compile(org.springframework.boot:spring-boot-starter-web)compile(org.springframework.boot:spring-boot-starter-cache)compile(org.springframework.boot:spring-boot-starter)testCompile(junit:junit) }bootRun {systemProperty spring.profiles.active, simple-cache } 由于同一项目将用于不同的缓存提供程序因此会有多个spring配置文件。 本教程的Spring配置文件将是简单缓存因为我们将使用基于ConcurrentMap的缓存该缓存恰好是默认缓存。 我们将实现一个应用程序该应用程序将从本地文件系统中获取用户信息。 该信息应位于users.json文件中 [{userName:user1,firstName:User1,lastName:First},{userName:user2,firstName:User2,lastName:Second},{userName:user3,firstName:User3,lastName:Third},{userName:user4,firstName:User4,lastName:Fourth} ] 我们还将为要检索的数据指定一个简单的模型。 package com.gkatzioura.caching.model;/*** Created by gkatzioura on 1/5/17.*/ public class UserPayload {private String userName;private String firstName;private String lastName;public String getUserName() {return userName;}public void setUserName(String userName) {this.userName userName;}public String getFirstName() {return firstName;}public void setFirstName(String firstName) {this.firstName firstName;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName lastName;} } 然后我们将添加一个将读取信息的bean。 package com.gkatzioura.caching.config;import com.fasterxml.jackson.databind.ObjectMapper; import com.gkatzioura.caching.model.UserPayload; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.core.io.Resource;import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.Collections; import java.util.List;/*** Created by gkatzioura on 1/5/17.*/ Configuration Profile(simple-cache) public class SimpleDataConfig {Autowiredprivate ObjectMapper objectMapper;Value(classpath:/users.json)private Resource usersJsonResource;Beanpublic ListUserPayload payloadUsers() throws IOException {try(InputStream inputStream usersJsonResource.getInputStream()) {UserPayload[] payloadUsers objectMapper.readValue(inputStream,UserPayload[].class);return Collections.unmodifiableList(Arrays.asList(payloadUsers));}} } 显然为了访问信息我们将使用实例化的Bean包含所有用户信息。 下一步将是创建一个存储库接口以指定将要使用的方法。 package com.gkatzioura.caching.repository;import com.gkatzioura.caching.model.UserPayload;import java.util.List;/*** Created by gkatzioura on 1/6/17.*/ public interface UserRepository {ListUserPayload fetchAllUsers();UserPayload firstUser();UserPayload userByFirstNameAndLastName(String firstName,String lastName);} 现在让我们深入研究将包含所需缓存注释的实现。 package com.gkatzioura.caching.repository;import com.gkatzioura.caching.model.UserPayload; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Repository;import java.util.List; import java.util.Optional;/*** Created by gkatzioura on 12/30/16.*/ Repository Profile(simple-cache) public class UserRepositoryLocal implements UserRepository {Autowiredprivate ListUserPayload payloadUsers;private static final Logger LOGGER LoggerFactory.getLogger(UserRepositoryLocal.class);OverrideCacheable(alluserscache)public ListUserPayload fetchAllUsers() {LOGGER.info(Fetching all users);return payloadUsers;}OverrideCacheable(cacheNames usercache,key #root.methodName)public UserPayload firstUser() {LOGGER.info(fetching firstUser);return payloadUsers.get(0);}OverrideCacheable(cacheNames usercache,key {#firstName,#lastName})public UserPayload userByFirstNameAndLastName(String firstName,String lastName) {LOGGER.info(fetching user by firstname and lastname);OptionalUserPayload user payloadUsers.stream().filter(p- p.getFirstName().equals(firstName)p.getLastName().equals(lastName)).findFirst();if(user.isPresent()) {return user.get();} else {return null;}}} 包含Cacheable的方法将触发缓存填充而包含CacheEvict的方法将触发缓存逐出。 通过使用Cacheable而不是仅指定将存储我们的值的缓存映射我们还可以基于方法名称或方法参数来指定键。 因此我们实现了方法缓存。 例如方法firstUser使用方法名称作为键而方法userByFirstNameAndLastName使用方法参数以创建键。 带有CacheEvict批注的两种方法将清空指定的缓存。 LocalCacheEvict将是处理驱逐的组件。 package com.gkatzioura.caching.repository;import org.springframework.cache.annotation.CacheEvict; import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component;/*** Created by gkatzioura on 1/7/17.*/ Component Profile(simple-cache) public class LocalCacheEvict {CacheEvict(cacheNames alluserscache,allEntries true)public void evictAllUsersCache() {}CacheEvict(cacheNames usercache,allEntries true)public void evictUserCache() {}} 由于我们使用非常简单的缓存形式因此不支持驱逐ttl。 因此我们将仅针对此特定情况添加一个调度程序该调度程序将在一定时间段后退出缓存。 package com.gkatzioura.caching.scheduler;import com.gkatzioura.caching.repository.LocalCacheEvict; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Profile; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;/*** Created by gkatzioura on 1/7/17.*/ Component Profile(simple-cache) public class EvictScheduler {Autowiredprivate LocalCacheEvict localCacheEvict;private static final Logger LOGGER LoggerFactory.getLogger(EvictScheduler.class);Scheduled(fixedDelay10000)public void clearCaches() {LOGGER.info(Invalidating caches);localCacheEvict.evictUserCache();localCacheEvict.evictAllUsersCache();}} 最后我们将使用控制器来调用指定的方法 package com.gkatzioura.caching.controller;import com.gkatzioura.caching.model.UserPayload; import com.gkatzioura.caching.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;import java.util.List;/*** Created by gkatzioura on 12/30/16.*/ RestController public class UsersController {Autowiredprivate UserRepository userRepository;RequestMapping(path /users/all,method RequestMethod.GET)public ListUserPayload fetchUsers() {return userRepository.fetchAllUsers();}RequestMapping(path /users/first,method RequestMethod.GET)public UserPayload fetchFirst() {return userRepository.firstUser();}RequestMapping(path /users/,method RequestMethod.GET)public UserPayload findByFirstNameLastName(String firstName,String lastName ) {return userRepository.userByFirstNameAndLastName(firstName,lastName);}} 最后但并非最不重要的一点是我们的Application类应包含两个额外的注释。 为了启用调度程序需要EnableScheduling为了启用缓存需要EnableCaching package com.gkatzioura.caching;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; import org.springframework.scheduling.annotation.EnableScheduling;/*** Created by gkatzioura on 12/30/16.*/ SpringBootApplication EnableScheduling EnableCaching public class Application {public static void main(String[] args) {SpringApplication.run(Application.class,args);}} 您可以在github上找到源代码。 翻译自: https://www.javacodegeeks.com/2017/01/spring-boot-cache-abstraction.htmlspring boot缓存
http://www.ihoyoo.com/news/87789.html

相关文章:

  • 阿里云做的网站程序员自己做网站吗
  • 做的阿里巴巴网站的放哪个科目珠海十大网站建设公司
  • 新浪网站制作济宁建站公司
  • 做律师事务所网站云南网站建设天锐科技
  • 网站设计好学吗销售人员培训课程有哪些
  • 如何做新网站保留域名如何寻找客户
  • 做广告公司网站建设驻马店做网站
  • 深圳珠宝网站建设临沧建设局网站
  • 买网站不给我备案包装设计怎么做
  • 响应式网站建设开发公司页面效果好的网站
  • 自助网站建设哪个好电商网站 设计方案
  • 做资源网站盈利点长春网站建设于健
  • 企业做网站有哪些好处如何做彩票网站信息
  • 韩顺平 开源网站专做畜牧招聘网站的
  • 自己做的网站能放到织梦上wordpress 件康
  • 临海市城乡建设规划局网站广州动画制作公司
  • 行业门户网站的优化怎么做yps行业门户系统新泰网络有限公司
  • 架设网站是自己架设服务器还是租服务器建站网站破解版
  • 有域名如何自己制作网站广东腾越建筑工程有限公司
  • 淘大象排名查询seo排名的影响因素有哪些
  • 网站色调代号建英文产品网站
  • 高端设计网站平台自己建一个网站需要多少钱?
  • 网站效果图怎么做自豪的wordpress
  • 房产网站流量排名网站应当实现那些功能 流程如何设计
  • 微机做网站的软件wordpress mysql 配置文件
  • 站内推广的方法wordpress setup-config.php空白
  • 向谷歌提交网站做图书馆网站模板
  • 网站建设咨询中心曲靖做网站公司
  • 如何做网站收录python 做网站开发
  • 鹤壁 网站建设给我一个网站图片