Spring思维导图,让Spring不再难懂(cache篇)

转载自 Spring思维导图,让Spring不再难懂(cache篇)

关于缓存

缓存是实际工作中非常常用的一种提高性能的方法。而在java中,所谓缓存,就是将程序或系统经常要调用的对象存在内存中,再次调用时可以快速从内存中获取对象,不必再去创建新的重复的实例。这样做可以减少系统开销,提高系统效率。

在增删改查中,数据库查询占据了数据库操作的80%以上,而非常频繁的磁盘I/O读取操作,会导致数据库性能极度低下。而数据库的重要性就不言而喻了:

  • 数据库通常是企业应用系统最核心的部分
  • 数据库保存的数据量通常非常庞大
  • 数据库查询操作通常很频繁,有时还很复杂

在系统架构的不同层级之间,为了加快访问速度,都可以存在缓存

缓存不同层级的作用.png

spring cache特性与缺憾

现在市场上主流的缓存框架有ehcache、redis、memcached。spring cache可以通过简单的配置就可以搭配使用起来。其中使用注解方式是最简单的。

特性与缺憾.png

Cache注解

缓存注解.png

从以上的注解中可以看出,虽然使用注解的确方便,但是缺少灵活的缓存策略,

缓存策略:

  • TTL(Time To Live ) 存活期,即从缓存中创建时间点开始直到它到期的一个时间段(不管在这个时间段内有没有访问都将过期)

  • TTI(Time To Idle) 空闲期,即一个数据多久没被访问将从缓存中移除的时间

项目中可能有很多缓存的TTL不相同,这时候就需要编码式使用编写缓存。

条件缓存

根据运行流程,如下@Cacheable将在执行方法之前( #result还拿不到返回值)判断condition,如果返回true,则查缓存; 

@Cacheable(value = "user", key = "#id", condition = "#id lt 10")  
public User conditionFindById(final Long id)  

如下@CachePut将在执行完方法后(#result就能拿到返回值了)判断condition,如果返回true,则放入缓存

@CachePut(value = "user", key = "#id", condition = "#result.username ne 'zhang'")  
public User conditionSave(final User user)   

  如下@CachePut将在执行完方法后(#result就能拿到返回值了)判断unless,如果返回false,则放入缓存;(即跟condition相反)

@CachePut(value = "user", key = "#user.id", unless = "#result.username eq 'zhang'")  
public User conditionSave2(final User user)   

  如下@CacheEvict, beforeInvocation=false表示在方法执行之后调用(#result能拿到返回值了);且判断condition,如果返回true,则移除缓存;

@CacheEvict(value = "user", key = "#user.id", beforeInvocation = false, condition = "#result.username ne 'zhang'"public User conditionDelete(final User user)   
  • 小试牛刀,综合运用:
    @CachePut(value = "user", key = "#user.id")public User save(User user) {users.add(user);return user;}@CachePut(value = "user", key = "#user.id")public User update(User user) {users.remove(user);users.add(user);return user;}@CacheEvict(value = "user", key = "#user.id")public User delete(User user) {users.remove(user);return user;}@CacheEvict(value = "user", allEntries = true)public void deleteAll() {users.clear();}@Cacheable(value = "user", key = "#id")public User findById(final Long id) {System.out.println("cache miss, invoke find by id, id:" + id);for (User user : users) {if (user.getId().equals(id)) {return user;}}return null;}

配置ehcache与redis

  • spring cache集成ehcache,spring-ehcache.xml主要内容:
<dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache-core</artifactId><version>${ehcache.version}</version>
</dependency>
<!-- Spring提供的基于的Ehcache实现的缓存管理器 --><!-- 如果有多个ehcacheManager要在bean加上p:shared="true" -->
<bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><property name="configLocation" value="classpath:xml/ehcache.xml"/>
</bean><bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"><property name="cacheManager" ref="ehcacheManager"/><property name="transactionAware" value="true"/>
</bean><!-- cache注解,和spring-redis.xml中的只能使用一个 -->
<cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true"/>
  • spring cache集成redis,spring-redis.xml主要内容:
<dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.8.1.RELEASE</version>
</dependency>
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>2.4.2</version>
</dependency>
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.9.0</version>
</dependency>
<!-- 注意需要添加Spring Data Redis等jar包 -->
<description>redis配置</description><bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxIdle" value="${redis.pool.maxIdle}"/><property name="maxTotal" value="${redis.pool.maxActive}"/><property name="maxWaitMillis" value="${redis.pool.maxWait}"/><property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/><property name="testOnReturn" value="${redis.pool.testOnReturn}"/>
</bean><!-- JedisConnectionFactory -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"><property name="hostName" value="${redis.master.ip}"/><property name="port" value="${redis.master.port}"/><property name="poolConfig" ref="jedisPoolConfig"/>
</bean><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"p:connectionFactory-ref="jedisConnectionFactory"><property name="keySerializer"><bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean></property><property name="valueSerializer"><bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/></property><property name="hashKeySerializer"><bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/></property><property name="hashValueSerializer"><bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/></property>
</bean><!--spring cache-->
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"c:redisOperations-ref="redisTemplate"><!-- 默认缓存10分钟 --><property name="defaultExpiration" value="600"/><property name="usePrefix" value="true"/><!-- cacheName 缓存超时配置,半小时,一小时,一天 --><property name="expires"><map key-type="java.lang.String" value-type="java.lang.Long"><entry key="halfHour" value="1800"/><entry key="hour" value="3600"/><entry key="oneDay" value="86400"/><!-- shiro cache keys --><entry key="authorizationCache" value="1800"/><entry key="authenticationCache" value="1800"/><entry key="activeSessionCache" value="1800"/></map></property>
</bean>
<!-- cache注解,和spring-ehcache.xml中的只能使用一个 -->
<cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true"/>

项目中注解缓存只能配置一个,所以可以通过以下引入哪个配置文件来决定使用哪个缓存。

<import resource="classpath:spring/spring-ehcache.xml"/>
<!-- <import resource="classpath:spring/spring-redis.xml"/>-->

当然,可以通过其他配置搭配使用两个缓存机制。比如ecache做一级缓存,redis做二级缓存。

缓存对比.png

更加详细的使用与配置,可以参考项目中spring-shiro-training中有关spring cache的配置。

  • https://git.oschina.net/wangzhixuan/spring-shiro-training.git

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/331122.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

intro to JNDI

【0】README 1&#xff09;本文转自 http://sishuok.com/forum/blogPost/list/1186.html 【1】原文如下 1&#xff09;JNDI 是什么&#xff1f; JNDI是 Java 命名与目录接口&#xff08;Java Naming and Directory Interface&#xff09;&#xff0c;在J2EE规范中是重要的规范之…

Mybatis3配置方法

一.准备 1.三个Jar包&#xff0c; 1.mybatis-3.2.1.jar 2.mysql-connector-java-5.1.12-bin.jar 3.log4j-1.2.17.jar 2.vo类对象 public class SysUser {private Long id;private String name;private String loginName;private String password;private String avatar;/** ge…

小白学数据:教你用Python实现简单监督学习算法

转载自 小白学数据&#xff1a;教你用Python实现简单监督学习算法今天&#xff0c;文摘菌想谈谈监督学习。监督学习作为运用最广泛的机器学习方法&#xff0c;一直以来都是从数据挖掘信息的重要手段。即便是在无监督学习兴起的近日&#xff0c;监督学习也依旧是入门机器学习的钥…

Spring_01_IoC初级总结

1.IoC简介 (转载) (原文&#xff1a;http://jinnianshilongnian.iteye.com/blog/1413846) via:jinnianshilongnian 1.1、IoC是什么 Ioc—Inversion of Control&#xff0c;即“控制反转”&#xff0c;不是什么技术&#xff0c;而是一种设计思想。在Java开发中&#xff0c;Io…

深度学习工具caffe详细安装指南

转载自 深度学习工具caffe详细安装指南前言&#xff1a; 在一台系统环境较好的linux机器上可以很容易的安装caffe&#xff0c;但是如果系统本身很旧&#xff0c;又没有GPU的话&#xff0c;安装就太麻烦了&#xff0c;所有都得从头做起&#xff0c;本文档旨在尽可能覆盖安装所要…

Spring_02_AOP初级总结

1.AOP简介 是对OOP编程方式的一种补充。翻译过来为“面向切面编程”。 可以理解为一个拦截器框架&#xff0c;但是这个拦截器会非常武断&#xff0c;如果它拦截一个类&#xff0c;那么它就会拦截这个类中的所有方法。如对一个目标列的代理&#xff0c;增强了目标类的所有方法…

spring(11)使用对象-关系映射持久化数据

【0】README1&#xff09;本文部分文字描述转自&#xff1a;“Spring In Action&#xff08;中/英文版&#xff09;”&#xff0c;旨在review “spring(11)使用对象-关系映射持久化数据” 的相关知识&#xff1b;【2】spring 与 java 持久化API1&#xff09;intro&#xff1a;…

漫画:什么是数据仓库

转载自 玻璃猫 算法与数据结构一个故事 在很久很久以前&#xff0c;世界上生活着许多种族&#xff0c;有人类&#xff0c;有矮人&#xff0c;有精灵......他们有着不同的信仰&#xff0c;不同的文化&#xff0c;彼此相安无事。可是&#xff0c;有一个猥琐男却偏偏想要统治整个世…

SpringMVC_初级总结

1.SpringMVC的工作原理 浏览器发出一个http请求给服务器&#xff0c;如果匹配DispatcherServlet的请求映射路径&#xff08;在web.xml中指定&#xff09;&#xff0c;服务器将请求转交给DispatcherServlet.DipatcherServlet接收到这个请求之后&#xff0c;根据请求的路径&#…

tomcat中配置jndi数据源以便spring获取

【0】README0&#xff09;intro to jndi&#xff0c; plase visit intro to jndi&#xff1b;1&#xff09;本文译自 Configuring Spring MVC JdbcTemplate with JNDI Data Source in Tomcat&#xff1b;2&#xff09;本文旨在分析如何通过springmvc 获取 JNDI 数据源 以连接到…

Machine Learning:十大机器学习算法

转载自 Machine Learning&#xff1a;十大机器学习算法摘要: - 机器学习算法分类&#xff1a;监督学习、无监督学习、强化学习 - 基本的机器学习算法&#xff1a;线性回归、支持向量机(SVM)、最近邻居(KNN)、逻辑回归、决策树、k平均、随机森林、朴素贝叶斯、降维、梯度增强 机…

Java的值传递解析

值传递与引用传递 最近学基础的时候&#xff0c;老师讲了值传递和引用传递&#xff0c;这个问题一直不太明白&#xff0c;上网查了很多资料&#xff0c;按照自己的理解整理了一遍&#xff0c;发现之前不太明白的地方基本上想明白了&#xff0c;如有不正确的地方&#xff0c;欢…

spring(13)缓存数据

【0】README1&#xff09;本文部分文字描述转自&#xff1a;“Spring In Action&#xff08;中/英文版&#xff09;”&#xff0c;旨在review “spring(13)缓存数据” 的相关知识&#xff1b;2&#xff09;缓存&#xff1a;缓存可以存储经常会用到的信息&#xff0c;这样每次需…

漫画:什么是分布式事务

转载自 漫画&#xff1a;什么是分布式事务&#xff1f;————— 第二天 —————————————————假如没有分布式事务 在一系列微服务系统当中&#xff0c;假如不存在分布式事务&#xff0c;会发生什么呢&#xff1f;让我们以互联网中常用的交易业务为例子&#…

Spring4.2.6+SpringMVC4.2.6+MyBatis3.4.0 整合

【0】README0&#xff09;本文旨在 review Spring4.2.6SpringMVC4.2.6MyBatis3.4.0 整合过程&#xff1b;1&#xff09;项目整合所涉及的源代码&#xff0c;please visit https://github.com/pacosonTang/MyBatis/tree/master/spring4mvc_mybatis32&#xff09;由于晚辈我还不…

ibatis(0)ibatis 与 mybatis 简述

【0】README:1&#xff09;本文旨在给出 ibatis 与 mybatis 简述&#xff0c;简述内容转自 如下链接&#xff1b;【1】main contents1&#xff09;apache offical declarationhttp://ibatis.apache.org/.apache ibatis is retired at the apache software foundation (2010/06/…

Java面试大纲

转载自 金三银四跳槽季&#xff0c;Java面试大纲跳槽时时刻刻都在发生&#xff0c;但是我建议大家跳槽之前&#xff0c;先想清楚为什么要跳槽。切不可跟风&#xff0c;看到同事一个个都走了&#xff0c;自己也盲目的面试起来&#xff08;期间也没有准备充分&#xff09;&#x…

ibatis(1)ibatis的理念

【0】README1&#xff09;本文部分内容转自 “ibatis in action”&#xff0c;旨在 review “ibatis的理念” 的相关知识&#xff1b;【1】结合所有优秀思想的混合型解决方案在现实世界中&#xff0c;混合型解决方案随处可见。将两个看上去相悖的思想在中间处巧妙结合&#xff…

究竟啥才是互联网架构“高并发”

转载自 究竟啥才是互联网架构“高并发”一、什么是高并发 高并发&#xff08;High Concurrency&#xff09;是互联网分布式系统架构设计中必须考虑的因素之一&#xff0c;它通常是指&#xff0c;通过设计保证系统能够同时并行处理很多请求。高并发相关常用的一些指标有响应时间…

ibatis(2)ibatis是什么

【0】README1&#xff09;本文部分内容转自 “ibatis in action”&#xff0c;旨在 review “ibatis是什么” 的相关知识&#xff1b;2&#xff09;intro to ibatis&#xff1a; ibatis 就是数据映射器&#xff0c;Martin Fowler在《企业应用架构模式》中&#xff0c;对 data m…