Nats


NATS Key/Value 스토리지 드라이버입니다.

참고: Go 1.20 이상이 필요합니다

Table of Contents

  • Signatures

  • Installation

  • Examples

  • Config

  • Default Config

Signatures

func New(config ...Config) Storage

func (s *Storage) Get(key string) ([]byte, error)

func (s *Storage) Set(key string, val []byte, exp time.Duration) error

func (s *Storage) Delete(key string) error 

func (s *Storage) Reset() error

func (s *Storage) Close() error

func (s *Storage) Conn() (*nats.Conn, jetstream.KeyValue)

func (s *Storage) Keys() ([]string, error)

Installation

NATS Key/Value Store 드라이버는 모듈을 지원하는 최근 2개 버전의 Go versions에서 테스트되었습니다. 아직 초기화하지 않았다면 먼저 다음을 실행하세요:

go mod init github.com/<user>/<repo>  

그리고 nats 구현체를 설치합니다:

go get github.com/gofiber/storage/nats

Examples

스토리지 패키지를 가져옵니다.

import "github.com/gofiber/storage/nats"

다음과 같은 방법으로 스토리지를 생성할 수 있습니다:

// 기본 설정으로 초기화
store := nats.New()

// 사용자 정의 설정으로 초기화  
store := nats.New(Config{
  URLs: "nats://127.0.0.1:4443",
  NatsOptions: []nats.Option{
    nats.MaxReconnects(2),  
    // RootCA를 지정하여 TLS 활성화
    nats.RootCAs("./testdata/certs/ca.pem"),
  },
  KeyValueConfig: jetstream.KeyValueConfig{
    Bucket:  "test",
    Storage: jetstream.MemoryStorage,
  },
})

Config

type Config struct {
  // Nats URLs, 기본값 "nats://127.0.0.1:4222". 여러 서버를 위해 쉼표로 구분된 목록일 수 있음
  URLs string

  // Nats 연결 옵션. 사용 예시는 nats_test.go 참고
  NatsOptions []nats.Option

  // Nats 연결 이름  
  ClientName string

  // Nats 컨텍스트
  Context context.Context

  // Nats key value 설정
  KeyValueConfig jetstream.KeyValueConfig

  // Logger. Fiber AllLogger 인터페이스를 사용하여 다양한 로그 라이브러리 적용 가능
  Logger log.AllLogger

  // Nats 이벤트에 Logger 사용 여부, 기본값: false
  Verbose bool

  // 연결이 설정될 때까지 대기, 기본값: 100ms
  WaitForConnection time.Duration 
}

Default Config

var ConfigDefault = Config{
  URLs:     nats.DefaultURL,
  Context:  context.Background(),
  ClientName: "fiber_storage", 
  KeyValueConfig: jetstream.KeyValueConfig{
    Bucket: "fiber_storage",
  },  
  WaitForConnection: 100 * time.Millisecond,
}

Last updated