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

做室内设计特别好的网站/百度销售平台怎样联系

做室内设计特别好的网站,百度销售平台怎样联系,广告设计网站都有哪些,大型网站快速排名程序性能的瓶颈之一我们知道是数据库。而内存的速度是远远大于数据库的速度的。如果我们需要重复的获取相同的数据的时候,我们就需要一次又一次的请求数据或者远程服务。导致大量的时间耗费在数据库查询或者远程方法调用上。因此,我们可以理由缓存来提升…

程序性能的瓶颈之一我们知道是数据库。而内存的速度是远远大于数据库的速度的。如果我们需要重复的获取相同的数据的时候,我们就需要一次又一次的请求数据或者远程服务。导致大量的时间耗费在数据库查询或者远程方法调用上。因此,我们可以理由缓存来提升我们程序的性能。

Spring的缓存支持

Spring 定义了org.springframework.cache.CacheManager和org.springframework.cache.Cache接口来统一不同的缓存技术。其中CacheManager是Spring提供的各种缓存技术抽象接口。Cache接口包含缓存的各种操作(增加、删除、获得缓存,我们一般不会直接与这个接口打交道)。

Spring支持的CacheManager

  • SimpleCacheManager,使用了简单的Collection来存储缓存,主要用于测试
  • ConcurrentMapCacheManager,使用ConcurrentMap来存储缓存
  • NoOpCacheManager,仅用于测试,不会实际存储缓存
  • EnCacheCacheManager,使用EnCache作为缓存技术
  • GuavaCacheManager,使用Google Guava的GuavaCache作为缓存技术
  • HazelcastCacheManager,使用Hazelcast作为缓存技术
  • JCacheCacheManager,支持Jcache(JSR-107)标准的实现作为缓存技术,如Appache Commons JCS
  • RedisCacheManager,使用Redis作为缓存技术

在我们实现任意一个实现了CacheManager的时候,需要注册实现CacheManager的Bean。例如

    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"><property name="caches"><set><bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"><property name="name" value="default"/></bean><bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"><property name="name" value="accountCache"/></bean></set></property></bean>

当然每一个缓存技术可能都需要额外的配置,但配置cacheManager是必不可少的。

声明式缓存注解

Spring提供了四个注解来声明缓存规则:
* @Cacheable,在方法执行前,Spring先去查看缓存中是否有数据。如果有数据,则直接返回远程数据,若没有数据,调用方法并将方法返回值放进缓存
* @CachePut,无论怎样,都会将方法的返回值放进缓存中,@CachePut和@Cacheable的属性一致
* @CacheEvict,将一条或者多条数据从缓存中删除
* Caching,可以通过@Caching注解组合多个注解策略在同一个方法上

@Cacheable,@CachePut,@CacheEvict都有Value属性,指定的是要使用的缓存名称,key属性指定的是数据在缓存中存储的键。

开启声明式缓存支持

开启声明式缓存支持非常简单,只需在缓存类中加入@EnableCaching注解即可

SpringBoot的缓存支持

在Spring中使用缓存的关键是配置CacheManager,而SpringBoot为我们配置了多个CacheManager的实现。即上述提到的EnCacheManager等实现例如EnCacheCacheConfiguration、RedisCacheConfiguration(使用Redis),在不做任何额外配置的情况下。默认使用的是SimpleCacheConfiguration。即使用 ConcurrentMapCacheManager。SpringBoot支持以spring.cache为前缀来在属性文件中配置属性。例如:

spring.cache.ehcache.config=#

在SpringBoot环境下使用缓存技术只需在项目中导入相关缓存技术的依赖包,并在配置类中使用EnableCaching开启缓存即可。

SpringBoot缓存实战

做好前期的准备

Person类

@Entity
public class Person {@Id@GeneratedValueprivate Long id;private String name;private Integer age;private String address;public Person() {super();}public Person(Long id, String name, Integer age, String address) {super();this.id = id;this.name = name;this.age = age;this.address = address;}public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}}

Dao层的Repository

public interface PersonRepository extends JpaRepository<Person,Long> {
}

Service层以及他的实现类
DemoService

public interface DemoService {public  Person save(Person person);public  void remove(Long id);public  Person findOne(Person person);
}

DemoServiceImpl

@Service
public class DemoServiceImpl implements  DemoService {@AutowiredPersonRepository personRepository;@Override@CachePut(value = "people",key = "#person.id")public Person save(Person person) {Person p = personRepository.save(person);System.out.println("为id,Key为"+p.getId()+"的数据做了缓存");return p;}@Override@CacheEvict(value = "people")public void remove(Long id) {System.out.println("删除了id,Key为"+id+"的数据缓存");personRepository.delete(id);}@Override@Cacheable(value = "people",key = "#person.id")public Person findOne(Person person) {Person p = personRepository.findOne(person.getId());System.out.println("为id,Key为"+p.getId()+"的数据做了缓存");return p;}
}

@CachePut缓存新增的或者更新的数据到缓存,其中缓存名称为people,数据的key是person 的id
@CacheEvict从缓存中删除key 为id的数据
@Cacheable缓存key为person的id数据到people中。
这里特别说明下。如果没有指定key。则方法参数作为key保存到缓存中

Controller层

@RestController
public class CacheController {@AutowiredDemoService demoService;@RequestMapping("/put")public Person put(Person person){return  demoService.save(person);}@RequestMapping("/able")public Person cacheable(Person person){return  demoService.findOne(person);}@RequestMapping("/evict")public String evict(Long id){demoService.remove(id);return  "OK";}
}

在SpringBoot的入口文件中开启缓存支持

@SpringBootApplication
@EnableCaching
public class SpringbootCacheApplication {public static void main(String[] args) {SpringApplication.run(SpringbootCacheApplication.class, args);}
}

properties配置文件

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456#1
spring.jpa.hibernate.ddl-auto=update#2
spring.jpa.show-sql=true#3
spring.jackson.serialization.indent_output=true

运行

当我们对数据做缓存之后,数据的获得将从缓存中得到而不是从数据库中得到
数据库初始情况如图
这里写图片描述

测试Cacheable

第一次访问http://localhost:8080/able?id=1的时候,将调用方法 查询数据库。并将数据放到缓存中此时控制台输出这里写图片描述
同时网页显示这里写图片描述

再次访问http://localhost:8080/able?id=1。此时控制台没有任何输出。表示没有调用这个方法。页面直接从缓存中获取数据。

测试CachePut

访问http://localhost:8080/put?name=zzzz&age=23&address=shanghai。此时可见控制台输出如图
这里写图片描述

页面输出
这里写图片描述

再次访问http://localhost:8080/put?name=zzzz&age=23&address=shanghai。控制台无输出,从缓存直接获取数据。界面与上图相同

测试CacheEvict

访问http://localhost:8080/able?id=1。为id为1的数据做缓存,再次访问http://localhost:8080/able?id=1。确认数据已经是从缓存中获取。访问http://localhost:8080/evict?id=1会看到控制台打印如下信息。
这里写图片描述

切换缓存技术

切换缓存技术只需在pom.xml中引入相关的依赖即可。当然如果需要进一步配置则需要进行一定的配置

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-redis</artifactId></dependency>

代码文件
参考书籍JavaEE的颠覆者–SpringBoot实战

http://www.lbrq.cn/news/1347643.html

相关文章:

  • 做网站价格多少钱/什么是整合营销概念
  • 网站做app有什么意义/哈尔滨优化网站公司
  • 网站建设制作收费/宁波seo网络推广咨询价格
  • 网站开发建设公司/汕头seo收费
  • 4.1进行网站建设与推广/黑帽seo是什么意思
  • wordpress隐藏自定义/广州seo公司官网
  • 网站建设哪里好/搜索引擎提交入口网址
  • 浙江建设继续教育学院网站/建设网站制作公司
  • 制作简历的免费网站/微信软文推广怎么做
  • 网站图标按钮用什么做/运营怎么做
  • wordpress 页面制作/南京seo公司排名
  • 东莞网站建设做网站/建站系统有哪些
  • 谁需要做网站的嘉兴/百度seo关键词优化公司
  • 娱乐网站建设ppt模板/惠州seo外包
  • 哪个网站可以做微商/成都网站快速开发
  • 做区位分析的地图网站/seo优化主要做什么
  • 哪个网站可以免费看小说不收费/百度卖货平台
  • 关于行业网站建设意见/网络推广外包加手机蛙软件
  • 新疆网络直播课空中课堂/南京seo全网营销
  • 怎么设置自己做的网站吗/如何建立电商平台
  • 许昌做网站公司报价/哈尔滨seo
  • 江山建设工程信息网站/友情链接怎么互换
  • 汕头网站优化/新闻 近期大事件
  • 惠州网站建设 翻译6/2023年8月疫情恢复
  • 有没有做cad单的网站/百度站点
  • 昌平住房和城乡建设委员会网站/怎么宣传网站
  • 深圳有做网站的吗/创建网站需要多少资金
  • 政府网站建设改版通知/seo外链增加
  • 排名优化是什么意思/北京seo运营
  • 怎么让google收录网站/常用的营销方法和手段
  • 5 种简单方法将 Safari 书签转移到新 iPhone
  • NCD57080CDR2G 安森美onsemi 通用驱动器, SOIC, 8针, 20V电源, 8 A输出NCD57080CDR2电流隔离式栅极驱动器
  • SpringBoot 3.x整合Elasticsearch:从零搭建高性能搜索服务
  • 【springcloud的配置文件不生效】
  • C语言线程同步详解(互斥锁、信号量、条件变量和读写锁)
  • 【机器学习深度学习】模型压缩简介