spring的缓存机制,是方法纬度的缓存机制,

这就意味着我们并不用关注 底层是否使用了数据库以及通过什么方式访问的数据库;

因此,此缓存方法既适用于dao层,也适用于service层

spring配置文件配置:
<!--缓存配置-->
<!--启用缓存注解功能-->
<cache:annotation-driven cache-manager="cacheManager"/>
<!--spring自己的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3.1开始提供)-->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
      <!-- 此处类concurrentMapCacheFactoryBean的作用是产生 缓存类 实例-->
<bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/>
</set>
</property>
</bean>
service层示例如下:
@Transactional(readOnly = true)
@Cacheable(value = "myCache")
public JsonObject getWFK(JsonObject params){
OneOb ob = new OneOb();
try {
List<Map<String,Object>> map = LawAssistantMapper.getWFK();
ob.setOb(map);
}catch (Exception e) {
e.printStackTrace();
ob.setCode(500);
ob.setMsg("服务器错误!!!");
return new JsonObject(Json.encode(ob));
}
ob.setCode(200);
ob.setMsg("ok");
logger.debug(Json.encode(ob));
return new JsonObject(Json.encode(ob)); } 由于使用的是spring自带的缓存类,所以,仅仅需要两步:1.在spring配置文件中声明,2.在service层,方法代码前增加注解,即可。 缺点:
spring自带的缓存功能,实质上是通过java类来保存缓存的数据,这样会占用一定的内存消耗,并发率越高,对内存的压力越大。
码民直接使用的缓存类:
org.springframework.cache.support.SimpleCacheManager,
org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean


spring 缓存(spring自带Cache)(入门)的更多相关文章

  1. spring 缓存机制

    简介 Spring3.1开始引入了基于注释的缓存,其使用方法和原理类似于Spring对事务管理的支持.可以对容器中的任意的bean或bean的方法添加缓存.   配置Spring缓存 Spring缓存 ...

  2. spring 缓存(spring自带Cache)(入门)源码解读

    spring自带的缓存类有两个基础类:Cache(org.springframework.cache.Cache)类,CacheManager(org.springframework.cache.Ca ...

  3. Spring Boot 2.x基础教程:进程内缓存的使用与Cache注解详解

    随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决这一问题非常好的手段之一.Spring 3开始提供了强大的基于注解的缓 ...

  4. spring缓存Ehcache(入门2)源码解读

    Ehcache缓存: 解读: Ehcache缓存是在继承spring缓存核心类CacheManager的基础上实现的. 常用类: EhCacheCacheManager:继承自CacheManager ...

  5. Spring缓存注解@Cache,@CachePut , @CacheEvict,@CacheConfig使用

    @Cacheable.@CachePut.@CacheEvict 注释介绍 表 1. @Cacheable 作用和配置方法 @Cacheable 的作用 主要针对方法配置,能够根据方法的请求参数对其结 ...

  6. 手把手带你入门 Spring Security!

    Spring Security 是 Spring 家族中的一个安全管理框架,实际上,在 Spring Boot 出现之前,Spring Security 就已经发展了多年了,但是使用的并不多,安全管理 ...

  7. 【Java EE 学习 78 上】【数据采集系统第十天】【Service使用Spring缓存模块】

    一.需求分析 调查问卷中或许每一个单击动作都会引发大量的数据库访问,特别是在参与调查的过程中,只是单击“上一页”或者“下一页”的按钮就会引发大量的查询,必须对这种问题进行优化才行.使用缓存策略进行查询 ...

  8. Spring – 缓存注解

    Spring缓存抽象概述 Spring框架自身并没有实现缓存解决方案,但是从3.1开始定义了org.springframework.cache.Cache和org.springframework.ca ...

  9. Spring 缓存注解解析过程

    Spring 缓存注解解析过程 通过 SpringCacheAnnotationParser 的 parseCacheAnnotations 方法解析指定方法或类上的缓存注解, @Cacheable ...

  10. Spring缓存机制的理解

    在spring缓存机制中,包括了两个方面的缓存操作:1.缓存某个方法返回的结果:2.在某个方法执行前或后清空缓存. 下面写两个类来模拟Spring的缓存机制: package com.sin90lzc ...

随机推荐

  1. ZendGuardLoader安装

    遇到的问题 php -v Zend Guard Loader requires Zend Engine API version 220090626. The Zend Engine API versi ...

  2. Non Lasting Storage File System、procfs、sysfs

    catalog . 引言 . proc文件系统 . 简单的文件系统 . sysfs 0. 引言 传统上,文件系统用于在块设备上持久存储数据,但也可以使用文件系统来组织.提供.交换并不存储在块设备上的信 ...

  3. linux下制作win7安装盘(mint、ubuntu、debian)

    今天替同事装系统.因为现在的debian系系统都可以直接用dd拷贝iso到u盘的方法进行安装,所以,想当然的用dd来制作windows安装盘.没反应!所以有了下面的方法. 1. 将u盘格式化为ntfs ...

  4. C#的图像处理方法--(作者:http://conner-wang.spaces.live.com转载)

    使用C#进行图像处理的几种方法 本文讨论了C#图像处理中Bitmap类.BitmapData类和unsafe代码的使用以及字节对齐问题. Bitmap类 命名空间:System.Drawing 封装 ...

  5. Unity3d5.0 新UI之2048

    因为汽车系统没写出来所以,纠结之中,弄了弄新版本的UI. 做了个2048. 新版本的unity的UI必须以Canvas为基底来呈现,如果没有加画布的话可是显示不出来东西的哦. 而且作为UI上的所有组件 ...

  6. PowerDesigner 学习笔记

    软件:PowerDesginer16.5 首先说一下对应的模型 New Model  --->  Model Type 作为一个英语渣,实在是压力颇大,然而汉化经常会导致无故的BUG,所以简单翻 ...

  7. 数据结构作业——max_and_min(栈)

    Description TonyY 最近喜欢上了数学,今天他研究一个只有加号和乘号,运算数为整数, 大小在 1-9 之间的表达式,你可以任意地往里加括号,如何让表达式的值最大或 者最小? Input ...

  8. 解决Eclipse 项目报错:Unbound classpath container

    Eclipse出现下面两条报错: The project cannot be built until build path errors are resolved HelloWord Unknown ...

  9. matlab解三元二次方程组

    C1=7.0863; C2=6.8971; C3=0.4929; C4=0.8131; C5=1.8240; C6=3.8108; C7=3.7318; C8=-2.2238; C9=1.9905; ...

  10. 阻塞式socket例子学习

    /************************************************************************* > File Name: Win_Serve ...