스프링 세션 사용
이 섹션에서는 통합 과정을 안내합니다. NCache 효율적이고 확장 가능한 Spring 세션 관리를 위해 Spring Boot 애플리케이션에 통합하세요. 이러한 세션은 일반 세션 또는 인덱싱된 세션일 수 있습니다. 인덱싱된 Spring 세션의 경우 인증된 사용자의 주체 이름이 저장됩니다. NCache 꼬리표.
사전 조건
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<!--for NCache Enterprise-->
<artifactId>ncache-spring-session</artifactId>
<version>x.x.x</version>
</dependency>
- Java 클라이언트 애플리케이션에서 다음 패키지를 가져옵니다.
- import **클라이언트 이름**
- import **런타임**을 위한 com.alachisoft.ncache.*;
- import com.alachisoft.ncache.런타임.예외.*;
- 캐시가 실행 중이어야 합니다.
- 추가되는 데이터가 직렬화 가능.
- 지원되는 Java 버전에 대한 자세한 내용은 다음을 참조하십시오. NCache 설치 가이드.
- Spring Framework 버전은 6.0.12 이상.
스프링 세션 구현
Spring 세션을 연결하려면 애플리케이션에 어노테이션을 추가해야 합니다. NCache이렇게 하려면 메인 애플리케이션 클래스에 다음 어노테이션을 추가하십시오.
@SpringBootApplication // this is needed so that Spring can read the user created Beans and Configurations
또한 추가해야 할 사항도 있습니다. 한 구성을 추가하려면 다음 중 하나를 수행하세요.
@EnableNCacheHttpSession // uses the default configurations@EnableNCacheHttpSession(cacheName = "demoCache")
하지만 인덱싱된 세션을 사용하려면 다음을 사용해야 합니다. @EnableNCacheIndexedHttpSession 위에서 언급한 설정을 사용하여 구성할 수 있는 주석 기능입니다.
사용자 지정 설정이 필요한 경우 어노테이션에 인수를 전달해야 합니다. 이러한 사용자 지정 설정은 쉼표로 구분됩니다. 예: cacheName="demoCache", namespace="randomNamespace"다음과 같은 사항을 포함할 수 있습니다.
cacheName -> default: demoCachenamespace -> default: "spring:sessions:"maxInactiveInterval -> default: 1000 secondssaveMode -> default: ON_SAVEflushMode -> default: ON_SAVE
예를 들어, 세션을 사용하는 추측 게임이 있다면 다음과 같이 주석을 달 수 있습니다.
package com.alachisoft.ncache.guessgame;
import com.alachisoft.ncache.spring.sessions.config.annotation.web.http.EnableNCacheHttpSession;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
/*
annotation to get access to NCache for session storage. Pass arguments in the annotation if you want custom settings. You can pass:
1) cacheName -> default: demoCache
2) namespace -> default: "spring:sessions:"
3) maxInactiveInterval -> default: 1000 seconds
4) saveMode -> default: ON_SAVE
5) flushMode -> default: ON_SAVE
*/
@EnableNCacheHttpSession(cacheName = "spring")
public class GuessGameApplication {
public static void main(String[] args) {
SpringApplication.run(GuessGameApplication.class, args);
}
}