springboot集成redis缓存

19 篇文章 0 订阅
11 篇文章 0 订阅

1.按照前面的文章“springboot连接redis集群”搭建项目

2.在集群配置类RedisClusterConfig.java中添加@EnableCaching注解

3.编写services服务类UserServices.java,模拟数据查询

package com.szcatic.service;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserServices {
	
	@Cacheable(value="pwd", key = "1000")
	public String getPassword() {
		System.out.println("没有缓存时读取数据");
		return "1234";
	}
}

4.编写测试类UserServicesTest.java

package com.szcatic.test;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import com.szcatic.Application;
import com.szcatic.service.UserServices;

@Configuration
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Application.class)
public class UserServicesTest {
	
	@Autowired
	private UserServices service;
	
	@Test
	void testGetPassword() {
		System.out.println("第一次读取数据");
		System.out.println("pwd======" + service.getPassword());
		System.out.println("第二次读取数据");
		System.out.println("pwd======" + service.getPassword());
	}
}

5.运行测试方法testGetPassword()测试结果,控制台输出内容

10:20:29.035 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
10:20:29.075 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
10:20:29.109 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.szcatic.test.UserServicesTest] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
10:20:29.169 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.szcatic.test.UserServicesTest], using SpringBootContextLoader
10:20:29.193 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.szcatic.test.UserServicesTest]: class path resource [com/szcatic/test/UserServicesTest-context.xml] does not exist
10:20:29.194 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.szcatic.test.UserServicesTest]: class path resource [com/szcatic/test/UserServicesTestContext.groovy] does not exist
10:20:29.194 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.szcatic.test.UserServicesTest]: no resource found for suffixes {-context.xml, Context.groovy}.
10:20:29.325 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.szcatic.test.UserServicesTest]
10:20:29.591 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.szcatic.test.UserServicesTest]: using defaults.
10:20:29.592 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
10:20:29.682 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@37f1104d, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@55740540, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@60015ef5, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@2f54a33d, org.springframework.test.context.support.DirtiesContextTestExecutionListener@1018bde2, org.springframework.test.context.transaction.TransactionalTestExecutionListener@65b3f4a4, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@f2ff811, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@568ff82, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@50caa560, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@2a266d09, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@5ab9e72c, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@186f8716]
10:20:29.689 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@69fb6037 testClass = UserServicesTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@36d585c testClass = UserServicesTest, locations = '{}', classes = '{class com.szcatic.Application, class com.szcatic.Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6a01e23, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@7995092a, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@3c72f59f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@2ddc8ecb], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null].
10:20:29.751 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=-1}

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.0.RELEASE)

2018-11-28 10:20:30.523  INFO 15048 --- [           main] com.szcatic.test.UserServicesTest        : Starting UserServicesTest on zsx with PID 15048 (started by admin in F:\workspace4.7\springboot)
2018-11-28 10:20:30.525  INFO 15048 --- [           main] com.szcatic.test.UserServicesTest        : No active profile set, falling back to default profiles: default
2018-11-28 10:20:33.523  INFO 15048 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2018-11-28 10:20:33.527  INFO 15048 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2018-11-28 10:20:33.605  INFO 15048 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 40ms. Found 0 repository interfaces.
2018-11-28 10:20:34.424  INFO 15048 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$6cc8798] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-11-28 10:20:36.622  INFO 15048 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2018-11-28 10:20:37.637  INFO 15048 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService
2018-11-28 10:20:38.168  INFO 15048 --- [           main] com.szcatic.test.UserServicesTest        : Started UserServicesTest in 8.39 seconds (JVM running for 10.563)
第一次读取数据
2018-11-28 10:20:39.033  INFO 15048 --- [           main] io.lettuce.core.EpollProvider            : Starting without optional epoll library
2018-11-28 10:20:39.039  INFO 15048 --- [           main] io.lettuce.core.KqueueProvider           : Starting without optional kqueue library
没有缓存时读取数据
pwd======1234
第二次读取数据
pwd======1234
2018-11-28 10:20:40.510  INFO 15048 --- [       Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService
2018-11-28 10:20:40.513  INFO 15048 --- [       Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

从以上结果可以看出,集成redis缓存成功 

6.项目结构

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值