기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
캐시에 데이터 읽기 및 쓰기
이 섹션에서는 Amazon EC2 인스턴스를 생성했고 이 인스턴스에 연결할 수 있다고 가정합니다. 작업 방법에 대한 지침은 Amazon EC2 시작 안내서
기본적으로 ElastiCache 캐시는 기본 VPC에 캐시를 생성합니다. 기본 VPC에서도 EC2 인스턴스를 생성해야 캐시에 연결할 수 있습니다.
캐시 엔드포인트 확인
AWS Management Console
ElastiCache 콘솔을 사용하여 캐시의 엔드포인트를 확인하려면 다음과 같이 하세요.
에 로그인 AWS Management Console 하고 https://console.aws.amazon.com/elasticache/
Amazon ElastiCache 콘솔을 엽니다. 콘솔 왼쪽의 탐색 창에서 Memcached 캐시를 선택합니다.
콘솔의 오른쪽에서 앞서 생성한 캐시의 이름을 클릭합니다.
캐시 세부 정보에서 캐시 엔드포인트를 찾아 복사합니다.
AWS CLI
다음 AWS CLI 예제에서는 describe-serverless-caches 명령을 사용하여 새 캐시의 엔드포인트를 찾는 방법을 보여줍니다. 명령을 실행한 후 '엔드포인트' 필드를 찾습니다.
Linux
aws elasticache describe-serverless-caches \ --serverless-cache-name CacheName
Windows
aws elasticache describe-serverless-caches ^ --serverless-cache-name CacheName
OpenSSL을 사용하여 연결하는 방법에 대한 내용은 ElastiCache 전송 중 데이터 암호화(TLS) 섹션을 참조하세요.
import java.security.KeyStore; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManagerFactory; import net.spy.memcached.AddrUtil; import net.spy.memcached.ConnectionFactoryBuilder; import net.spy.memcached.FailureMode; import net.spy.memcached.MemcachedClient; public class TLSDemo { public static void main(String[] args) throws Exception { ConnectionFactoryBuilder connectionFactoryBuilder = new ConnectionFactoryBuilder(); // Build SSLContext TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init((KeyStore) null); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, tmf.getTrustManagers(), null); // Create the client in TLS mode connectionFactoryBuilder.setSSLContext(sslContext); // Set Failure Mode to Retry connectionFactoryBuilder.setFailureMode(FailureMode.Retry); MemcachedClient client = new MemcachedClient(connectionFactoryBuilder.build(), AddrUtil.getAddresses("mycluster-fnjyzo.serverless.use1.cache.amazonaws.com:11211")); // Store a data item for an hour. client.set("theKey", 3600, "This is the data value"); } }
<?php $cluster_endpoint = "mycluster.serverless.use1.cache.amazonaws.com"; $server_port = 11211; /* Initialize a persistent Memcached client in TLS mode */ $tls_client = new Memcached('persistent-id'); $tls_client->addServer($cluster_endpoint, $server_port); if(!$tls_client->setOption(Memcached::OPT_USE_TLS, 1)) { echo $tls_client->getLastErrorMessage(), "\n"; exit(1); } $tls_config = new MemcachedTLSContextConfig(); $tls_config->hostname = '*.serverless.use1.cache.amazonaws.com'; $tls_config->skip_cert_verify = false; $tls_config->skip_hostname_verify = false; $tls_client->createAndSetTLSContext((array)$tls_config); /* store the data for 60 seconds in the cluster */ $tls_client->set('key', 'value', 60); ?>
https://pymemcache.readthedocs.io/en/latest/getting_started.html
import ssl from pymemcache.client.base import Client context = ssl.create_default_context() cluster_endpoint = <To be taken from the AWS CLI / console> target_port = 11211 memcached_client = Client(("{cluster_endpoint}", target_port), tls_context=context) memcached_client.set("key", "value", expire=500, noreply=False) assert self.memcached_client.get("key").decode() == "value"
https://github.com/electrode-io/memcache
npm i memcache-client
를 통해 설치합니다.
애플리케이션에서 다음과 같이 Memcache TLS 클라이언트를 생성합니다.
var memcache = require("memcache-client"); const client = new memcache.MemcacheClient({server: "{cluster_endpoint}:11211", tls: {}}); client.set("key", "value");
https://crates.io/crates/memcache
// create connection with to memcached server node: let client = memcache::connect("memcache+tls://<cluster_endpoint>:11211?verify_mode=none").unwrap(); // set a string value client.set("foo", "bar", 0).unwrap();
https://github.com/bradfitz/gomemcache
c := New(net.JoinHostPort("{cluster_endpoint}", strconv.Itoa(port))) c.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { var td tls.Dialer td.Config = &tls.Config{} return td.DialContext(ctx, network, addr) } foo := &Item{Key: "foo", Value: []byte("fooval"), Flags: 123} err := c.Set(foo)
https://github.com/petergoldstein/dalli
require 'dalli' ssl_context = OpenSSL::SSL::SSLContext.new ssl_context.ssl_version = :SSLv23 ssl_context.verify_hostname = true ssl_context.verify_mode = OpenSSL::SSL::VERIFY_PEER client = Dalli::Client.new("<cluster_endpoint>:11211", :ssl_context => ssl_context); client.get("abc")
https://github.com/cnblogs/EnyimMemcachedCore
"MemcachedClient": { "Servers": [ { "Address": "{cluster_endpoint}", "Port": 11211 } ], "UseSslStream": true }
이제 (선택 사항) 정리 섹션으로 이동합니다.