Spring实战之缓存使用key操作示例

本文实例讲述了Spring实战之缓存使用key操作。分享给大家供大家参考,具体如下:

一 配置文件

<?xml version="1.0" encoding="GBK"?>
<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:cache="http://www.springframework.org/schema/cache"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/cache
   http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-4.0.xsd">
   <context:component-scan
      base-package="org.crazyit.app.service" />
   <cache:annotation-driven
      cache-manager="cacheManager" />
   <!-- 配置EhCache的CacheManager 通过configLocation指定ehcache.xml文件的位置 -->
   <bean id="ehCacheManager"
      class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
      p:configLocation="classpath:ehcache.xml" p:shared="false" />
   <!-- 配置基于EhCache的缓存管理器 并将EhCache的CacheManager注入该缓存管理器Bean -->
   <bean id="cacheManager"
      class="org.springframework.cache.ehcache.EhCacheCacheManager"
      p:cacheManager-ref="ehCacheManager">
   </bean>
</beans>

二 属性文件

<?xml version="1.0" encoding="gbk"?>
<ehcache>
  <diskStore path="java.io.tmpdir" />
   <!-- 配置默认的缓存区 -->
  <defaultCache
    maxElementsInMemory="10000"
    eternal="false"
    timeToIdleSeconds="120"
    timeToLiveSeconds="120"
    maxElementsOnDisk="10000000"
    diskExpiryThreadIntervalSeconds="120"
    memoryStoreEvictionPolicy="LRU"/>
   <!-- 配置名为users的缓存区 -->
  <cache name="users"
    maxElementsInMemory="10000"
    eternal="false"
    overflowToDisk="true"
    timeToIdleSeconds="300"
    timeToLiveSeconds="600" />
</ehcache>

三 领域模型

package org.crazyit.app.domain;
public class User
{
   private String name;
   private int age;
   public User()
   {}
   public User(String name, int age)
   {
      super();
      this.name = name;
      this.age = age;
   }
   public String getName()
   {
      return name;
   }
   public void setName(String name)
   {
      this.name = name;
   }
   public int getAge()
   {
      return age;
   }
   public void setAge(int age)
   {
      this.age = age;
   }
}

四 Service

1 接口类

package org.crazyit.app.service;
import org.crazyit.app.domain.User;
public interface UserService
{
   User getUsersByNameAndAge(String name, int age);
   User getAnotherUser(String name, int age);
}

2 实现类

package org.crazyit.app.service.impl;
import org.crazyit.app.service.UserService;
import org.crazyit.app.domain.User;
import org.springframework.stereotype.Service;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Scope;
@Service("userService")
@Cacheable(value = "users" , key="#name")
public class UserServiceImpl implements UserService
{
  public User getUsersByNameAndAge(String name, int age)
  {
    System.out.println("--正在执行findUsersByNameAndAge()查询方法--");
    return new User(name, age);
  }
  public User getAnotherUser(String name, int age)
  {
    System.out.println("--正在执行findAnotherUser()查询方法--");
    return new User(name, age);
  }
}

五 测试类

package lee;
import org.crazyit.app.service.UserService;
import org.crazyit.app.domain.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest
{
  public static void main(String[] args)
  {
    ApplicationContext ctx =
      new ClassPathXmlApplicationContext("beans.xml");
    UserService us = ctx.getBean("userService" , UserService.class);
    // 第一次调用us对象的方法时会执行该方法,并缓存方法的结果
    User u1 = us.getUsersByNameAndAge("孙悟空", 500);
    // 指定使用name作为缓存key,因此主要两次调用方法的name参数相同
    // 缓存机制就会生效
    User u2 = us.getAnotherUser("孙悟空", 400);
    System.out.println(u1 == u2); // 输出true
  }
}

六 测试结果

--正在执行findUsersByNameAndAge()查询方法--
true

更多关于java相关内容感兴趣的读者可查看本站专题:《Spring框架入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。

(0)

相关推荐

  • 详解redis与spring的整合(使用缓存)

    1.实现目标 通过redis缓存数据.(目的不是加快查询的速度,而是减少数据库的负担) 2.所需jar包 注意:jdies和commons-pool两个jar的版本是有对应关系的,注意引入jar包是要配对使用,否则将会报错.因为commons-pooljar的目录根据版本的变化,目录结构会变.前面的版本是org.apache.pool,而后面的版本是org.apache.pool2... 3.redis简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的val

  • spring整合redis缓存并以注解(@Cacheable、@CachePut、@CacheEvict)形式使用

    maven项目中在pom.xml中依赖2个jar包,其他的spring的jar包省略: <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>org.springfra

  • Spring Boot 中使用cache缓存的方法

    一.什么是缓存 Cache Cache 一词最早来自于CPU设计 当CPU要读取一个数据时,首先从CPU缓存中查找,找到就立即读取并送给CPU处理:没有找到,就从速率相对较慢的内存中读取并送给CPU处理,同时把这个数据所在的数据块调入缓存中,可以使得以后对整块数据的读取都从缓存中进行,不必再调用内存.正是这样的读取机制使CPU读取缓存的命中率非常高(大多数CPU可达90%左右),也就是说CPU下一次要读取的数据90%都在CPU缓存中,只有大约10%需要从内存读取.这大大节省了CPU直接读取内存的

  • Spring Boot Web 静态文件缓存处理的方法

    采用Spring Boot + Freemarker开发Web项目时,由于一些静态文件比较大,如果是在PC上访问影响不大,当在手机上访问时,特别是用流量访问时速度会慢很多,而且很耗流量. 通过对请求进行抓包,可以发现每次进入一个页面都需要加载静态文件,如果不差钱的公司可以将静态文件放在CDN上来加快访问速度,或者用Nginx来做静态文件的缓存. 今天给大家介绍一种其他的缓存优化方式,通过Spring的缓存机制来缓存静态文件,在Spring Boot中配置静态文件缓存只需要在配置文件中加入下面的配

  • Spring Boot 基于注解的 Redis 缓存使用详解

    看文本之前,请先确定你看过上一篇文章<Spring Boot Redis 集成配置>并保证 Redis 集成后正常可用,因为本文是基于上文继续增加的代码. 一.创建 Caching 配置类 RedisKeys.Java package com.shanhy.example.redis; import java.util.HashMap; import java.util.Map; import javax.annotation.PostConstruct; import org.springf

  • 详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用

    注释介绍 @Cacheable @Cacheable 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 @Cacheable 作用和配置方法 参数 解释 example value 缓存的名称,在 spring 配置文件中定义,必须指定至少一个 例如: @Cacheable(value="mycache") @Cacheable(value={"cache1","cache2"} key 缓存的 key,可以为空,如果指定要按照

  • 详解Spring MVC 集成EHCache缓存

    废话少说,直接上代码: ehcache.xml 文件 <?xml version="1.0" encoding="UTF-8"?> <ehcache dynamicConfig="false" monitoring="off" updateCheck="false" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  • Spring实战之类级别缓存实现与使用方法

    本文实例讲述了Spring实战之类级别缓存实现与使用方法.分享给大家供大家参考,具体如下: 一 配置文件 <?xml version="1.0" encoding="GBK"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml

  • spring boot+spring cache实现两级缓存(redis+caffeine)

    spring boot中集成了spring cache,并有多种缓存方式的实现,如:Redis.Caffeine.JCache.EhCache等等.但如果只用一种缓存,要么会有较大的网络消耗(如Redis),要么就是内存占用太大(如Caffeine这种应用内存缓存).在很多场景下,可以结合起来实现一.二级缓存的方式,能够很大程度提高应用的处理效率. 内容说明: 缓存.两级缓存 spring cache:主要包含spring cache定义的接口方法说明和注解中的属性说明 spring boot

  • Spring实战之缓存使用key操作示例

    本文实例讲述了Spring实战之缓存使用key操作.分享给大家供大家参考,具体如下: 一 配置文件 <?xml version="1.0" encoding="GBK"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:

  • Spring实战之缓存使用condition操作示例

    本文实例讲述了Spring实战之缓存使用condition操作.分享给大家供大家参考,具体如下: 一 配置文件 <?xml version="1.0" encoding="GBK"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  • Spring实战之注入集合值操作示例

    本文实例讲述了Spring实战之注入集合值操作.分享给大家供大家参考,具体如下: 一 配置 <?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schema

  • Spring实战之注入嵌套Bean操作示例

    本文实例讲述了Spring实战之注入嵌套Bean操作.分享给大家供大家参考,具体如下: 一 配置 <?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:sch

  • Spring实战之搜索Bean类操作示例

    本文实例讲述了Spring实战之搜索Bean类操作.分享给大家供大家参考,具体如下: 一 配置文件 <?xml version="1.0" encoding="GBK"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:

  • Spring实战之容器后处理器操作示例

    本文实例讲述了Spring实战之容器后处理器.分享给大家供大家参考,具体如下: 一 配置文件 <?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p=&

  • Spring Cache+Redis缓存数据的实现示例

    目录 1.为什么使用缓存 2.常用的缓存注解 2.1 @Cacheable 2.2 @CacheEvict 2.3.@Cacheput 2.4.@Caching 2.5.@CacheConfig 3.SpringBoot缓存支持 4.项目继承Spring Cache+Redis 4.1 添加依赖 4.2 配置类 4.3 添加redis配置 4.4 接口中使用缓存注解 4.5 缓存效果测试 1.为什么使用缓存   我们知道内存的读取速度远大于硬盘的读取速度.当需要重复地获取相同数据时,一次一次地请

  • Spring Boot之内嵌tomcat版本升级操作示例

    目录 Spring Boot之如何升级内嵌tomcat版本 1. 背景 修复方案如下: 2. 过程 2.1 升级单模块项目的tomcat版本 2.2 升级包含多个模块的项目 3. 项目有打包子模块如何升级 尾声 Spring Boot之如何升级内嵌tomcat版本 1. 背景 根据信息安全运营团队发布的Tomcat-AJP协议漏洞风险预警,Tomcat的AJP协议存在高危漏洞(默认8009端口)由于存在实现缺陷导致相关参数可控,攻击者利用该漏洞可通过构造特定参数,读取服务器webapp 下的任意

  • Spring实战之ServletContextResource访问资源文件示例

    本文实例讲述了Spring实战之ServletContextResource访问资源文件.分享给大家供大家参考,具体如下: 一 视图 <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@ page import="org.springframework.web.context.support.ServletCo

  • node Buffer缓存区常见操作示例

    本文实例讲述了node Buffer缓存区常见操作.分享给大家供大家参考,具体如下: 创建buffer类 var buf=new buffer(10); var buf=new buffer([10,20,30,40]); var buf=new buffer("www.baidu.com","utf-8"); 写入缓存区 buf.write(string[,offset[,length]][encoding]) buf=new buffer(256); len=b

随机推荐