缓存级别 java_Spring实战之方法级别缓存用法示例

本文实例讲述了Spring实战之方法级别缓存用法。分享给大家供大家参考,具体如下:

一 配置文件

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">

base-package="org.crazyit.app.service" />

cache-manager="cacheManager" />

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

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

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

p:cacheManager-ref="ehCacheManager">

二 属性文件

maxElementsInMemory="10000"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

maxElementsOnDisk="10000000"

diskExpiryThreadIntervalSeconds="120"

memoryStoreEvictionPolicy="LRU"/>

maxElementsInMemory="10000"

eternal="false"

overflowToDisk="true"

timeToIdleSeconds="300"

timeToLiveSeconds="600" />

maxElementsInMemory="10000"

eternal="false"

overflowToDisk="true"

timeToIdleSeconds="300"

timeToLiveSeconds="600" />

三 领域模型

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")

public class UserServiceImpl implements UserService

{

@Cacheable(value = "users1")

public User getUsersByNameAndAge(String name, int age)

{

System.out.println("--正在执行findUsersByNameAndAge()查询方法--");

return new User(name, age);

}

@Cacheable(value = "users2")

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);

// 由于getAnotherUser()方法使用另一个缓存区,

// 因此无法使用getUsersByNameAndAge()方法缓存区的数据。

User u2 = us.getAnotherUser("孙悟空", 500);

System.out.println(u1 == u2); // 输出false

// getAnotherUser("孙悟空", 500)已经执行过一次,故下面代码使用缓存

User u3 = us.getAnotherUser("孙悟空", 500);

System.out.println(u2 == u3); // 输出true

}

}

六 测试结果

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

--正在执行findAnotherUser()查询方法--

false

true

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值