Spring實戰(zhàn)之緩存使用key操作示例
本文實例講述了Spring實戰(zhàn)之緩存使用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" />
<!-- 配置默認(rèn)的緩存區(qū) -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"/>
<!-- 配置名為users的緩存區(qū) -->
<cache name="users"
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="300"
timeToLiveSeconds="600" />
</ehcache>
三 領(lǐng)域模型
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 實現(xiàn)類
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("--正在執(zhí)行findUsersByNameAndAge()查詢方法--");
return new User(name, age);
}
public User getAnotherUser(String name, int age)
{
System.out.println("--正在執(zhí)行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);
// 第一次調(diào)用us對象的方法時會執(zhí)行該方法,并緩存方法的結(jié)果
User u1 = us.getUsersByNameAndAge("孫悟空", 500);
// 指定使用name作為緩存key,因此主要兩次調(diào)用方法的name參數(shù)相同
// 緩存機(jī)制就會生效
User u2 = us.getAnotherUser("孫悟空", 400);
System.out.println(u1 == u2); // 輸出true
}
}
六 測試結(jié)果
--正在執(zhí)行findUsersByNameAndAge()查詢方法--
true
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
- Spring實戰(zhàn)之方法級別緩存用法示例
- Spring實戰(zhàn)之緩存使用condition操作示例
- Spring實戰(zhàn)之類級別緩存實現(xiàn)與使用方法
- Spring Boot集成Ehcache緩存解決方式
- spring結(jié)合redis如何實現(xiàn)數(shù)據(jù)的緩存
- Redis整合Spring結(jié)合使用緩存實例
- 詳解Spring緩存注解@Cacheable,@CachePut , @CacheEvict使用
- Spring Boot 基于注解的 Redis 緩存使用詳解
- SpringBoot項目中使用redis緩存的方法步驟
- Spring Boot 中使用cache緩存的方法
- Spring實戰(zhàn)之清除緩存操作示例
相關(guān)文章
SpringBoot+MyBatis簡單數(shù)據(jù)訪問應(yīng)用的實例代碼
這篇文章主要介紹了SpringBoot+MyBatis簡單數(shù)據(jù)訪問應(yīng)用的實例代碼,需要的朋友可以參考下2017-05-05
SpringCloud Feign傳遞HttpServletRequest對象流程
HttpServletRequest接口的對象代表客戶端的請求,當(dāng)客戶端通過HTTP協(xié)議訪問Tomcat服務(wù)器時,HTTP請求中的所有信息都封裝在HttpServletRequest接口的對象中,這篇文章介紹了Feign傳遞HttpServletRequest對象的流程,感興趣的同學(xué)可以參考下文2023-05-05
在Java中關(guān)閉SQL執(zhí)行日志來優(yōu)化服務(wù)器性能
Java應(yīng)用程序中,數(shù)據(jù)庫操作是一個常見的任務(wù),如果不適當(dāng)?shù)靥幚鞸QL執(zhí)行日志,可能會導(dǎo)致不必要的性能損失,SQL執(zhí)行日志通常由數(shù)據(jù)庫連接池、ORM框架(如Hibernate、MyBatis)、或者應(yīng)用服務(wù)器的內(nèi)置日志機(jī)制生成,本文將探討如何在Java中關(guān)閉SQL執(zhí)行日志,提升應(yīng)用性能和效率2024-11-11
如何獲取springboot打成jar后的classpath
這篇文章主要介紹了如何獲取springboot打成jar后的classpath問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過程詳解
這篇文章主要介紹了SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12

