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

网友投稿 260 2022-12-16


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

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

一 配置文件

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

xmlns:p="http://springframework.org/schema/p"

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/cache

http://springframework.org/schema/cSUzzIache/spring-cache-4.0.xsd

http://springframework.org/schema/context

http://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">

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

xmlns:p="http://springframework.org/schema/p"

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

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

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/cache

http://springframework.org/schema/cSUzzIache/spring-cache-4.0.xsd

http://springframework.org/schema/context

http://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">

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

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

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

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

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

p:cacheManager-ref="ehCacheManager">

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

三 领域模型

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(Strihttp://ng 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程序设计有所帮助。

maxElementsInMemory="10000"

eternal="false"

timeToIdleSeconds="120"

timeToLiveSeconds="120"

maxElementsOnDisk="10000000"

diskExpiryThreadIntervalSeconds="120"

memoryStoreEvictionPolicy="LRU"/>

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(Strihttp://ng 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程序设计有所帮助。

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(Strihttp://ng 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程序设计有所帮助。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringCloud Eureka搭建的方法步骤
下一篇:Java使用DateTimeFormatter格式化输入的日期时间
相关文章

 发表评论

暂时没有评论,来抢沙发吧~