Atitit SpringCache缓存使用 艾提拉 attilax总结 1. Spring的抽象已经做得够好了,适合于大多数场景,非常复杂的就需要自己AOP实现了。 1 1.1. 设置配置文件支持
Atitit SpringCache缓存使用 艾提拉 attilax总结
1. Spring的抽象已经做得够好了,适合于大多数场景,非常复杂的就需要自己AOP实现了。 1
1.1. 设置配置文件支持 applicataion.xml 1
1.2. -------------通过注解去使用到Cache--- 3
1.3. @Cacheable-------使用这个注解的方法在执行后会缓存其返回结果。 3
1.4. @CachePut 应用到写数据的方法上,如新增/修改方法,调用方法时会自动把相应的数据放入缓存: 4
1.4.1. @CacheEvict 即应用到移除数据的方法上,如删除方法,调用方法时会从缓存中移除相应的数据: 4
1.4.2. @Caching 组合多个Cache注解使用 4
1.4.3. 自定义缓存注解 4
1.5. 测试 4
2. 总结 5
2.1. key的设置 默认以参数作为key 5
2.2. 定时刷新的问题 5
2.3. 不同的方法使用不同的缓存以及缓存时间 6
3. Spring cache独立使用的模式 6
4. 不足之处 8
5. 参考资料 8
设置配置文件支持 applicataion.xml
1. Spring的抽象已经做得够好了,适合于大多数场景,非常复杂的就需要自己AOP实现了。
1.1. 设置配置文件支持 applicataion.xml
<?xml version="1.0" encoding="UTF-8"?>
<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" >
<!-- ati cache cfg-->
<context:annotation-config/>
<cache:annotation-driven/>
<bean id="cacheManager"
class="org.springframework.cache.concurrent.ConcurrentMapCacheManager" />
<!-- end ati cache -->
1.2. -------------通过注解去使用到Cache---
1.3. @Cacheable-------使用这个注解的方法在执行后会缓存其返回结果。
应用到读取数据的方法上,即可缓存的方法,如查找方法:先从缓存中读取,如果没有再调用方法获取数据,然后把数据添加到缓存中:
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 应用到写数据的方法上,如新增/修改方法,调用方法时会自动把相应的数据放入缓存:
1.4.1. @CacheEvict 即应用到移除数据的方法上,如删除方法,调用方法时会从缓存中移除相应的数据:
1.4.2. @Caching 组合多个Cache注解使用
有时候我们可能组合多个Cache注解使用;比如用户新增成功后,我们要添加id-->user;username--->user;email--->user的缓存;此时就需要@Caching组合多个注解标签了。
如用户新增成功后,添加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. 自定义缓存注解
比如之前的那个@Caching组合,会让方法上的注解显得整个代码比较乱,此时可以使用自定义注解把这些注解组合到一个注解中,如:
1.5. 测试
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, Object> newConcurrentMap = Maps.newConcurrentMap();newConcurrentMap.put("key", "key1");
Object setting = d.getSetting(newConcurrentMap);
System.out.println(setting);
Object setting2 = d.getSetting(newConcurrentMap);
System.out.println(setting2);
2. 总结
2.1. key的设置 默认以参数作为key
@Cacheable(value="缓存空间的名称,xml中配置的" , key = "#spittle.id")--spittle是参数里面的spittle,如果不设置,就以参数作为key
Spittle save(Spittle spittle);
2.2. 定时刷新的问题
· 缓存失效时间支持在方法的注解上指定
Spring Cache默认是不支持在@Cacheable上添加过期时间的,可以在配置缓存容器时统一指定:
@Bean
ConcurrentMapCacheManager可以作为一种缓存方案,但不能设置过期,最大缓存条目等,需进行改造。
class="com.cnhis.cloudhealth.clinical.util.cache.MyConcurrentMapCacheManager">
<constructor-arg index="0" value="300" />
其实也可以timer to refresh
2.3. 不同的方法使用不同的缓存以及缓存时间
还可以使用不同的多个 CacheManager 每个,cm时间不同即可。。注意cache那么也不要重复了,可以增加个cm前缀命名空间。。
另外还提供了CompositeCacheManager用于组合CacheManager,即可以从多个CacheManager中轮询得到相应的Cache,如
Java代码
1. <bean id="cacheManager" class="org.springframework.cache.support.CompositeCacheManager">
2. <property name="cacheManagers">
3. <list>
4. <ref bean="ehcacheManager"/>
5. <ref bean="jcacheManager"/>
6. </list>
7. </property>
8. <property name="fallbackToNoOpCache" value="true"/>
9. </bean>
当我们调用cacheManager.getCache(cacheName) 时,会先从第一个cacheManager中查找有没有cacheName的cache,如果没有接着查找第二个,如果最后找不到,因为fallbackToNoOpCache=true,那么将返回一个NOP的Cache否则返回null。
Spring Cache抽象详解,一篇很好的spring Cache的解释文章,结合源码更加让人容易懂 - CSDN博客.html
不同的缓存可以指定cachename
不同的时间,则只好使用不同的定时器,清理指定的缓存
CacheManager CacheManager1= (CacheManager) SpringUtilV3_prjcli.getBean( CacheManager.class);
Cache Cache1=CacheManager1.getCache("Cachename1");
Cache1.clear();
3. Spring cache独立使用的模式
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.
*
* <p>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. 不足之处
当然Spring Cache注解对于大多数场景够用了,如果场景复杂还是考虑使用AOP吧;如果自己实现请考虑使用Spring Cache API进行缓存抽象。
5. 参考资料
SpringCache缓存初探 - 沐魇 - 博客园.html
Spring Cache抽象详解,一篇很好的spring Cache的解释文章,结合源码更加让人容易懂 - CSDN博客.html (注解全局配置)
Atitit SpringCache缓存使用 艾提拉 attilax总结 1. Spring的抽象已经做得够好了,适合于大多数场景,非常复杂的就需要自己AOP实现了。 1 1.1. 设置配置文件支持相关推荐
- Atitit mis 管理信息系统概论 艾提拉著 目录 1. 互联网三大定律 2 1.1. 摩尔定律和 2 1.2. 吉尔德定律 电脑及网络宽带资源成为重要免费资源 2 1.3. 梅特卡夫定律 用户
Atitit mis 管理信息系统概论 艾提拉著 目录 1. 互联网三大定律 2 1.1. 摩尔定律和 2 1.2. 吉尔德定律 电脑及网络宽带资源成为重要免费资源 2 1.3. 梅特卡夫定律 用户越 ...
- Atitit 关于艾提拉个人简绍 中文日文英文对照版
Atitit 关于艾提拉个人简绍 日文版 经历了很多项目实践,具备较为宽广的IT从业与信息化工作背景,具备若干创业历程,道路曲折,初心不改.在相关领域累计了较深的深度(细化度)与高度(抽象度)与广度 ...
- 艾提拉近年来技术大总结
##基本信息 姓名:艾提拉 民族:汉 生日:1984.3 email:1466519819@qq.com 英文名&网名id : attilax 曾用名:艾龙 ##主要方向与目标: 技术 ...
- 大话数据结构读书笔记艾提拉总结 查找算法 和排序算法比较好 第1章数据结构绪论 1 第2章算法 17 第3章线性表 41 第4章栈与队列 87 第5章串 123 第6章树 149 第7章图 21
大话数据结构读书笔记艾提拉总结 查找算法 和排序算法比较好 第1章数据结构绪论 1 第2章算法 17 第3章线性表 41 第4章栈与队列 87 第5章串 123 第6章树 149 第7章图 211 第 ...
- Atitit 切入一个领域的方法总结 attilax这里,机器学习为例子
Atitit 切入一个领域的方法总结 attilax这里,机器学习为例子 1.1. 何为机器学习?1 1.2. 两类机器学习算法 :监督式学习(Supervised Learning)和非监督式学习( ...
- Atitit 怎么阅读一本书 消化 分析 检索 attilax总结 1 读书的本质 是数据的处理,大量的数据,处理能力
分享一下我老师大神的人工智能教程.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow Atitit 怎么 ...
- Atitit 怎么阅读一本书 消化 分析 检索 attilax总结 1 读书的本质 是数据的处理 大量的数据 处理能力
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Atit ...
- Atitit 学习方法 补充 艾龙 著 attilax著 1. Atitit 学习的方法 attilax总结 1 1.1. 2. 基于学习策略的分类 2机械 示教 演绎 类比 解释 归纳 2 1.
Atitit 学习方法 补充 艾龙 著 attilax著 1. Atitit 学习的方法 attilax总结 1 1.1. 2. 基于学习策略的分类 2机械 示教 演绎 类比 解释 归纳 2 1.2 ...
- atitit.微信支付的教程文档 attilax总结
atitit.微信支付的教程文档 attilax总结 1. 支付流程概览 1 2. 设置支付起始目录 host/app/paydir/ 1 3. 设置oauth验证域名 1 4. 测试目录 可以使 ...
- SpringCache缓存处理
SpringCache缓存处理 学习过redis的都知道redis缓存是一个很不错的nosql,对于上线项目正的有很大的帮助,平常在java中使用一般都是通过jedis或者spring-boot-st ...
最新文章
- 分布式 RPC 框架
- 软件测试(一):概念篇
- select可选择、同时可自行输入
- chrome 浏览器自保留端口、安全端口有哪些?chrome不能访问某个端口的环境网址,但是其它的浏览器可以访问原因及解决办法
- 随笔汇总_索取资料请进入
- 如何查找Fiori UI上某个字段对应的后台存储表的名称
- Android屏幕大小适配问题解决
- 与c++ 进行最简单的进程通信
- [导入][转载]超强大的jquery formValidator
- CGO磁盘管理For流星无语
- Linux 常用解压缩命令
- Javascript 学习之路:鼠标长按事件
- UVa 12333 - Revenge of Fibonacci manweifc(模拟加法竖式 字典树)
- DNS服务器配置图解过程(1)
- 跨时代作品:超级IE缓存提取器
- SnowNLP——SnowNLP是什么及SnowNLP的使用示例
- codeblocks 汉化包+汉化教程,仅需三步
- 如何设置计算机用户名和密码忘了怎么办,电脑密码忘记了怎么办
- 会议报到和撒离时间算会期_会议议程会议日程格式
- 1.1微信支付之现金红包 - Java 开发