tencent cloud

Tencent Cloud Distributed Cache (Redis OSS-Compatible)

Release Notes and Announcements
Release Notes
Announcements
User Tutorial
Product Introduction
Overview
Product Strengths
Use Cases
Storage Engine
Product Series
Product Versions
Specifications and Performance
Read/Write Separation
Multi-AZ Deployment
Regions and AZs
Terms
Service Regions and Service Providers
Purchase Guide
Billing Overview
Pricing Center
Instance Purchasing
Renewal (Yearly/Monthly Subscription)
Refund (Yearly/Monthly Subscription)
Overdue Payments
Switching from Pay-as-You-Go to Yearly/Monthly Subscription
Getting Started
Quickly Creating an Instance
Connecting to an Instance (Redis/Valkey Edition)
Operation Guide
Operation Overview
Connecting to a Database Instance
Managing Instances
Upgrade Instance
Management Node (Redis/ValKey Edition)
Multi-AZ Deployment Management
Backup and Restoration
Managing Accounts
Parameter Configuration
Slow Query
Access Management
Network and Security
Monitoring and Alarms
Event Management (Redis/ValKey Edition)
Data Migration
Global Replication for Redis Edition
Database Audit
Performance Optimization
Sentinel Mode
Development Guidelines
Naming Rules
Basic Usage Guidelines
Design Principles of Key and Value
Command Usage Guidelines
Design Principles of Client Programs
Connection Pool Configuration
Command Reference
Command Reference Overview
Redis Edition and Valkey Edition Command Compatibility
Version Command Usage Differences
Differences Between the Proxy Architecture and Direct Connection Mode
More Command Operations (Redis/Valkey Edition)
Memcached Edition Command Compatibility
Practical Tutorial
Building TencentDB for Redis® Client Monitoring Based on Spring Boot
Redis Client Connection Configuration Policy and Practice
Global SCAN Guide for Cluster Architecture
Eliminating Instances Securely
Hot Key and Big Key
AZ Migration Scheme
Troubleshooting
Connection Exception
Exception Analysis and Solution of Redisson Client Timeout Reconnection
Performance Troubleshooting and Fine-Tuning
API Documentation
History
Introduction
API Category
Making API Requests
Instance APIs
Parameter Management APIs
Other APIs
Backup and Restoration APIs
Region APIs
Monitoring and Management APIs
Log APIs
Data Types
Error Codes
FAQs
General
Connection and Login
Purchase
Service Agreement
Service Level Agreement
Terms of Service
Glossary
Contact Us

Sample Code of Jedis Connection Pool

PDF
포커스 모드
폰트 크기
마지막 업데이트 시간: 2024-11-05 10:22:22

Prerequisite

Download and install Jedis. The latest version is recommended.

Sample code

Sample code of the connection pool and the meaning of the parameters are as follows:
Parameter
Description
Suggestion
setMaxTotal
Maximum connections in the connection pool
It is subjected to the factors like the volume of concurrent business, access delay, and maximum connections.
setMaxIdle
Maximum idle connections in the connection pool
Set it to a value same as setMaxTotal
setMinIdle
Minimum idle connections in the connection pool
Set it the same as setMaxTotal
timeout
Timeout period
It is set based on your business model and the network linkage performance.
When network latency is low and your businesses are very sensitive to service latency, set the value from 50 to 100 ms.
If your business has high latency tolerance or large volume of key-value data, set it to 500 ms or 1,000 ms.
setTestOnBorrow
Whether to test the connection when obtaining it in a connection pool
When the value is true, connection.isValid() will be called for the connection test. This ensures the availability of the obtained connection while consuming QPS performance.
When the value is false, connection test will not be performed. The speed of obtaining a connection can be improved, but available connections may not be obtained.
setTestOnReturn
Whether to perform verification when the connection is returned to the connection pool.
When the value is true, connection.isValid() will be called when returning the connection, ensuring the connections are valid.
When the value is false, connection test will not be performed. The speed of returning a connection can be improved, but the returned connections may be unavailable.
JedisPoolConfig config = new JedisPoolConfig();
// Maximum idle connections, which can't exceed the maximum connections of Redis instances
config.setMaxIdle(200);
// Maximum connections, which can't exceed the maximum connections of Redis instances
config.setMaxTotal(200);
// Minimum idle connections in the connection pool
config.setMinIdle(20);
// Maximum wait time when the connections are used up
config.setMaxWaitMillis(3000);
// When an object is obtained from the connection pool, a ping check will be performed first. If the check fails, the object will be removed and destroyed.
config.setTestOnBorrow(false);
// When a connection is returned, a check will be performed first. Once the check fails, the connection will be terminated.
config.setTestOnReturn(false);
// Set the connection pool mode to “queue”
config.setLifo(false);
// Set the minimum connections
config.setTimeBetweenEvictionRunsMillis(3000);
// Replace the values of "host" and "password" with the connection address and password of the instance respectively
String host = "192.xx.xx.195";
String password = "123ad6aq";
// Read/write timeout in ms
int timeout = 2000;
int port = 6379;
JedisPool pool = new JedisPool(config,host,port,timeout,password);
Jedis jedis = null;
boolean broken = false;
try
{
jedis = pool.getResource();
/// ... do stuff here ... for example
jedis.set("redis", "tencent");
String foobar = jedis.get("redis");
jedis.zadd("tec", 0, "a");
jedis.zadd("tec", 0, "b");
Set < String > sose = jedis.zrange("tec", 0, -1);
}
catch(Exception e)
{
broken = true;
}
finally
{
if(broken)
{
pool.returnBrokenResource(jedis);
}
else if(jedis != null)
{
pool.returnResource(jedis);
}
}


도움말 및 지원

문제 해결에 도움이 되었나요?

피드백