spring 自带的缓存的使用
来源:互联网 发布:团队协作工具知乎 编辑:程序博客网 时间:2024/06/05 13:23
从未见过写得这么简单、详细的文章,spring的缓存大家可以参考
https://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/
以下是我根据这篇文章,结合自己项目的一些经验
根据项目需求,上次的微博也有提到,有几张表的数据量太大,随意需要做分表。在插入数据的时候,要判断这张表在数据库是否存在。每次都去查询数据库太浪费资源,所以这次的目的是将表名写入到spring自带的缓存。废话不多说,直接上代码:
新加spring-cache-anno.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <cache:annotation-driven /> <!-- generic cache manager --> <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="default" /> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="accountCache" /> </set> </property> </bean></beans>然后在springxxx中引入<import resource="classpath*:spring-cache-anno.xml" ></import>controller://根据传入的时间参数获取对应的表名String tableName = xxxService.getTableNameByDate(new Date());//从缓存中获取所有以"visit_history_"开头的表名,并放入缓存中,如果缓存已存在,就不查询数据库 List<String> tableNameList = tjscVisitBuyHistoryService.getTableNameListCache();//表名不存在,则创建该表,并刷新缓存if (tableNameList == null || !tableNameList.contains(tableName)) {xxxService.createTable(tableName);xxxService.updateTableNameListCache();}
service:import org.springframework.cache.annotation.Cacheable;import org.springframework.cache.annotation.CacheEvict; //根据日期返回表名 public String getTableNameByDate(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); String calendarMounth = null; //日历获取的月份为0-11,所以这里加1 if (calendar.get(Calendar.MONTH) < 9) { calendarMounth = "0" + (calendar.get(Calendar.MONTH) + 1); } else { calendarMounth = String.valueOf((calendar.get(Calendar.MONTH) + 1)); } String tableName = preTableName + calendar.get(Calendar.YEAR) + calendarMounth; return tableName;} //使用了一个缓存名叫 accountCache /*@Cacheable(value=”accountCache”), 这个注释的意思是,当调用这个方法的时候,会从一个名叫 accountCache 的缓存中查询,如果没有,则执行实际的方法(即查询数据库), 并将执行的结果存入缓存中,否则返回缓存中的对象*/ @Cacheable(value = "accountCache") public List<String> getTableNameListCache() { return getTableNameListFromDB(); } // 更新accountCache 缓存 @CacheEvict(value="accountCache") public void updateTableNameListCache() { updateTableNameListFromDB(); } private List<String> getTableNameListFromDB() { //从数据库获取表名,具体sql是 /*SELECT table_name FROM information_schema.`tables` WHERE TABLE_SCHEMA='mydb' AND table_name LIKE 'visit_history%_'*/ return xxxMapper.getTableNameList(); } private void updateTableNameListFromDB() { //从数据库获取表名 xxxMapper.getTableNameList(); }
第一次访问的时候tableNameList会从数据库中获取,后续除了更新缓存,再也没有数据库的查询操作~
阅读全文
0 0
- spring 自带的缓存的使用
- 使用spring自带的Cacheable注解处理Redis缓存
- 利用Spring aop 自带的ehcache来缓存对象
- 使用spring自带的文件上传
- django 自带页面缓存cache_page的使用及清除
- Yii框架自带缓存的使用方法
- 图片处理自带缓存的Picasso
- Yii框架自带缓存的使用方法
- MyBatis自带的缓存配置(Cache)
- 使用spring自带的编码格式过滤器
- JDK自带的缓存--包装类的缓存
- 如何使用asp.net自带的缓存优化我们的项目?
- spring自带的乱码过滤器
- spring自带的轻量级定时任务
- 配置Spring自带的编码过滤器
- 运行Spring-security自带的示例
- Spring自带的处理器映射
- spring自带的定时任务配置
- CentOS7.3安装Python3.6
- 启动项目异常处理
- 图书展示APP 数据接口调用豆瓣的数据
- Linux中文件种类
- 逻辑数据库设计
- spring 自带的缓存的使用
- 深度学习环境搭建:linux下 Ubuntu16.04+cuda8.0+cudnn+anaconda+tensorflow并配置远程访问jupyter notebook
- BZOJ 3566: [SHOI2014]概率充电器
- 为什么当前大多数应用程序是多线程的,为什么要使用支持多线程的库
- Dib to Bitmap doesn't work in WPF
- Codeforces 870
- Day 5
- 数据结构实验之串一:KMP简单应用
- Linux下Zookeeper设置开启自启动