Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.
Konfigurasikan batas waktu sisi klien (Valkey dan Redis OSS)
Mengkonfigurasi batas waktu sisi klien
Konfigurasikan waktu habis sisi klien dengan tepat agar server memiliki cukup waktu untuk memproses permintaan dan menghasilkan respons. Hal ini juga membantunya melakukan gagal cepat (fail fast) jika koneksi ke server tidak dapat dibuat. Perintah Valkey atau Redis OSS tertentu bisa lebih mahal secara komputasi daripada yang lain. Misalnya, skrip Lua atau transaksi MULTI/EXEC yang berisi beberapa perintah yang harus dijalankan secara atomik. Secara umum, waktu habis sisi klien yang lebih tinggi disarankan untuk menghindari waktu habis klien sebelum respons diterima dari server, termasuk yang berikut:
Menjalankan perintah di beberapa kunci
Menjalankan transaksi MULTI/EXEC atau skrip Lua yang terdiri dari beberapa perintah Valkey atau Redis OSS individual
Membaca nilai besar
Melakukan operasi pemblokiran seperti BLPOP
Dalam kasus operasi pemblokiran seperti BLPOP, praktik terbaiknya adalah dengan mengatur waktu habis perintah ke jumlah yang lebih rendah dari waktu habis soket.
Berikut ini adalah contoh kode untuk menerapkan batas waktu sisi klien di redis-py,, dan Lettuce. PHPRedis
Contoh konfigurasi waktu habis 1: redis-py
Berikut ini adalah contoh kode dengan redis-py:
# connect to Redis server with a 100 millisecond timeout # give every Redis command a 2 second timeout client = redis.Redis(connection_pool=redis.BlockingConnectionPool(host=HOST, max_connections=10,socket_connect_timeout=0.1,socket_timeout=2)) res = client.set("key", "value") # will timeout after 2 seconds print(res) # if there is a connection error res = client.blpop("list", timeout=1) # will timeout after 1 second # less than the 2 second socket timeout print(res)
Contoh konfigurasi batas waktu 2: PHPRedis
Berikut ini adalah contoh kode dengan PHPRedis:
// connect to Redis server with a 100ms timeout // give every Redis command a 2s timeout $client = new Redis(); $timeout = 0.1; // 100 millisecond connection timeout $retry_interval = 100; // 100 millisecond retry interval $client = new Redis(); if($client->pconnect($HOST, $PORT, 0.1, NULL, 100, $read_timeout=2) != TRUE){ return; // ERROR: connection failed } $client->set($key, $value); $res = $client->set("key", "value"); // will timeout after 2 seconds print "$res\n"; // if there is a connection error $res = $client->blpop("list", 1); // will timeout after 1 second print "$res\n"; // less than the 2 second socket timeout
Contoh konfigurasi waktu habis 3: Lettuce
Berikut ini adalah contoh kode dengan Lettuce:
// connect to Redis server and give every command a 2 second timeout public static void main(String[] args) { RedisClient client = null; StatefulRedisConnection<String, String> connection = null; try { client = RedisClient.create(RedisURI.create(HOST, PORT)); client.setOptions(ClientOptions.builder() .socketOptions(SocketOptions.builder().connectTimeout(Duration.ofMillis(100)).build()) // 100 millisecond connection timeout .timeoutOptions(TimeoutOptions.builder().fixedTimeout(Duration.ofSeconds(2)).build()) // 2 second command timeout .build()); // use the connection pool from above example commands.set("key", "value"); // will timeout after 2 seconds commands.blpop(1, "list"); // BLPOP with 1 second timeout } finally { if (connection != null) { connection.close(); } if (client != null){ client.shutdown(); } } }