使用Spring缓存的简单Demo

简介:

使用Spring缓存的简单Demo

1. 首先创建Maven工程,在Pom中配置

1
2
3
4
5
6
7
8
9
10
         <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-core</artifactId>
     <version> 4.1 . 2 .RELEASE</version>
</dependency>
<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version> 4.1 . 2 .RELEASE</version>
</dependency>

2. 创建Student类和StudentServer 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package  my.testcache;
 
public  class  Student {
     private  String name;
     private  int  age;
     
     
     public  Student(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;
     }
     
     @Override
     public  String toString() {
         return  "Student [name="  + name +  ", age="  + age +  "]" ;
     }
     
     
     
}

  StudentServer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package  my.testcache;
 
import  java.util.HashMap;
import  java.util.Map;
 
import  org.springframework.cache.annotation.Cacheable;
 
public  class  StudentService {
     
     private  Map<Integer, Student> stus =  new  HashMap<Integer, Student>();
     {
         stus.put( 1 new  Student( "zhangsan" , 100 ));
         stus.put( 2 new  Student( "lisi" 106 ));
     };
     
     /**
      * 根据参数对返回值进行缓存
      */
     @Cacheable (value= "students" )
     public  Student getStudent( int  id){
         System.out.println( "getStudent: "  + id);
         return  stus.get(id);
     };
}

注意:getStudent方法要定义为Public才能使用缓存。

有条件的缓存 condition = "#id < 2",表示只缓存id<2的数据。

1
2
3
4
5
@Cacheable (value= "students" , condition =  "#id < 2" )
public  Student getStudent( int  id){
     System.out.println( "getStudent: "  + id);
     return  stus.get(id);
};

 

2. 在src/main/srsources中创建applicationContext.xml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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:cache= "http://www.springframework.org/schema/cache"
   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">
      
     <!-- 应用Bean类或者其方法的注解完成缓存的配置 -->
     <cache:annotation-driven/>
     <bean id= "studentServer"  class = "my.testcache.StudentService"  />
     
     <!-- cacheManager使用JDK中的ConcurrentMap作为存储器 -->
     <bean id= "cacheManager"  class = "org.springframework.cache.support.SimpleCacheManager"  >
         <property name= "caches" >
             <set>
                 <bean id= "students"  class = "org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" />
             </set>
         </property>
     </bean>
</beans>

 

注意: 此时命名的bean students要与 StudentServer中的getStudents 的注解alue (@Cacheable(value="students"))一致

 

4. 执行Main方法

1
2
3
4
5
6
7
8
9
10
public  static  void  main(String[] args) {
         ApplicationContext context =  new  ClassPathXmlApplicationContext( "applicationContext.xml" );
                                             
         StudentService studentService = context.getBean(StudentService. class );
         Student student1 = studentService.getStudent( 1 );
         System.out.println(student1);
         
         Student student2 = studentService.getStudent( 1 );
         System.out.println(student2);
     }

5. 显示结果

1
2
3
getStudent:  1
Student [name=zhangsan, age= 100 ]
Student [name=zhangsan, age= 100 ]

说明第二次调用使用了缓存。

 


本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/p/5893606.html,如需转载请自行联系原作者

目录
相关文章
|
11月前
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
1809 5
|
11月前
|
存储 缓存 Java
Spring中@Cacheable、@CacheEvict以及其他缓存相关注解的实用介绍
缓存是提升应用性能的重要技术,Spring框架提供了丰富的缓存注解,如`@Cacheable`、`@CacheEvict`等,帮助开发者简化缓存管理。本文介绍了如何在Spring中配置缓存管理器,使用缓存注解优化数据访问,并探讨了缓存的最佳实践,以提升系统响应速度与可扩展性。
454 0
Spring中@Cacheable、@CacheEvict以及其他缓存相关注解的实用介绍
|
缓存 NoSQL Java
什么是缓存?如何在 Spring Boot 中使用缓存框架
什么是缓存?如何在 Spring Boot 中使用缓存框架
1117 0
|
消息中间件 缓存 NoSQL
基于Spring Data Redis与RabbitMQ实现字符串缓存和计数功能(数据同步)
总的来说,借助Spring Data Redis和RabbitMQ,我们可以轻松实现字符串缓存和计数的功能。而关键的部分不过是一些"厨房的套路",一旦你掌握了这些套路,那么你就像厨师一样可以准备出一道道饕餮美食了。通过这种方式促进数据处理效率无疑将大大提高我们的生产力。
407 32
|
存储 缓存 NoSQL
Spring Cache缓存框架
Spring Cache是Spring体系下的标准化缓存框架,支持多种缓存(如Redis、EhCache、Caffeine),可独立或组合使用。其优势包括平滑迁移、注解与编程两种使用方式,以及高度解耦和灵活管理。通过动态代理实现缓存操作,适用于不同业务场景。
820 0
|
缓存 NoSQL Java
【Azure Redis 缓存】示例使用 redisson-spring-boot-starter 连接/使用 Azure Redis 服务
【Azure Redis 缓存】示例使用 redisson-spring-boot-starter 连接/使用 Azure Redis 服务
513 0
|
缓存 Java 开发工具
Spring是如何解决循环依赖的?从底层源码入手,详细解读Spring框架的三级缓存
三级缓存是Spring框架里,一个经典的技术点,它很好地解决了循环依赖的问题,也是很多面试中会被问到的问题,本文从源码入手,详细剖析Spring三级缓存的来龙去脉。
2396 24
Spring是如何解决循环依赖的?从底层源码入手,详细解读Spring框架的三级缓存
|
缓存 Java 数据库连接
深入探讨:Spring与MyBatis中的连接池与缓存机制
Spring 与 MyBatis 提供了强大的连接池和缓存机制,通过合理配置和使用这些机制,可以显著提升应用的性能和可扩展性。连接池通过复用数据库连接减少了连接创建和销毁的开销,而 MyBatis 的一级缓存和二级缓存则通过缓存查询结果减少了数据库访问次数。在实际应用中,结合具体的业务需求和系统架构,优化连接池和缓存的配置,是提升系统性能的重要手段。
604 4
|
缓存 NoSQL Java
Spring Boot中的分布式缓存方案
Spring Boot提供了简便的方式来集成和使用分布式缓存。通过Redis和Memcached等缓存方案,可以显著提升应用的性能和扩展性。合理配置和优化缓存策略,可以有效避免常见的缓存问题,保证系统的稳定性和高效运行。
540 3
|
存储 缓存 Java
Spring缓存注解【@Cacheable、@CachePut、@CacheEvict、@Caching、@CacheConfig】使用及注意事项
Spring缓存注解【@Cacheable、@CachePut、@CacheEvict、@Caching、@CacheConfig】使用及注意事项
5439 2