Spring-spring缓存和ehcache(二)

Ehcache如何支持分布式缓存?在分布式环境下,如何保证缓存的一致性?Ehcache的缓存过期和缓存失效机制。Ehcache如何与Spring框架集成?如何配置和使用Ehcache? 上述示例中定义了一个默认的缓存,其最大内存元素数量为10000,非永久,空闲时间为120秒,生存时间为120秒,当内存满时溢出到磁盘,磁盘存储不是持久的,磁盘过期线程间隔时间为120秒,内存存储的驱逐策略为LRU(最近最少使用)。需要注意的是,Ehcache的配置和使用可能会因版本和项目需求的不同而有所差异,因此在实际使用中,你可能需要查阅相关的官方文档或参考其他资源来获取更详细和准确的信息。通过合理设置过期时间,可以确保缓存中的数据始终是最新的,并减少因数据过时而导致的错误或不一致。 阅读详情

今天仔细读了读spring的缓存相关的内容,这里做个总结:

缓存,即一种存储机制,数据可以存放在redis这类nosql的数据库内,存放在本机存储器,存放量相对小,但读取速度非常快。

spring运用的缓存会如AOP一样,准确到方法级别,能明确到哪个方法采用缓存,什么条件下才缓存。

测试所需jar包如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>gccSpring</groupId>
  <artifactId>gccSpring</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>gccSpring Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.14.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.14.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>4.3.14.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache -->
    <dependency>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache</artifactId>
      <version>2.10.4</version>
    </dependency>


  </dependencies>
  <build>
    <finalName>gccSpring</finalName>
  </build>
</project>

主要为spring核心包,spring上下文包。在支持ehcache时再添加spring支持包,ehcache包。



编写的测试实体如下:

package com.gcc.entity;

/**
 * Created by gcc on 2018/2/26.
 */
public class Users {

    private int id;
    private String name;
    private String phoneNumber;
    private int age;


    public Users() {
    }

    public Users(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public Users(int id, String name, String phoneNumber, int age) {
        this.id = id;
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Users{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", phoneNumber='" + phoneNumber + '\'' +
                ", age=" + age +
                '}';
    }
}

get,set方法可以省略,数据通过构造方法注入,这里只是本人强迫症写了下



service类内容如下:

package com.gcc.service;

import com.gcc.entity.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by gcc on 2018/2/26.
 */
@Service
public class UserService {

    private Map<Integer,Users> users = new HashMap<Integer, Users>();

    {
        users.put(1,new Users(1,"admin"));
        users.put(2,new Users(2,"jack"));
        users.put(3,new Users(3,"mary"));
        users.put(4,new Users(4,"sky","13598789990",20));
        users.put(5,new Users(5,"faker","13598789991",21));
    }

    //服务启动后先添加预定的缓存数据
    @Autowired
    CacheManager cacheManager;

    @PostConstruct
    public void setup(){
        Cache userCache = cacheManager.getCache("users");
        for (Integer key :users.keySet()){
            userCache.put(key,users.get(key));
        }
    }

    /**
     @Cacheable 查询缓存,对返回结果缓存,默认的value必填,缓存储存器ConcurrentMapCache的名字.condition是缓存条件
     @CacheEvict 删除缓存条目,在删除某数据时候可以一同删除缓存
     @CachePut 更新缓存条目
     @CacheConfig
     */
    @Cacheable(value = "users",condition = "#id!=3")
    public Users getUser(int id){
        System.out.println("get id is:"+id);
        return users.get(id);
    }

}

用于添加静态数据,初始化填充数据,测试方法为getUser



测试类如下:

package com.gcc.test;

import com.gcc.entity.Users;
import com.gcc.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by gcc on 2018/2/26.
 */
public class test {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//        ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        UserService service = context.getBean(UserService.class);
        Users users = service.getUser(1);
        System.out.println(users);
        Users users2 = service.getUser(1);
        System.out.println(users2);

        Users users3 = service.getUser(3);
        System.out.println(users3);
        Users users4 = service.getUser(3);
        System.out.println(users4);

        Users users5 = service.getUser(4);
        System.out.println(users5);
        Users users6 = service.getUser(4);
        System.out.println(users6);

    }

}

第一种方法是通过xml来实例化spring,再获取bean,调用测试类

第二种方法是通过java代码实现spring


第一种方法的xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<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"
       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/>

    <bean id="userService" class="com.gcc.service.UserService"></bean>
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <property name="caches">
            <set>
                <bean id="users" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"></bean>
            </set>
        </property>
    </bean>

    <bean id="ehcacheManger" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehcache"></property>
    </bean>
    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml"></property>
    </bean>

</beans>

上方的bean是spring自带的缓存管理器,这里用于测试,set设置的是缓存存放的数据集合名字

下方的bean是企业级别的常用缓存框架,ehcache,设置对应的管理器和初始化工厂。

ehcache.xm简单配置如下:

<ehcache>
    <cache name="users" maxElementsInMemory="1000"></cache>
</ehcache>


第二种方法java代码如下:

package com.gcc.tool;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * Created by gcc on 2018/2/26.
 */
@Configuration
@ComponentScan(basePackages = {"com.gcc.*"})
@EnableCaching
public class ApplicationConfig {

    @Bean
    public CacheManager cacheManager(){
        SimpleCacheManager cacheManager=new SimpleCacheManager();
        cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("users")));
        return cacheManager;
    }

}

这里通过代码设置了缓存管理器,并命名ConcurrentMapCache


方式一运行结果如下:

get id is:1
Users{id=1, name='admin', phoneNumber='null', age=0}
Users{id=1, name='admin', phoneNumber='null', age=0}
get id is:3
Users{id=3, name='mary', phoneNumber='null', age=0}
get id is:3
Users{id=3, name='mary', phoneNumber='null', age=0}
get id is:4
Users{id=4, name='sky', phoneNumber='13598789990', age=20}
Users{id=4, name='sky', phoneNumber='13598789990', age=20}

方式二运行结果如下:

Users{id=1, name='admin', phoneNumber='null', age=0}
Users{id=1, name='admin', phoneNumber='null', age=0}
get id is:3
Users{id=3, name='mary', phoneNumber='null', age=0}
get id is:3
Users{id=3, name='mary', phoneNumber='null', age=0}
Users{id=4, name='sky', phoneNumber='13598789990', age=20}
Users{id=4, name='sky', phoneNumber='13598789990', age=20}

说明:因为xml没有加扫描注解的路径范围,所以方式一是扫描不到service的@PostConstruct的,而这注解会在servlet init之前调用,所以方式一不会进行初始化时添加数据


        
从星形到三角形:永磁同步电机FOC+SVPWM控制的相位偏移与扇区重构 本文深入探讨了将FOC+SVPWM控制方案从星形接法永磁同步电机移植到三角形接法时遇到的核心挑战。文章重点解析了由绕组连接方式差异导致的30度相位偏移问题,并详细阐述了如何为此重构扇区判断逻辑、矢量作用时间计算以及电流采样变换,为工程师提供了从理论到实践的完整移植指南。 阅读详情

相关推荐

Flutter模块打包AAR集成Android原生项目实践

跨平台开发框架Flutter通过将UI组件渲染为原生代码实现高性能表现,其核心优势在于一套代码可同时运行在iOS和Android平台。在工程实践中,将Flutter模块打包为AAR(Android Archive)进行集成,成为混合开发的主流方案。这种方案既保留了原生项目的稳定性,又能渐进式引入Flutter的高效开发特性。通过配置MethodChannel实现原生与Dart代码通信,开发者可以灵活控制Flutter引擎生命周期,优化内存占用和渲染性能。对于电商、社交等需要快速迭代的业务场景,该方案能显著提

weixin_34025051的博客 399

SpringBoot整合Ehcache缓存

1.pom.xml配置依赖包 <!-- ehcache依赖 --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.8.3</version>...

王绍桦 1116

从ResNet到EfficientNet:特征金字塔网络(FPN)在不同骨干网中的适配指南

本文深入探讨了特征金字塔网络(FPN)与ResNet、VGG、EfficientNet等主流骨干网络的适配策略。文章详细解析了FPN的核心原理,并针对不同骨干网络的特征提取特性,提供了通道对齐、参数量估算及迁移学习的系统化指南,旨在帮助开发者在目标检测等视觉任务中构建高性能、高效率的模型。

weixin_29047919的博客 221

spring整合ehcache找不到org.springframework.cache.ehcache.EhCacheCacheManager的解决方案

一般org.springframework.cache.ehcache.EhCacheCacheManager和org.springframework.cache.ehcache.EhCacheManagerFactoryBean会同时找不到。 解决方法很简单,在pom.xml里引入这个依赖即可: <dependency> <groupId>org.springfr...

HermitSun的博客 4745

获取气体传感器温度调制响应数据并转换为NumPy数组

本任务旨在获取气体传感器在温度调制条件下的响应数据,将其处理为结构化数据格式(NumPy数组),以便用于机器学习模型训练。整个过程包含数据采集、预处理、特征工程和格式转换等关键步骤。

huanghm88的专栏 1207

【赛题解析&命令脚本配置】2023年全国职业院校技能大赛GZ073网络系统管理赛项赛题第10套模块A:网络构建 & (五)网络运维配置+(六)SDN网络配置-命令配置

【赛题解析&命令脚本配置】2023年全国职业院校技能大赛GZ073网络系统管理赛项赛题第10套模块A:网络构建 & (五)网络运维配置+(六)SDN网络配置-命令配置

君逍遥o 810

SpringCache使用Ehcache做缓存

SpringCache简介 一个项目随着时间的积累, 数据规模会越来越大, 对数据的访问需求也越来越高, 数据库的查询等操作的使用量也会越来越大.但是数据库的性能是有上限的.因此如何提高数据查询的性能, 分担数据库的访问压力也成为了一个重要的功能. 而缓存就是实际工作中经常会被使用的一种分担数据库访问压力,提高数据查询性能的方法. 从3.1开始Spring引入了对Cache的支持。其使用方法和原理...

liangshui999的博客 651

使用 Simulink + Simscape Electrical 实现一个三相半控桥式整流器的仿真模型

本文详细介绍了使用Simulink搭建三相半控桥式整流器仿真模型的方法。主要内容包括系统结构设计、建模步骤(从创建项目到参数配置)、移相触发控制的实现,以及阻感负载下的动态响应分析。通过合理设置三相电源、整流器模块、触发控制和测量模块,实现了输出电压和电流波形的仿真观测。文章还提供了优化建议,如添加滤波器、实时控制等改进方向,为电力电子变换器的仿真研究和性能优化提供了实用指导。

amy_mhd的博客 140

EhCache初体验

一、简介   EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点。Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。     主要的特性有:  快速  简单  多种缓存策略 缓...

weixin_34306446的博客 954

电子电器架构刷写方案——General Flash Bootloader

### 文章大体有如下内容: ### 1、简介 ### 2、刷写系统概述 ### 3、Flash Bootloader刷写流程 ### 4、Flash Bootloader架构介绍 ### 5、关于刷写的思考

Soly_kun的博客 2211

【制作100个unity游戏之30】使用unity实现一款2d俯视角RPG战斗游戏,主要使用InputSystem+有限状态机+A*寻路实现(完结,附带项目源码)

【制作100个unity游戏之30】使用unity实现一款2d俯视角RPG战斗游戏,主要使用InputSystem+有限状态机+A*寻路实现

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 4653

Ehcache学习笔记

Ehcache是一种广泛使用的开源Java分布式缓存,主要面向通用缓存,Java EE和轻量级容器。本博文使用3.x版本。 maven包含依赖 <dependency> <groupId>org.ehcache</groupId> <artifactId&gt...

weixin_33812433的博客 263

双Orin PCIe RC&EP模式互通

orin pcie 互通

乐于分享 3877

springboot 整合ehcache+redis 高性能二级缓存

l2cache 开源项目 ehcache与redis集成,提供数据拦截器解决缓存穿透问题。同时,二级缓存大大降低了缓存雪崩和击穿的可能性。此外,还提供了redis超时键本地同步删除功能,缓解了redis与本地缓存的数据一致性问题。 开源项目github https://github.com/smallbeanteng/l2cache 项目开始 具体看使用例子。 Maven 依赖 <properties> <guava.version>28.1-jre</guava.ver

yeyuekuanglang2的博客 1784

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

Ehcache缓存的使用

缓存应该是每个系统都要考虑的架构,缓存不仅可以加速系统的访问速度还可以提升系统的性能。如我们需要经常访问的高频热点数据,如果把它缓存起来就能有效减少数据库服务器的压力。手机验证码等有一定的失效时间,我们就可以考虑使用缓存,等失效时间过了,就删掉验证码。

laizhenghua的博客 6443

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

如何在Spring Boot中使用EhCache缓存

在查询数据的时候,数据大多来自于数据库,我们会基于SQL语句与数据库交互,数据库一般会基于本地磁盘IO将数据读取到内存,返回给Java服务端,我们再将数据响应给前端,做数据展示。但是MySQL这种关系型数据库查询数据相对比较慢,因为有磁盘IO,或者是全盘扫描的风险,在针对一些热点数据时,会对MySQL造成比较大的压力,此时我们可以采用缓存的方式来解决。而缓存又分为很多种,相对服务端角度来说,可以采用Redis和JVM这两种方式。

weixin_73412838的博客 3750

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

spring EhCache缓存之annotation注解

spring + ehcache 注解使用实例 EhCache缓存之annotation注解

lemonzone2010的专栏 1万+

spring4配置基于注解的ehcache缓存

一、 ehcache配置文件ehcache.xml xml version="1.0" encoding="UTF-8"?> ehcache name="es">       diskStore path="java.io.tmpdir"/>       defaultCache         maxElementsInMemory="10000"  eterna

愿夜幕永不开启 5872

【Spring】Mybatis缓存+ehcache

基础缓存:Mybatis一级缓存、二级缓存 基于Spring的Mybatis缓存+ehcache 注解:@Cacheable、@CachePut、@CacheEvict、@Caching

m0_66584716的博客 1522

Spring框架如何做EhCache缓存?

本文以EhCache为例,来演示Spring框架如何做缓存。

熬过万丈孤独,藏下星辰大海 1137

在Spring上集成使用ehcache缓存框架以及实现集群缓存同步

周一第一天实习,老大给了个任务给我,让我看看j2cache两级缓存框架,然后我在开源中国了解到,开源中国目前就用到这个缓存框架,主要是为了减少redis的负荷,这个两级缓存框架使用了ehcache作为一级L1缓存,使用redis作为二级L2缓存,了解到这个之后,我果断学习了一波ehcache在spring的使用。一、依赖jar包这里使用maven:<properties> <spring.v

禤永豪 2858

ehcache和redis实现二级缓存

二级缓存的作用 简要说明:ehcache是内存缓存,在本地jvm内存中,十分高效,但是如果缓存数据都存在jvm中,内存是不够用的,于是使用到了redis数据库缓存,redis是键值对数据库,也比较高效,如果仅用redis做缓存,则存在频繁的网络IO读写,因为一般的会将redis部署在一个单独的服务器上,或者是集群部署。所以我们结合两者的特性,优先使用ehcache缓存,当ehcac...

JinXYan的博客 1362

ehcache缓存问题

步骤: 1.ehcache.xml ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"> diskStore path="java.io.tmpdir"/> defaultCache maxElementsInMemor

东天里的冬天 2123

Ultimate Vocal Remover v5.6 人声分离完整指南:十分钟装好,第一次就能出伴奏

想把一首歌里的人声和伴奏拆开?Ultimate Vocal Remover(UVR)v5.6 就是干这个的:免费开源、深度人声分离,基于深度神经网络把音源拆成人声、伴奏,甚至鼓、贝斯、其他等独立音轨。适合第一次接触分离工具的普通用户,也适合想做卡拉 OK 伴奏、翻录、混音素材的人。下面这条路径,装好、下模型、跑出第一对音轨,顺利的话十分钟以内。 [![Ultimate Vocal Remover

gitblog_00734的博客 1076

spring ehCache缓存

从 Spring 1.1.1 开始,EHCache 就作为一种通用缓存解决方案集成进 Spring。 我将示范拦截器的例子,它能把方法返回的结果缓存起来。 利用 Spring IoC 配置 EHCache 在 Spring 里配置 EHCache 很简单。你只需一个 ehcache.xml 文件,该文件用于配置 EHCache:

刘道平的专栏 837

spring整合ehcache和redis缓存

1.redis已在上一篇中集成,这里就直接上ehcache了 需要jar报:ehcache-2.10.5.jar ehcache-core-2.4.5.jar ehcache-spring-annotations-1.2.0.jar 2.配置ehcache.xml <?xml version="1.0" encoding="UTF-8"?> <ehcache dynamicConfig="false" monitoring="off" updateCheck="false"

qwer1154928613的博客 359
上一篇: Spring-ioc等概念(一)
下一篇: SpringBoot-结合thymeleaf和mybatis案例
gcc_front
博客等级 码龄9年 16粉丝 98原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值