Cache 서비스
이 서비스는 플러그인이 캐시와 상호 작용할 수 있게 해요. 플러그인에도 바인딩되므로 플러그인의 사설 네임스페이스에서만 값을 설정하고 가져올 수 있어요.
출처: 문서
본문
이 서비스는 플러그인이 캐시와 상호 작용할 수 있게 해요. 플러그인에도 바인딩되므로 플러그인의 사설 네임스페이스에서만 값을 설정하고 가져올 수 있어요.
구성
캐시 서비스는 app-config.yaml의 backend.cache 섹션을 사용해 구성할 수 있어요:
인메모리 (기본)
backend: cache: store: memory
Memcache
backend: cache: store: memcache connection: user:[email protected]:11211
Redis
backend: cache: store: redis # Connection can be a simple URL string: connection: redis://localhost:6379 # Or an object with additional options passed to the underlying client # (only supported for the Redis store): # connection: # url: redis://localhost:6379 # pingInterval: 60000 # Store-specific configuration (optional) redis: client: # Global namespace prefix for all cache keys namespace: 'my-app' # Separator used between namespace and plugin ID (default: ':') keyPrefixSeparator: ':' # Other Redis-specific options... clearBatchSize: 1000 useUnlink: false
Valkey
backend: cache: store: valkey connection: redis://localhost:6379 # Store-specific configuration (optional) valkey: # Global namespace prefix for all cache keys (including separator used between namespace and plugin ID) keyPrefix: 'my-app:'
Infinispan
backend: cache: store: infinispan # Store-specific configuration (optional) infinispan: servers: # IP address or hostname of the server (default: '127.0.0.1') - host: 127.0.0.1 # Port number of the server (default: '11222') port: 11222 # Name of the cache (default: 'cache') cacheName: cache mediaType: application/json authentication: # Whether authentication is enabled (default: 'false') enabled: true userName: yourusername password: yourpassword saslMechanism: PLAIN
전체 구성 항목 목록은 백업 클러스터 지원을 포함해 여기에서 확인할 수 있어요.
네임스페이스 구성
Redis 및 Valkey 스토어의 경우 모든 캐시 키에 접두사로 추가될 전역 네임스페이스를 구성할 수 있어요:
-
네임스페이스 없음: 캐시 키는 플러그인 ID만 사용 (예:
catalog:some-key) -
네임스페이스 있음: 캐시 키는
namespace:pluginId:key형식 사용 (예:my-app:catalog:some-key)
Redis의 경우 keyPrefixSeparator는 네임스페이스와 플러그인 ID 사이에 사용되는 문자를 제어해요(기본값은 :).
Valkey의 경우 구분자를 포함한 전체 keyPrefix를 설정해요.
참고: 인메모리, Memcache 및 Infinispan 스토어는 네임스페이스 구성을 지원하지 않으며 항상 플러그인 ID를 직접 사용해요.
서비스 사용
다음 예시는 example 백엔드 플러그인에서 캐시 클라이언트를 얻고 캐시에서 값을 설정하고 가져오는 방법을 보여줘요.
import { coreServices, createBackendPlugin,} from '@backstage/backend-plugin-api';createBackendPlugin({ pluginId: 'example', register(env) { env.registerInit({ deps: { cache: coreServices.cache, }, async init({ cache }) { const { key, value } = { key: 'test:key', value: 'bob' }; await cache.set(key, value, { ttl: 1000 }); // .. some other stuff. await cache.get(key); // 'bob' }, }); },});