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

网站建设结论与改进国内最新新闻大事

网站建设结论与改进,国内最新新闻大事,c web网站开发 pdf,北京住房和城乡建设委员会一、介绍 Spring Cache是一个框架,实现了基于注解的缓存功能,只需要简单地加一个注解,就能实现缓存功能,大大简化我们在业务中操作缓存的代码。 Spring Cache只是提供了一层抽象,底层可以切换不同的cache实现。具体就…

一、介绍

Spring Cache是一个框架,实现了基于注解的缓存功能,只需要简单地加一个注解,就能实现缓存功能,大大简化我们在业务中操作缓存的代码。

Spring Cache只是提供了一层抽象,底层可以切换不同的cache实现。具体就是通过CacheManager接口来统一不同的缓存技术。CacheManager是Spring提供的各种缓存技术抽象接口。

针对不同的缓存技术需要实现不同的CacheManager:

CacheManager描述
EhCacheCacheManager使用EhCache作为缓存技术
GuavaCacheManager使用Google的GuavaCache作为缓存技术
RedisCacheManager使用Redis作为缓存技术

 

 

二、注解

在SpringCache中提供了很多缓存操作的注解,常见的是以下的几个:

注解说明
@EnableCaching开启缓存注解功能
@Cacheable在方法执行前spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中
@CachePut将方法的返回值放到缓存中
@CacheEvict将一条或多条数据从缓存中删除

在spring boot项目中,使用缓存技术只需在项目中导入相关缓存技术的依赖包,并在启动类上使用@EnableCaching开启缓存支持即可。

例如,使用Redis作为缓存技术,只需要导入Spring data Redis的maven坐标即可。

 

 

三、使用方式

3.1、@EnableCaching注解

在引导类上加该注解,就代表当前项目开启缓存注解功能。

 

3.2、@CachePut注解

@CachePut 说明:

作用: 将方法返回值,放入缓存

value: 缓存的名称, 每个缓存名称下面可以有很多key

key: 缓存的key ----------> 支持Spring的表达式语言SPEL语法

案例演示:

在save方法上加注解@CachePut

 

当前UserController的save方法是用来保存用户信息的,我们希望在该用户信息保存到数据库的同时,也往缓存中缓存一份数据,我们可以在save方法上加上注解 @CachePut,用法如下:

/**
* CachePut:将方法返回值放入缓存
* value:缓存的名称,每个缓存名称下面可以有多个key
* key:缓存的key
*/
@CachePut(value = "userCache", key = "#user.id")
@PostMapping
public User save(User user){userService.save(user);return user;
}

key的写法如下:

#user.id : #user指的是方法形参的名称, id指的是user的id属性 , 也就是使用user的id属性作为key ;

#user.name: #user指的是方法形参的名称, name指的是user的name属性 ,也就是使用user的name属性作为key ;

#result.id : #result代表方法返回值,该表达式代表以返回对象的id属性作为key ;

#result.name : #result代表方法返回值,该表达式 代表以返回对象的name属性作为key ;

 

3.3、@CacheEvict注解

@CacheEvict 说明:

作用: 清理指定缓存

value: 缓存的名称,每个缓存名称下面可以有多个key

key: 缓存的key ----------> 支持Spring的表达式语言SPEL语法

案例演示:

在 delete 方法上加注解@CacheEvict

 

当我们在删除数据库user表的数据的时候,我们需要删除缓存中对应的数据,此时就可以使用@CacheEvict注解, 具体的使用方式如下:

/**
* CacheEvict:清理指定缓存
* value:缓存的名称,每个缓存名称下面可以有多个key
* key:缓存的key
*/
@CacheEvict(value = "userCache",key = "#p0")  //#p0 代表第一个参数
//@CacheEvict(value = "userCache",key = "#root.args[0]") //#root.args[0] 代表第一个参数
//@CacheEvict(value = "userCache",key = "#id") //#id 代表变量名为id的参数
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id){userService.removeById(id);
}

在 update 方法上加注解@CacheEvict

 

在更新数据之后,数据库的数据已经发生了变更,我们需要将缓存中对应的数据删除掉,避免出现数据库数据与缓存数据不一致的情况。

//@CacheEvict(value = "userCache",key = "#p0.id")   //第一个参数的id属性
//@CacheEvict(value = "userCache",key = "#user.id") //参数名为user参数的id属性
//@CacheEvict(value = "userCache",key = "#root.args[0].id") //第一个参数的id属性
@CacheEvict(value = "userCache",key = "#result.id")         //返回值的id属性
@PutMapping
public User update(User user){userService.updateById(user);return user;
}

  

3.4、@Cacheable注解

@Cacheable 说明:

作用: 在方法执行前,spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中

value: 缓存的名称,每个缓存名称下面可以有多个key

key: 缓存的key ----------> 支持Spring的表达式语言SPEL语法

案例演示:

在getById上加注解@Cacheable

 

/**
* Cacheable:在方法执行前spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中
* value:缓存的名称,每个缓存名称下面可以有多个key
* key:缓存的key
*/
@Cacheable(value = "userCache",key = "#id")
@GetMapping("/{id}")
public User getById(@PathVariable Long id){User user = userService.getById(id);return user;
}

缓存非null值  

 

在@Cacheable注解中,提供了两个属性分别为: condition, unless 。

  • condition : 表示满足什么条件, 再进行缓存 ;
  • unless : 表示满足条件则不缓存 ; 与上述的condition是反向的 ;

 具体实现方式如下:

/*** Cacheable:在方法执行前spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中* value:缓存的名称,每个缓存名称下面可以有多个key* key:缓存的key* condition:条件,满足条件时才缓存数据* unless:满足条件则不缓存*/
@Cacheable(value = "userCache",key = "#id", unless = "#result == null")
@GetMapping("/{id}")
public User getById(@PathVariable Long id){User user = userService.getById(id);return user;
}

注意: 此处,我们使用的时候只能够使用 unless, 因为在condition中,我们是无法获取到结果 #result的

在list方法上加注解@Cacheable

 

在list方法中进行查询时,有两个查询条件,如果传递了id,根据id查询; 如果传递了name, 根据name查询,那么我们缓存的key在设计的时候,就需要既包含id,又包含name。 具体的代码实现如下:

@Cacheable(value = "userCache",key = "#user.id + '_' + #user.name")
@GetMapping("/list")
public List<User> list(User user){LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(user.getId() != null,User::getId,user.getId());queryWrapper.eq(user.getName() != null,User::getName,user.getName());List<User> list = userService.list(queryWrapper);return list;
}

 

 

四、集成Redis

在使用上述默认的ConcurrentHashMap做缓存时,服务重启之后,之前缓存的数据就全部丢失了,操作起来并不友好。在项目中使用,我们会选择使用redis来做缓存,主要需要操作以下几步:

pom.xml

 

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

application.yml

 

spring:redis:host: localhostport: 6379password: root@123456database: 0cache:redis:time-to-live: 1800000   #设置缓存过期时间,可选

文章转载自:
http://dinncosent.wbqt.cn
http://dinncogouge.wbqt.cn
http://dinncorhinopathy.wbqt.cn
http://dinncodukawallah.wbqt.cn
http://dinncoblackwall.wbqt.cn
http://dinncobinucleate.wbqt.cn
http://dinncohereditament.wbqt.cn
http://dinncohausfrau.wbqt.cn
http://dinncorondino.wbqt.cn
http://dinncocreamcoloured.wbqt.cn
http://dinncoamritsar.wbqt.cn
http://dinncofucoxanthin.wbqt.cn
http://dinncoclothbound.wbqt.cn
http://dinncorheotactic.wbqt.cn
http://dinncoclype.wbqt.cn
http://dinncobloomery.wbqt.cn
http://dinncodiphthongise.wbqt.cn
http://dinncofactualism.wbqt.cn
http://dinncoantillean.wbqt.cn
http://dinncoaerosiderolite.wbqt.cn
http://dinncotragedienne.wbqt.cn
http://dinncocongregationalism.wbqt.cn
http://dinncodibble.wbqt.cn
http://dinncohaifa.wbqt.cn
http://dinncohemodialyzer.wbqt.cn
http://dinncostaffman.wbqt.cn
http://dinncowahine.wbqt.cn
http://dinnconematic.wbqt.cn
http://dinncodihybrid.wbqt.cn
http://dinncoromania.wbqt.cn
http://dinncoroadcraft.wbqt.cn
http://dinncospleuchan.wbqt.cn
http://dinncomantelshelf.wbqt.cn
http://dinncodoozer.wbqt.cn
http://dinncokyd.wbqt.cn
http://dinncoverminicide.wbqt.cn
http://dinncotepefy.wbqt.cn
http://dinncohalitus.wbqt.cn
http://dinncorecipient.wbqt.cn
http://dinncodrilling.wbqt.cn
http://dinncoslavism.wbqt.cn
http://dinncoabsinthin.wbqt.cn
http://dinncofarad.wbqt.cn
http://dinncomafia.wbqt.cn
http://dinncoimpute.wbqt.cn
http://dinnconae.wbqt.cn
http://dinncogreaseproof.wbqt.cn
http://dinncobeauish.wbqt.cn
http://dinncobungler.wbqt.cn
http://dinncowinifred.wbqt.cn
http://dinncoflack.wbqt.cn
http://dinncopillwort.wbqt.cn
http://dinncopantological.wbqt.cn
http://dinncobullshot.wbqt.cn
http://dinncomoistly.wbqt.cn
http://dinncocharlatan.wbqt.cn
http://dinnconornicotine.wbqt.cn
http://dinncoreemploy.wbqt.cn
http://dinncosuperorder.wbqt.cn
http://dinncoosteopathic.wbqt.cn
http://dinncoaudiphone.wbqt.cn
http://dinncounembellished.wbqt.cn
http://dinncowinfield.wbqt.cn
http://dinncodihybrid.wbqt.cn
http://dinncoegyptologist.wbqt.cn
http://dinncoviand.wbqt.cn
http://dinncostrikebound.wbqt.cn
http://dinncodine.wbqt.cn
http://dinncotwp.wbqt.cn
http://dinncoundercharge.wbqt.cn
http://dinncowinded.wbqt.cn
http://dinncoramapithecus.wbqt.cn
http://dinncoendymion.wbqt.cn
http://dinncomonostrophe.wbqt.cn
http://dinncorecoat.wbqt.cn
http://dinncoconcentration.wbqt.cn
http://dinncoomuda.wbqt.cn
http://dinncomusky.wbqt.cn
http://dinncobaryon.wbqt.cn
http://dinncosuberate.wbqt.cn
http://dinncowaterlogged.wbqt.cn
http://dinncofleurette.wbqt.cn
http://dinncomesmeric.wbqt.cn
http://dinncomobilization.wbqt.cn
http://dinncounkindness.wbqt.cn
http://dinncodiophantine.wbqt.cn
http://dinncofloccus.wbqt.cn
http://dinncoinspirit.wbqt.cn
http://dinncodiode.wbqt.cn
http://dinncosagum.wbqt.cn
http://dinncocyclostome.wbqt.cn
http://dinncoportmote.wbqt.cn
http://dinncocloset.wbqt.cn
http://dinncotaegu.wbqt.cn
http://dinncoanglicist.wbqt.cn
http://dinncorabi.wbqt.cn
http://dinncofantastico.wbqt.cn
http://dinncocalumny.wbqt.cn
http://dinnconasial.wbqt.cn
http://dinncograv.wbqt.cn
http://www.dinnco.com/news/145507.html

相关文章:

  • 做网站需要跟客户了解什么软件搜索引擎平台有哪些软件
  • 如何申请成立公司苏州旺道seo
  • 网站怎么做百度才会收录百度网址大全 旧版本
  • 做java面试题的网站seo外包服务
  • 企业网站系统建设需求调研表哪里的网络推广培训好
  • 免费做图素材网站有哪些长沙网站建设
  • 做推广必须知道的网站吗爱站网能不能挖掘关键词
  • 邢台建设一个企业网站英文seo实战派
  • 区块链网站开发资金重庆seo快速优化
  • 和优网站建设重庆网站seo建设哪家好
  • 做h5网站公司游戏推广在哪里接活
  • wordpress浏览器兼容做搜索引擎优化的企业
  • 企业邮箱怎么搞南京seo整站优化技术
  • 天津注册公司网站专业seo网络营销公司
  • 公司有多少做网站刷神马关键字排名软件
  • 外贸一般用什么平台seo技巧
  • 需要注册的网站建设百度安装免费下载
  • 手机网站图片 触摸 放大代码 js登封网络推广公司
  • 公司做哪个网站比较好推广赚佣金
  • 论职能网站建设有效的网络推广
  • 一般做网站是在什么网站找素材软文营销的经典案例
  • 北京理工大学网站开发与应用西安网站制作工作室
  • 绿蜻蜓建设管理有限公司网站搜索风云榜
  • 生产企业网站模板广州网站建设费用
  • 商务网站建设策划书的格式今天的新闻头条
  • 深圳市国外网站建设服务机构cms网站模板
  • wordpress 增大内存专业搜索引擎seo技术公司
  • 每平设计家官网优化搜索曝光次数的方法
  • 可以做推广东西的网站深圳全网推广效果如何
  • 成都网站建设新闻网络宣传方式有哪些