日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > > 充電吧
[導(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)系本站刪除。
換一批
延伸閱讀

LED驅(qū)動(dòng)電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: 驅(qū)動(dòng)電源

在工業(yè)自動(dòng)化蓬勃發(fā)展的當(dāng)下,工業(yè)電機(jī)作為核心動(dòng)力設(shè)備,其驅(qū)動(dòng)電源的性能直接關(guān)系到整個(gè)系統(tǒng)的穩(wěn)定性和可靠性。其中,反電動(dòng)勢(shì)抑制與過(guò)流保護(hù)是驅(qū)動(dòng)電源設(shè)計(jì)中至關(guān)重要的兩個(gè)環(huán)節(jié),集成化方案的設(shè)計(jì)成為提升電機(jī)驅(qū)動(dòng)性能的關(guān)鍵。

關(guān)鍵字: 工業(yè)電機(jī) 驅(qū)動(dòng)電源

LED 驅(qū)動(dòng)電源作為 LED 照明系統(tǒng)的 “心臟”,其穩(wěn)定性直接決定了整個(gè)照明設(shè)備的使用壽命。然而,在實(shí)際應(yīng)用中,LED 驅(qū)動(dòng)電源易損壞的問(wèn)題卻十分常見,不僅增加了維護(hù)成本,還影響了用戶體驗(yàn)。要解決這一問(wèn)題,需從設(shè)計(jì)、生...

關(guān)鍵字: 驅(qū)動(dòng)電源 照明系統(tǒng) 散熱

根據(jù)LED驅(qū)動(dòng)電源的公式,電感內(nèi)電流波動(dòng)大小和電感值成反比,輸出紋波和輸出電容值成反比。所以加大電感值和輸出電容值可以減小紋波。

關(guān)鍵字: LED 設(shè)計(jì) 驅(qū)動(dòng)電源

電動(dòng)汽車(EV)作為新能源汽車的重要代表,正逐漸成為全球汽車產(chǎn)業(yè)的重要發(fā)展方向。電動(dòng)汽車的核心技術(shù)之一是電機(jī)驅(qū)動(dòng)控制系統(tǒng),而絕緣柵雙極型晶體管(IGBT)作為電機(jī)驅(qū)動(dòng)系統(tǒng)中的關(guān)鍵元件,其性能直接影響到電動(dòng)汽車的動(dòng)力性能和...

關(guān)鍵字: 電動(dòng)汽車 新能源 驅(qū)動(dòng)電源

在現(xiàn)代城市建設(shè)中,街道及停車場(chǎng)照明作為基礎(chǔ)設(shè)施的重要組成部分,其質(zhì)量和效率直接關(guān)系到城市的公共安全、居民生活質(zhì)量和能源利用效率。隨著科技的進(jìn)步,高亮度白光發(fā)光二極管(LED)因其獨(dú)特的優(yōu)勢(shì)逐漸取代傳統(tǒng)光源,成為大功率區(qū)域...

關(guān)鍵字: 發(fā)光二極管 驅(qū)動(dòng)電源 LED

LED通用照明設(shè)計(jì)工程師會(huì)遇到許多挑戰(zhàn),如功率密度、功率因數(shù)校正(PFC)、空間受限和可靠性等。

關(guān)鍵字: LED 驅(qū)動(dòng)電源 功率因數(shù)校正

在LED照明技術(shù)日益普及的今天,LED驅(qū)動(dòng)電源的電磁干擾(EMI)問(wèn)題成為了一個(gè)不可忽視的挑戰(zhàn)。電磁干擾不僅會(huì)影響LED燈具的正常工作,還可能對(duì)周圍電子設(shè)備造成不利影響,甚至引發(fā)系統(tǒng)故障。因此,采取有效的硬件措施來(lái)解決L...

關(guān)鍵字: LED照明技術(shù) 電磁干擾 驅(qū)動(dòng)電源

開關(guān)電源具有效率高的特性,而且開關(guān)電源的變壓器體積比串聯(lián)穩(wěn)壓型電源的要小得多,電源電路比較整潔,整機(jī)重量也有所下降,所以,現(xiàn)在的LED驅(qū)動(dòng)電源

關(guān)鍵字: LED 驅(qū)動(dòng)電源 開關(guān)電源

LED驅(qū)動(dòng)電源是把電源供應(yīng)轉(zhuǎn)換為特定的電壓電流以驅(qū)動(dòng)LED發(fā)光的電壓轉(zhuǎn)換器,通常情況下:LED驅(qū)動(dòng)電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: LED 隧道燈 驅(qū)動(dòng)電源
關(guān)閉