日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

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

發布時間:2023/12/3 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring思维导图,让Spring不再难懂(cache篇) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載自?Spring思維導圖,讓Spring不再難懂(cache篇)

關于緩存

緩存是實際工作中非常常用的一種提高性能的方法。而在java中,所謂緩存,就是將程序或系統經常要調用的對象存在內存中,再次調用時可以快速從內存中獲取對象,不必再去創建新的重復的實例。這樣做可以減少系統開銷,提高系統效率。

在增刪改查中,數據庫查詢占據了數據庫操作的80%以上,而非常頻繁的磁盤I/O讀取操作,會導致數據庫性能極度低下。而數據庫的重要性就不言而喻了:

  • 數據庫通常是企業應用系統最核心的部分
  • 數據庫保存的數據量通常非常龐大
  • 數據庫查詢操作通常很頻繁,有時還很復雜

在系統架構的不同層級之間,為了加快訪問速度,都可以存在緩存

spring cache特性與缺憾

現在市場上主流的緩存框架有ehcache、redis、memcached。spring cache可以通過簡單的配置就可以搭配使用起來。其中使用注解方式是最簡單的。

Cache注解

從以上的注解中可以看出,雖然使用注解的確方便,但是缺少靈活的緩存策略,

緩存策略:

  • 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做二級緩存。

更加詳細的使用與配置,可以參考項目中spring-shiro-training中有關spring cache的配置。

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

總結

以上是生活随笔為你收集整理的Spring思维导图,让Spring不再难懂(cache篇)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。