찾다
Javajava지도 시간Spring 4.0 Annotation Cache+Redis 캐시 구성 사용 예시 공유

본 글은 주로 Spring 4.0 Annotation Cache + Redis 캐시 사용의 세부 구성을 소개하는데, 관심 있는 친구들은 참고하시기 바랍니다.

서문:

현재 이전 기술 아키텍처에서는 회사 프로젝트용으로 Redis가 구축되어 있는데, Redis는 세션 관리용으로만 사용하고, Background Object Cache는 사용하지 않습니다

1. redis와 ehcache의 차이점:

간단히 이해한 후 개인적으로 배포 관점에서 ehcache는 각 컴퓨터에 있는 반면 Redis는 분산 배포에 더 적합하다고 생각합니다. 캐싱을 위해 애플리케이션 서버에 메모리를 할당해야 합니다. Redis는 캐싱을 고려할 필요가 없습니다.

2. spring mvc+maven+mybaits+redis), 여기서는 Spring 통합 redis에 대해서만 설명합니다:

a. pom.xml 파일 구성(maven 관리 프로젝트가 아닌 경우 2개의 jar 다운로드) )

<!-- redis cache related.....start --> 
    <dependency> 
      <groupId>org.springframework.data</groupId> 
      <artifactId>spring-data-redis</artifactId> 
      <version>1.6.0.RELEASE</version> 
    </dependency> 
    <dependency> 
      <groupId>redis.clients</groupId> 
      <artifactId>jedis</artifactId> 
      <version>2.7.3</version> 
    </dependency> 
    <!-- redis cache related.....end -->

b. applicationContext.xml 파일 구성

먼저

xmlns:cache="http://www.springframework.org/schema/cache" 
xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd"

Spring에서 redis 구성 로드

<!-- ******************** redis缓存  **********************--> 
<!-- 注解一定要配置,不然不起作用 --> 
<cache:annotation-driven /> 
 
<!-- jedis 配置 --> 
  <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> 
    <property name="maxIdle" value="${redis.maxIdle}" /> 
    <!--<property name="maxWaitMillis" value="${redis.maxWait}" />--> 
    <property name="testOnBorrow" value="${redis.testOnBorrow}" /> 
  </bean> 
 
  <!-- redis服务器中心 --> 
  <bean id="connectionFactory" 
    class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> 
    <property name="poolConfig" ref="poolConfig" /> 
    <property name="port" value="${redis.port}" /> 
    <property name="hostName" value="${redis.hostname}" /> 
    <!-- <property name="password" value="${redis.password}" /> --> 
    <property name="timeout" value="${redis.timeout}"></property> 
  </bean> 
   
  <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> 
    <property name="connectionFactory" ref="connectionFactory" /> 
    <property name="keySerializer"> 
      <bean 
        class="org.springframework.data.redis.serializer.StringRedisSerializer" /> 
    </property> 
    <property name="valueSerializer"> 
      <bean 
        class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> 
    </property> 
  </bean> 
  <!-- 配置缓存 --> 
  <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"> 
    <constructor-arg ref="redisTemplate" /> 
  </bean> 
   
<!-- ******************** redis缓存  **********************-->

c. application.properties 리소스 파일 구성

#redis config 
#redis.hostname=192.168.242.131  
redis.hostname=localhost 
redis.port=6379  
redis.timeout=2000 
redis.usePool=true 
redis.default.db=0 
#\u6700\u5927\u5206\u914D\u7684\u5BF9\u8C61\u6570   
redis.maxTotal=600 
#\u6700\u5927\u80FD\u591F\u4FDD\u6301idel\u72B6\u6001\u7684\u5BF9\u8C61\u6570  
redis.maxIdle=300  
#\u591A\u957F\u65F6\u95F4\u68C0\u67E5\u4E00\u6B21\u8FDE\u63A5\u6C60\u4E2D\u7A7A\u95F2\u7684\u8FDE\u63A5 
redis.timeBetweenEvictionRunsMillis=30000  
#\u7A7A\u95F2\u8FDE\u63A5\u591A\u957F\u65F6\u95F4\u540E\u4F1A\u88AB\u6536\u56DE 
redis.minEvictableIdleTimeMillis=30000  
#\u5F53\u8C03\u7528borrow Object\u65B9\u6CD5\u65F6\uFF0C\u662F\u5426\u8FDB\u884C\u6709\u6548\u6027\u68C0\u67E5  
redis.testOnBorrow=true  
########reids\u7F16\u7801\u683C\u5F0F 
redis.encode=utf-8 
######\u7F13\u5B58\u8FC7\u671F\u65F6\u95F4 \u79D2 1000*60*60*24*7 \u4E03\u5929 
redis.expire=604800000 
####\u662F\u5426\u5F00\u542FRedis\u670D\u52A1\u5E94\u7528 
redis.unlock=false

3.

@Service("testService") 
public class TestServiceImpl implements ITestService { 
   
  @Resource 
  private ITestDao testDao; 
 
  @Cacheable(value="testId",key="&#39;id_&#39;+#id") 
  public Test getTestById(int id) { 
    return this.testDao.getObjById(id); 
  } 
   
  @CacheEvict(value="testId",key="&#39;id_&#39;+#id") 
  public void removeTestById(int id) { 
     
  } 
}

테스트 결과:

처음으로 서비스 방법 입력

두 번째로 서비스 방식을 입력하지 않아도 값을 얻었습니다

주의: 어떤 친구들은 접속을 시작할 때 오류 방지에 대해 물어볼 것입니다. win32/win64 버전을 다운로드하세요. 예, 시작하고 다시 액세스하면 오류가 보고되지 않습니다.

위 내용은 Spring 4.0 Annotation Cache+Redis 캐시 구성 사용 예시 공유의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

VSCode Windows 64비트 다운로드

VSCode Windows 64비트 다운로드

Microsoft에서 출시한 강력한 무료 IDE 편집기

맨티스BT

맨티스BT

Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.

ZendStudio 13.5.1 맥

ZendStudio 13.5.1 맥

강력한 PHP 통합 개발 환경

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구

MinGW - Windows용 미니멀리스트 GNU

MinGW - Windows용 미니멀리스트 GNU

이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.