做百度网上搜索引擎推广最好网站,trellis wordpress,iis网站目录权限设置,韩国网站模板下载地址转载自 Spring思维导图#xff0c;让Spring不再难懂#xff08;cache篇#xff09; 关于缓存 缓存是实际工作中非常常用的一种提高性能的方法。而在java中#xff0c;所谓缓存#xff0c;就是将程序或系统经常要调用的对象存在内存中#xff0c;再次调用时可以快速从内存…转载自 Spring思维导图让Spring不再难懂cache篇 关于缓存 缓存是实际工作中非常常用的一种提高性能的方法。而在java中所谓缓存就是将程序或系统经常要调用的对象存在内存中再次调用时可以快速从内存中获取对象不必再去创建新的重复的实例。这样做可以减少系统开销提高系统效率。
在增删改查中数据库查询占据了数据库操作的80%以上而非常频繁的磁盘I/O读取操作会导致数据库性能极度低下。而数据库的重要性就不言而喻了
数据库通常是企业应用系统最核心的部分数据库保存的数据量通常非常庞大数据库查询操作通常很频繁有时还很复杂
在系统架构的不同层级之间为了加快访问速度都可以存在缓存 spring cache特性与缺憾 现在市场上主流的缓存框架有ehcache、redis、memcached。spring cache可以通过简单的配置就可以搭配使用起来。其中使用注解方式是最简单的。 Cache注解 从以上的注解中可以看出虽然使用注解的确方便但是缺少灵活的缓存策略
缓存策略 TTLTime To Live 存活期即从缓存中创建时间点开始直到它到期的一个时间段不管在这个时间段内有没有访问都将过期 TTITime 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 beforeInvocationfalse表示在方法执行之后调用#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集成ehcachespring-ehcache.xml主要内容
dependencygroupIdnet.sf.ehcache/groupIdartifactIdehcache-core/artifactIdversion${ehcache.version}/version
/dependency!-- Spring提供的基于的Ehcache实现的缓存管理器 --!-- 如果有多个ehcacheManager要在bean加上p:sharedtrue --
bean idehcacheManager classorg.springframework.cache.ehcache.EhCacheManagerFactoryBeanproperty nameconfigLocation valueclasspath:xml/ehcache.xml/
/beanbean idcacheManager classorg.springframework.cache.ehcache.EhCacheCacheManagerproperty namecacheManager refehcacheManager/property nametransactionAware valuetrue/
/bean!-- cache注解和spring-redis.xml中的只能使用一个 --
cache:annotation-driven cache-managercacheManager proxy-target-classtrue/spring cache集成redisspring-redis.xml主要内容
dependencygroupIdorg.springframework.data/groupIdartifactIdspring-data-redis/artifactIdversion1.8.1.RELEASE/version
/dependency
dependencygroupIdorg.apache.commons/groupIdartifactIdcommons-pool2/artifactIdversion2.4.2/version
/dependency
dependencygroupIdredis.clients/groupIdartifactIdjedis/artifactIdversion2.9.0/version
/dependency!-- 注意需要添加Spring Data Redis等jar包 --
descriptionredis配置/descriptionbean idjedisPoolConfig classredis.clients.jedis.JedisPoolConfigproperty namemaxIdle value${redis.pool.maxIdle}/property namemaxTotal value${redis.pool.maxActive}/property namemaxWaitMillis value${redis.pool.maxWait}/property nametestOnBorrow value${redis.pool.testOnBorrow}/property nametestOnReturn value${redis.pool.testOnReturn}/
/bean!-- JedisConnectionFactory --
bean idjedisConnectionFactory classorg.springframework.data.redis.connection.jedis.JedisConnectionFactoryproperty namehostName value${redis.master.ip}/property nameport value${redis.master.port}/property namepoolConfig refjedisPoolConfig/
/beanbean idredisTemplate classorg.springframework.data.redis.core.RedisTemplatep:connectionFactory-refjedisConnectionFactoryproperty namekeySerializerbean classorg.springframework.data.redis.serializer.JdkSerializationRedisSerializer/bean/propertyproperty namevalueSerializerbean classorg.springframework.data.redis.serializer.JdkSerializationRedisSerializer//propertyproperty namehashKeySerializerbean classorg.springframework.data.redis.serializer.JdkSerializationRedisSerializer//propertyproperty namehashValueSerializerbean classorg.springframework.data.redis.serializer.JdkSerializationRedisSerializer//property
/bean!--spring cache--
bean idcacheManager classorg.springframework.data.redis.cache.RedisCacheManagerc:redisOperations-refredisTemplate!-- 默认缓存10分钟 --property namedefaultExpiration value600/property nameusePrefix valuetrue/!-- cacheName 缓存超时配置半小时一小时一天 --property nameexpiresmap key-typejava.lang.String value-typejava.lang.Longentry keyhalfHour value1800/entry keyhour value3600/entry keyoneDay value86400/!-- shiro cache keys --entry keyauthorizationCache value1800/entry keyauthenticationCache value1800/entry keyactiveSessionCache value1800//map/property
/bean
!-- cache注解和spring-ehcache.xml中的只能使用一个 --
cache:annotation-driven cache-managercacheManager proxy-target-classtrue/
项目中注解缓存只能配置一个所以可以通过以下引入哪个配置文件来决定使用哪个缓存。
import resourceclasspath:spring/spring-ehcache.xml/
!-- import resourceclasspath:spring/spring-redis.xml/--当然可以通过其他配置搭配使用两个缓存机制。比如ecache做一级缓存redis做二级缓存。 更加详细的使用与配置可以参考项目中spring-shiro-training中有关spring cache的配置。
https://git.oschina.net/wangzhixuan/spring-shiro-training.git