www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁(yè) > 芯聞號(hào) > 充電吧
[導(dǎo)讀]Atitit SpringCache緩存使用 艾提拉 attilax總結(jié)?1. Spring的抽象已經(jīng)做得夠好了,適合于大多數(shù)場(chǎng)景,非常復(fù)雜的就需要自己AOP實(shí)現(xiàn)了。 11.1. 設(shè)置配置文件支持 ?

Atitit SpringCache緩存使用 艾提拉 attilax總結(jié)

?

1. Spring的抽象已經(jīng)做得夠好了,適合于大多數(shù)場(chǎng)景,非常復(fù)雜的就需要自己AOP實(shí)現(xiàn)了。 1

1.1. 設(shè)置配置文件支持 ?applicataion.xml 1

1.2. -------------通過(guò)注解去使用到Cache--- 3

1.3. @Cacheable-------使用這個(gè)注解的方法在執(zhí)行后會(huì)緩存其返回結(jié)果。 3

1.4. @CachePut?應(yīng)用到寫數(shù)據(jù)的方法上,如新增/修改方法,調(diào)用方法時(shí)會(huì)自動(dòng)把相應(yīng)的數(shù)據(jù)放入緩存:? 4

1.4.1. @CacheEvict?即應(yīng)用到移除數(shù)據(jù)的方法上,如刪除方法,調(diào)用方法時(shí)會(huì)從緩存中移除相應(yīng)的數(shù)據(jù): 4

1.4.2. @Caching?組合多個(gè)Cache注解使用 4

1.4.3. 自定義緩存注解 4

1.5. 測(cè)試 4

2. 總結(jié) 5

2.1. key的設(shè)置 默認(rèn)以參數(shù)作為key 5

2.2. 定時(shí)刷新的問(wèn)題 5

2.3. 不同的方法使用不同的緩存以及緩存時(shí)間 6

3. Spring cache獨(dú)立使用的模式 6

4. 不足之處 8

5. 參考資料 8

?

設(shè)置配置文件支持 ?applicataion.xml

1.?Spring的抽象已經(jīng)做得夠好了,適合于大多數(shù)場(chǎng)景,非常復(fù)雜的就需要自己AOP實(shí)現(xiàn)了。 1.1.?設(shè)置配置文件支持 ?applicataion.xml

?

<beans ??default-lazy-init="true"????xmlns="http://www.springframework.org/schema/beans"

???????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

???????xmlns:aop="http://www.springframework.org/schema/aop"

???????xmlns:tx="http://www.springframework.org/schema/tx"

???????xmlns:context="http://www.springframework.org/schema/context"

??????xmlns:cache="http://www.springframework.org/schema/cache"

????????

?????????xsi:schemaLocation="http://www.springframework.org/schema/beans ???????????????????????http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

???????????????????????http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

???????????????????????http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

????????????????????????http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

?????????????????????????http://www.springframework.org/schema/cache

??????????????http://www.springframework.org/schema/cache/spring-cache.xsd" ????>



???

<bean id="cacheManager"

?

?????????????????class="org.springframework.cache.concurrent.ConcurrentMapCacheManager"?/>

?

?

?

?

1.2.?-------------通過(guò)注解去使用到Cache--- 1.3.?@Cacheable-------使用這個(gè)注解的方法在執(zhí)行后會(huì)緩存其返回結(jié)果。

應(yīng)用到讀取數(shù)據(jù)的方法上,即可緩存的方法,如查找方法:先從緩存中讀取,如果沒(méi)有再調(diào)用方法獲取數(shù)據(jù),然后把數(shù)據(jù)添加到緩存中:

?

package com.cnhis.cloudhealth.clinical.clidoctor.acolsetting.dao;

?

import java.net.URLDecoder;

import java.util.List;

import java.util.Map;

import java.util.concurrent.ConcurrentMap;

?

import org.springframework.cache.annotation.Cacheable;

import org.springframework.stereotype.Repository;

?

import com.alibaba.fastjson.JSON;

import com.cnhis.cloudhealth.clinical.util.SpringUtilV3_prjcli;

import com.cnhis.cloudhealth.commons.Mappers.ModelVo;

import com.cnhis.cloudhealth.commons.dao.BaseDao;

import com.google.common.collect.Maps;

@SuppressWarnings("rawtypes")

@Repository

public class IcolSettingDao extends BaseDao {



@Cacheable(value?=?{?"Cachename1"?})

public?Object?getSetting(Map?map)?{

try?{

return????getSqlSession().selectList("IcolSetting.select_datadic",?map);

}?catch?(Exception?e)?{

throw?new?RuntimeException(e);

}

//return new ModelVo();// Maps.newConcurrentMap();

}

?

1.4.?@CachePut?應(yīng)用到寫數(shù)據(jù)的方法上,如新增/修改方法,調(diào)用方法時(shí)會(huì)自動(dòng)把相應(yīng)的數(shù)據(jù)放入緩存:? 1.4.1.?@CacheEvict?即應(yīng)用到移除數(shù)據(jù)的方法上,如刪除方法,調(diào)用方法時(shí)會(huì)從緩存中移除相應(yīng)的數(shù)據(jù):

?

1.4.2.?@Caching?組合多個(gè)Cache注解使用

有時(shí)候我們可能組合多個(gè)Cache注解使用;比如用戶新增成功后,我們要添加id-->user;username--->user;email--->user的緩存;此時(shí)就需要@Caching組合多個(gè)注解標(biāo)簽了。

?

如用戶新增成功后,添加id-->user;username--->user;email--->user到緩存;?

Java代碼??

1.?@Caching(??

2.?????????put?=?{??

3.?????????????????@CachePut(value?=?"user",?key?=?"#user.id"),??

4.?????????????????@CachePut(value?=?"user",?key?=?"#user.username"),??

5.?????????????????@CachePut(value?=?"user",?key?=?"#user.email")??

6.?????????}??

7.?)??

8.?public?User?save(User?user)?{??

1.4.3.?自定義緩存注解

比如之前的那個(gè)@Caching組合,會(huì)讓方法上的注解顯得整個(gè)代碼比較亂,此時(shí)可以使用自定義注解把這些注解組合到一個(gè)注解中,如:

?

?

1.5.?測(cè)試

SpringUtilV3_prjcli.cfgFileDir="C:\0wkspc\clis413\clinical\springtest_cli";

SpringUtilV3_prjcli.setLocations(cfgFileDir,"applicationContext-datasource.xml,onehis-dubbo.xml");

IcolSettingDao?d=(IcolSettingDao)?SpringUtilV3_prjcli.getBean(?IcolSettingDao.class);

//gene_barcode_test();

ConcurrentMap

Object?setting?=?d.getSetting(newConcurrentMap);

System.out.println(setting);


Object?setting2?=?d.getSetting(newConcurrentMap);

System.out.println(setting2);

?

?

2.?總結(jié) 2.1.?key的設(shè)置 默認(rèn)以參數(shù)作為key

?????? @Cacheable(value="緩存空間的名稱,xml中配置的" , key = "#spittle.id")--spittle是參數(shù)里面的spittle,如果不設(shè)置,就以參數(shù)作為key

??????? Spittle save(Spittle spittle);

2.2.?定時(shí)刷新的問(wèn)題

·?緩存失效時(shí)間支持在方法的注解上指定
Spring Cache默認(rèn)是不支持在@Cacheable上添加過(guò)期時(shí)間的,可以在配置緩存容器時(shí)統(tǒng)一指定:

@Bean

ConcurrentMapCacheManager可以作為一種緩存方案,但不能設(shè)置過(guò)期,最大緩存條目等,需進(jìn)行改造。

???class="com.cnhis.cloudhealth.clinical.util.cache.MyConcurrentMapCacheManager">

????????

?

其實(shí)也可以timer to refresh

?

2.3.?不同的方法使用不同的緩存以及緩存時(shí)間

?

還可以使用不同的多個(gè) CacheManager?每個(gè),cm時(shí)間不同即可。。注意cache那么也不要重復(fù)了,可以增加個(gè)cm前綴命名空間。。

?

另外還提供了CompositeCacheManager用于組合CacheManager,即可以從多個(gè)CacheManager中輪詢得到相應(yīng)的Cache,如

Java代碼??

1.?

2.?????

3.?????????

4.?????????????

5.?????????????

6.???????????

7.???????

8.?????

9.???

當(dāng)我們調(diào)用cacheManager.getCache(cacheName) 時(shí),會(huì)先從第一個(gè)cacheManager中查找有沒(méi)有cacheName的cache,如果沒(méi)有接著查找第二個(gè),如果最后找不到,因?yàn)閒allbackToNoOpCache=true,那么將返回一個(gè)NOP的Cache否則返回null。

?

Spring Cache抽象詳解,一篇很好的spring Cache的解釋文章,結(jié)合源碼更加讓人容易懂 - CSDN博客.html

?

不同的緩存可以指定cachename

不同的時(shí)間,則只好使用不同的定時(shí)器,清理指定的緩存

CacheManager?CacheManager1=??(CacheManager)?SpringUtilV3_prjcli.getBean(?CacheManager.class);

Cache?Cache1=CacheManager1.getCache("Cachename1");

Cache1.clear();

?

?

?

3.?Spring cache獨(dú)立使用的模式

?

package com.cnhis.cloudhealth.clinical.util.cache;

?

/*

?* Copyright 2002-2014 the original author or authors.

?*

?* Licensed under the Apache License, Version 2.0 (the "License");

?* you may not use this file except in compliance with the License.

?* You may obtain a copy of the License at

?*

?* ?????http://www.apache.org/licenses/LICENSE-2.0

?*

?* Unless required by applicable law or agreed to in writing, software

?* distributed under the License is distributed on an "AS IS" BASIS,

?* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

?* See the License for the specific language governing permissions and

?* limitations under the License.

?*/

?

import java.util.Collection;

import java.util.Collections;

import java.util.Map;

import java.util.concurrent.ConcurrentHashMap;

import java.util.concurrent.ConcurrentMap;

import java.util.concurrent.TimeUnit;

?

import org.springframework.cache.Cache;

import org.springframework.cache.CacheManager;

import org.springframework.cache.concurrent.ConcurrentMapCache;

?

import com.google.common.cache.CacheBuilder;

?

/**

?* {@link CacheManager} implementation that lazily builds {@link ConcurrentMapCache}

?* instances for each {@link #getCache} request. Also supports a ‘static‘ mode where

?* the set of cache names is pre-defined through {@link #setCacheNames}, with no

?* dynamic creation of further cache regions at runtime.

?*

?*

Note: This is by no means a sophisticated CacheManager; it comes with no

?* cache configuration options. However, it may be useful for testing or simple

?* caching scenarios. For advanced local caching needs, consider

?* {@link org.springframework.cache.guava.GuavaCacheManager} or

?* {@link org.springframework.cache.ehcache.EhCacheCacheManager}.

?*

?* @author Juergen Hoeller

?* @since 3.1

?* @see ConcurrentMapCache

?*/

public class MyConcurrentMapCacheManager implements CacheManager {

public static void main(String[] args) {

MyConcurrentMapCacheManager CacheManager2=new MyConcurrentMapCacheManager(20, 1000);

????CacheManager2.createConcurrentMapCache("cachename1");

????

????Cache Cache1=CacheManager2.getCache("cachename1");

????

????Cache1.put("k", "vv"); ???

????System.out.println(Cache1.get("k").get());

????System.out.println(Cache1.get("k2").get()); ??//if no key ?ret ?NullPointerException

????

}

?

4.?不足之處

?

當(dāng)然Spring Cache注解對(duì)于大多數(shù)場(chǎng)景夠用了,如果場(chǎng)景復(fù)雜還是考慮使用AOP吧;如果自己實(shí)現(xiàn)請(qǐng)考慮使用Spring Cache API進(jìn)行緩存抽象。

5.?參考資料

SpringCache緩存初探 - 沐魘 - 博客園.html

Spring Cache抽象詳解,一篇很好的spring Cache的解釋文章,結(jié)合源碼更加讓人容易懂 - CSDN博客.html (注解全局配置)

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國(guó)汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來(lái)越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來(lái)越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開幕式在貴陽(yáng)舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語(yǔ)權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營(yíng)業(yè)績(jī)穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤(rùn)率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長(zhǎng) 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營(yíng)商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國(guó)電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長(zhǎng)三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡(jiǎn)稱"軟通動(dòng)力")與長(zhǎng)三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉