`
mqiqe
  • 浏览: 22641 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring cache

阅读更多

ehcache.xml 配置

<?xml version="1.0" encoding="UTF-8"?>

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"

monitoring="autodetect">

<!-- <diskStore path="java.io.tmpdir" /> -->

<diskStore path="/cachetmpdir" />

<defaultCache maxElementsInMemory="10000" eternal="false"

timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"

maxElementsOnDisk="10000000" diskPersistent="false"

diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />

<cache name="andCache" maxElementsInMemory="10000"

maxElementsOnDisk="1000" eternal="false" overflowToDisk="true"

diskSpoolBufferSizeMB="20" timeToIdleSeconds="300" timeToLiveSeconds="600"

memoryStoreEvictionPolicy="LFU" />

</ehcache>  

 

applicationContext.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:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"

xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:tool="http://www.springframework.org/schema/tool" xmlns:cache="http://www.springframework.org/schema/cache"

xsi:schemaLocation="

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd

http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd

http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool-3.1.xsd

http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd"

default-lazy-init="true">

<cache:annotation-driven cache-manager="cacheManager" />

<!-- cacheManager工厂类 -->

<bean id="cacheManagerFactory"

class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"

p:configLocation="classpath:ehcache.xml" />

 

<!-- 声明cacheManager -->

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"

p:cacheManager-ref="cacheManagerFactory" />

</beans>

 

CacheDemoService.java

package com.piaoyi.function.demo.service;

 

import org.springframework.cache.annotation.CacheEvict;

import org.springframework.cache.annotation.Cacheable;

import org.springframework.stereotype.Service;



@Service

public class CacheDemoService{

  @Cacheable(value="andCache",key="#userId + 'CacheDemoService.userId'")

public void save(String userId){

System.out.println("save cache |" +userId +"| CacheDemoService.userId");

}

@CacheEvict(value="andCache",key="#userId + 'CacheDemoService.userId'")

public void flush(String userId){

System.out.println("flush cache |" +userId +"| CacheDemoService.userId");

}

@CacheEvict(value="andCache",allEntries=true)

public void flushAll(){

System.out.println("flush cache all");

}

}

 

DemoTest.java

package com.piaoyi.function.demo;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import com.piaoyi.function.demo.service.DemoService;

 

public class DemoTest {

 

static String context = null;

static ApplicationContext applicationContext;

static{  

        context = "applicationContext.xml";//ehcache简单配置(命名空间)   

 

        applicationContext = new ClassPathXmlApplicationContext(context);  

    }  

 

/**

* 描述 : <描述函数实现的功能>. <br>

* <p>

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

CacheDemoService service =(DemoService)applicationContext.getBean("cacheDemoService");  

 

service.save("123");//打印

service.save("aaa");//打印

service.save("123");//不打印

service.save("aaa");//不打印

service.flush("123");//打印

service.flush("aaa");//打印

service.flush("aaa");//不打印

service.flushAll();//打印

}

 

}


分享到:
评论

相关推荐

    springboot 使用spring cache缓存 和 使用fastjson配置redis系列化

    在Spring Boot应用中,Spring Cache是一个强大的工具,用于在应用程序中实现缓存抽象,它可以减少对数据库或远程服务的重复调用,从而提高性能。在本篇文档中,我们将探讨如何使用Spring Cache来缓存数据,并结合...

    SSM与memcached整合项目Spring Cache

    在本项目中,我们主要探讨的是如何将Spring Cache与memcached进行整合,以提升应用程序的性能和效率。Spring Cache是Spring框架的一部分,它提供了一种抽象的缓存管理机制,可以方便地集成到各种缓存解决方案中,如...

    springcache+redis springboot maven

    在这个项目中,"springcache+redis"的整合意味着我们要利用Spring Cache的特性,将缓存存储在Redis中,以提升应用的性能。 首先,Spring Cache提供了`@Cacheable`、`@CacheEvict`和`@Caching`等注解,允许我们在...

    SpringCache与redis集成,优雅的缓存解决方案.docx

    SpringCache与Redis集成,优雅的缓存解决方案 SpringCache是一种基于Java的缓存解决方案,它可以与Redis集成,提供了一种优雅的缓存解决方案。在本文中,我们将对SpringCache与Redis集成的优雅缓存解决方案进行详细...

    redis与springcache集成

    Redis是一款高性能的键值对数据存储系统,常用于构建分布式缓存系统,而Spring Cache是Spring框架提供的一个抽象层,允许我们使用不同的缓存技术,如Redis, EhCache等。在Spring Boot应用中,集成Redis作为缓存机制...

    Redis整合SpringCache实例

    **Redis整合SpringCache实例** 在现代的Web应用中,数据缓存是提高系统性能的关键技术之一。本示例主要探讨如何将开源的内存数据结构存储系统Redis与Spring Cache框架结合,实现高效的分布式缓存解决方案。Redis以...

    springCache

    Spring Cache 是 Spring 框架的一个重要组成部分,它提供了一种在应用程序中统一管理缓存的能力,无需依赖特定的缓存实现,如 Ehcache、Redis 或 Hibernate 二级缓存等。通过 Spring Cache,开发者可以方便地在方法...

    spring cache + redis 主从

    此外,还需要掌握如何将Spring Cache与Redis整合,以便在应用中高效使用缓存机制。 一、Redis的主从配置 1. 准备工作: - 操作系统要求:Ubuntu 16.04。 - Redis版本:选择适合的稳定版本,例如redis-4.0.9.tar....

    spring-cache(通过key值更新缓存)

    Spring Cache是一个抽象层,它允许开发者在不关注具体缓存实现的情况下,轻松地在应用程序中添加缓存功能。本篇文章将详细探讨如何通过key值更新Spring Cache中的指定缓存,以及相关的缓存管理策略。 首先,让我们...

    JAVA编程之spring cache本机缓存应用

    1、SpringCache是Spring提供的一个缓存框架,在Spring3.1版本开始支持将缓存添加到现有的spring应用程序中,在4.1开始,缓存已支持JSR-107注释和更多自定义的选项 2、Spring Cache利用了AOP,实现了基于注解的缓存...

    springboot使用springCache和不使用springCache的对比.zip

    在Spring Boot应用中,Spring Cache是一个强大的功能,它允许我们以声明式的方式管理应用程序的缓存,从而提高性能。Spring Cache抽象了缓存提供者的具体实现,如 EhCache、Redis 或 Hazelcast,使得开发者可以方便...

    SpringCache缓存初探共5页.pdf.zip

    SpringCache是Spring框架提供的一种轻量级的缓存解决方案,旨在简化在应用程序中集成缓存的能力,以提高性能和响应速度。在这个“SpringCache缓存初探共5页.pdf.zip”压缩包中,很可能是对SpringCache的基础知识进行...

    Spring Cache 复合缓存管理器

    在Spring框架中,Spring Cache是用于提供统一的缓存抽象层的一个重要组件,它使得开发者能够在不修改代码的情况下,方便地在应用中引入缓存机制,以提高性能和响应速度。"Spring Cache 复合缓存管理器"指的是通过...

    springboot 使用spring cache缓存 和 缓存数据落地到redis

    【Spring Boot 使用 Spring Cache 缓存与数据落地到 Redis】\n\n在Spring Boot应用中,Spring Cache是一个强大的工具,可以极大地提升应用的性能,通过缓存非计算性或者昂贵的计算结果。Spring Cache抽象了缓存管理...

    springcache-1.1.0.zip

    《SpringCache深度解析与实战应用》 在Java开发领域,Spring框架因其强大的功能和灵活性而备受推崇。SpringCache是Spring框架的一部分,它提供了一种在应用程序中统一管理和使用缓存的解决方案,使得开发者能够轻松...

    spring cache

    spring cacke spring cacke

    redis-string-springcache.zip

    《SpringCache与Redis结合在Mybatis中的应用》 在当今的Web开发中,缓存技术是提高系统性能、减轻数据库压力的重要手段。本教程将详细探讨如何将SpringCache与Redis集成,结合Mybatis进行高效的数据缓存。我们将从...

    springboot_springcache_redis入门实例

    SpringBoot、SpringCache和Redis是Java开发中常用的三大技术组件,它们在构建高效、可扩展的应用程序中扮演着重要角色。让我们深入探讨一下这三个技术及其整合使用的入门实例。 SpringBoot是由Pivotal团队开发的...

Global site tag (gtag.js) - Google Analytics