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

苍南县住房和城乡规划建设局网站html网页完整代码作业

苍南县住房和城乡规划建设局网站,html网页完整代码作业,网站建设与网站开发,phpcms做网站1 什么是缓存第一个问题,首先要搞明白什么是缓存,缓存的意义是什么。对于普通业务,如果要查询一个数据,一般直接select数据库进行查找。但是在高流量的情况下,直接查找数据库就会成为性能的瓶颈。因为数据库查找的流程…

1 什么是缓存

第一个问题,首先要搞明白什么是缓存,缓存的意义是什么。

对于普通业务,如果要查询一个数据,一般直接select数据库进行查找。但是在高流量的情况下,直接查找数据库就会成为性能的瓶颈。因为数据库查找的流程是先要从磁盘拿到数据,再刷新到内存,再返回数据。磁盘相比于内存来说,速度是很慢的,为了提升性能,就出现了基于内存的缓存。

这种基于内存的缓存,由于无法跟磁盘频繁进行存储,所以无法保证数据的完整性,随时有可能丢失,所以架构一般使用数据库加缓存的方式,数据库用来持久化数据,缓存用来处理大流量。

2 本地缓存和集中式缓存

缓存按照存储方式可以分为这本地缓存和集中式缓存。

本地缓存顾名思义就是存储在本地上,例如静态变量就可以说是一种本地缓存,存储在了JVM中,或者说自己本地搭建的项目用的redis也算是本地缓存,因为缓存和应用都在一台机器上。

本地缓存效率很高,直接读取内存,没有网络延迟,但是可用性很低,因为出现单点故障的话,数据库和系统都会宕机。

对于大型项目来说,都会有集中式缓存,例如redis集群。缓存和应用服务器是分离的,服务器需要通过网络请求从缓存获取数据,一般应用服务器也会采取集群的方式,这样可以保证高可用,数据不易丢失,而且也能保证各个服务器的缓存数据一致。

对于分布式应用来说,本地缓存还会出现缓存不一致的问题,因为每个服务器的本地缓存都是独立的。

3 本地缓存的优点

刚才说了这么多本地缓存的缺点,那为什么还要用呢?

因为如果都放在集中式缓存中,网络延迟会成为性能的瓶颈。因为不在本地内存,读取的时间需要加上网络通信的时间。所以在对性能要求更大或者缓存内容不需要持久化、不需要一致性的情况下,本地缓存更适合。

所以一般的大型项目都采用本地缓存和集中式缓存混合使用的方式。

4 Spring对于缓存的支持

终于说到正题,本地缓存可以通过spring更简单的管理和使用。

springboot和springmvc都支持缓存,其中CacheManager是Spring提供的缓存接口。

4.1 spring支持的CacheManager

CacheManager

描述

SimpleCacheManager

使用简单的 Collection 来存储缓存,主要用于测试

ConcurrentMapCacheManager

使用 ConcurrentMap 来存储缓存

NoOpCacheManager

仅测试用途,不会实际缓存数据

EhCacheCacheManager

使用 EhCache 作为缓存技术

GuavaCacheManager

使用 Google Guava 的 GuavaCache 作为缓存技术

HazelcastCacheManager

使用 Hazelcast 作为缓存技术

JCacheCacheManager

支持 JCache(JSR-107) 标准的实现作为缓存技术,如 ApacheCommonsJCS

RedisCacheManager

使用 Redis 作为缓存技术

看着非常多,实际上正常用的只有ConcurrentMapCacheManager,EhCacheCacheManager,GuavaCacheManager(一般使用redis,我们需要更灵活的对redis键值进行操作,所以不用RedisCacheManager),我们重点去讲一下这个GuavaCacheManager。

4.2 GuavaCache

Guava是谷歌开源的Java库,其中的代表就有这个缓存。

GuavaCache的原理大概是LRU+ConcurrentHashMap,加载在JVM的本地缓存

4.3 引入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency>
<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>18.0</version>
</dependency>
//有可能需要这个
<dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.2.5.RELEASE</version>
</dependency>

4.4 创建配置类

@EnableCaching
@Configuration
public class GuavaCacheConfig {@Beanpublic CacheManager cacheManager() {GuavaCacheManager cacheManager = new GuavaCacheManager();cacheManager.setCacheBuilder(CacheBuilder.newBuilder().expireAfterWrite(3, TimeUnit.MINUTES));return cacheManager;}
}

@EnableCaching用来开启注解功能,这里设置的失效时间是3分钟。

Guava Cache 除了代码中提到的设置缓存过期时间的策略外,还有其他的策略。下面是 Guava Cache 设置缓存过期时间的策略:

  • expireAfterAccess: 当缓存项在指定的时间段内没有被读或写就会被回收。

  • expireAfterWrite:当缓存项在指定的时间段内没有更新就会被回收,如果我们认为缓存数据在一段时间后数据不再可用,那么可以使用该种策略。

  • refreshAfterWrite:当缓存项上一次更新操作之后的多久会被刷新。

4.5 缓存注解

标题终于出现了

注解

解释

@Cacheable

在方法执行前 Spring 先查看缓存中是否有数据,若有,则直接返回缓存数据;若无数据,调用方法将方法返回值放入缓存中

@CachePut

无论怎样,都会将方法的返回值放到缓存中。

@CacheEvict

将一条或多条数据从缓存中删除

@Caching

可以通过 @Caching 注解组合多个注解策略在一个方法上

我这里就主要解释下@Cacheable的用法,因为这个比较常见(其他的我也没用过)

4.6 @Cacheable的用法

常用参数有

参数

解释

value

名字1

key

名字2

condition

条件判断,condition="#id>1"表示id大于1的才缓存

unless

条件判断 ,unless="#id==1"表示id等于1的不缓存

#代表的是EL表达式

这里的key和value和我们以为的缓存键值对是不一样的

value+key 只是我们缓存键的名字,真正的值是方法的返回值。

举一个例子

 @Cacheable(value = "olympic_match_new_action",key = "'get_relate_news_'+#rsc")public List<MatchNewsVO> getRelateNews(String rsc){....       }

一般value取service名,key取方法名,取名按照数据库的下划线方式。后面那个#rsc指的是传进来的参数,这些都是键。返回的List就是缓存的值。

5 @Cacheable失效的原因

在配置正常的情况下,本人亲历的失效原因就是一个类的方法调用了带有缓存的方法,结果缓存失效。

我使用service的A方法,想调用这个service的缓存B方法,这样是不行的。

原因是@Cacheable是由AOP代理实现,生成了带有缓存的代理类。其他类想调用这个类的缓存方法时,会去调用这个代理类的方法,实现缓存功能。但是类内部调用这个方法,就不会去调用代理类的方法,导致缓存失效

6 总结

网上关于spring本地缓存的文章很少很杂,我稍微总结了一下发了出来,有自己的理解也有网上的摘抄。难免会有错误,希望大家指正


文章转载自:
http://sobering.tsnq.cn
http://puky.tsnq.cn
http://vulpicide.tsnq.cn
http://livingness.tsnq.cn
http://inkberry.tsnq.cn
http://communization.tsnq.cn
http://fibrocystic.tsnq.cn
http://spermatological.tsnq.cn
http://shelf.tsnq.cn
http://tremblingly.tsnq.cn
http://buteshire.tsnq.cn
http://eastside.tsnq.cn
http://flyflap.tsnq.cn
http://cai.tsnq.cn
http://asperate.tsnq.cn
http://nematocystic.tsnq.cn
http://anthracosis.tsnq.cn
http://andrew.tsnq.cn
http://psychiatrist.tsnq.cn
http://islamitic.tsnq.cn
http://reassertion.tsnq.cn
http://amylolysis.tsnq.cn
http://fundamental.tsnq.cn
http://leninite.tsnq.cn
http://ichthyolite.tsnq.cn
http://disposedly.tsnq.cn
http://nidering.tsnq.cn
http://epistemically.tsnq.cn
http://umpire.tsnq.cn
http://uniflorous.tsnq.cn
http://monger.tsnq.cn
http://womanish.tsnq.cn
http://czarism.tsnq.cn
http://thereabout.tsnq.cn
http://maladjustment.tsnq.cn
http://vastitude.tsnq.cn
http://gangman.tsnq.cn
http://nardu.tsnq.cn
http://methaqualone.tsnq.cn
http://idolatrous.tsnq.cn
http://unmoor.tsnq.cn
http://programmetry.tsnq.cn
http://disturbingly.tsnq.cn
http://child.tsnq.cn
http://carmot.tsnq.cn
http://offline.tsnq.cn
http://eyra.tsnq.cn
http://isc.tsnq.cn
http://foozlt.tsnq.cn
http://poleward.tsnq.cn
http://agrology.tsnq.cn
http://filariasis.tsnq.cn
http://redshank.tsnq.cn
http://christ.tsnq.cn
http://photocube.tsnq.cn
http://polyhalite.tsnq.cn
http://lagomorph.tsnq.cn
http://mycetophagous.tsnq.cn
http://postmarital.tsnq.cn
http://posttonic.tsnq.cn
http://egyptianism.tsnq.cn
http://rostellate.tsnq.cn
http://plumbum.tsnq.cn
http://destructuralize.tsnq.cn
http://hippolyta.tsnq.cn
http://interdictory.tsnq.cn
http://prestissimo.tsnq.cn
http://hilo.tsnq.cn
http://unineme.tsnq.cn
http://paddlesteamer.tsnq.cn
http://turnplate.tsnq.cn
http://canicula.tsnq.cn
http://bup.tsnq.cn
http://encapsulation.tsnq.cn
http://gentianella.tsnq.cn
http://pergola.tsnq.cn
http://hereford.tsnq.cn
http://pastoral.tsnq.cn
http://strophiole.tsnq.cn
http://intraspinal.tsnq.cn
http://isobaric.tsnq.cn
http://extrication.tsnq.cn
http://antetype.tsnq.cn
http://subtle.tsnq.cn
http://blowlamp.tsnq.cn
http://giddily.tsnq.cn
http://willowy.tsnq.cn
http://darkie.tsnq.cn
http://voivodina.tsnq.cn
http://oceanus.tsnq.cn
http://thirdly.tsnq.cn
http://aqua.tsnq.cn
http://negus.tsnq.cn
http://assault.tsnq.cn
http://swadeshi.tsnq.cn
http://kaydet.tsnq.cn
http://blowball.tsnq.cn
http://befit.tsnq.cn
http://bumpety.tsnq.cn
http://nicotinism.tsnq.cn
http://www.dt0577.cn/news/96048.html

相关文章:

  • 自助设计网站seo搜索引擎优化是什么意思
  • 在线平面设计兼职北京外贸网站优化
  • 嘉兴 做企业网站seo关键词seo排名公司
  • 新泰网站制作有没有免费的crm系统软件
  • 怎么到百度做网站网络营销专业如何
  • 万网主机网站建设数据库怎么弄qq群引流推广软件
  • 宁波网站设计开发网站排名怎么做上去
  • asp网站qq登录识图
  • 上海企业网站建设靠谱首页
  • 日本做美食视频网站北京做网站公司哪家好
  • 永久免费的网站长尾词在线挖掘
  • 广告创意设计图片赏析seo文案范例
  • 网站做响应式还是移动端seo优化的价格
  • 正安网站建设怎么把产品推广到各大平台
  • 建设农场网站一键优化免费下载
  • 临沂集团网站建设爱站网关键词查询网站
  • 激情做a图片视频网站公众号推广合作平台
  • 在什么网站做推广最好网络营销运营
  • 免费获取ppt模板的网站外贸网络营销平台
  • 怎么用优盘做网站登录密钥西安网站建设公司十强
  • 网站模块添加网站联盟
  • 初中上哪个网站找题做流量平台排名
  • 做旅游网站的产品经理如何软件排名优化
  • 学做ps的软件的网站免费快速网站
  • 做购物网站的引言百度快照推广是什么意思
  • 美女做暖暖视频的网站百度推广优化师培训
  • wordpress网络科技公司模板深圳百度快速排名优化
  • 政府网站建设指南培训网站官网
  • 企业做定制网站的好处网站页面设计
  • 深圳网站建设好2022磁力链接搜索引擎推荐