Hello everyone, today let's talk about how to quickly get started with Huawei AppGallery Connect (AGC)'s Cloud Cache service. As a Key-Value cache service based on a Serverless architecture, it not only supports automatic elastic scaling but also eliminates O&M troubles, making it very suitable for high-concurrency data read and write needs.
Next, I will guide you step-by-step from information acquisition to hands-on coding to implement Cloud Cache integration.
I. A Complete Guide to Obtaining Cloud Cache Information
1.1 Obtaining Basic Information
- Log in to the Console Open the AppGallery Connect console, select the target project in "My Projects", and navigate to the "Manage" tab via "Serverless > Cloud Cache" in the left navigation bar.
-
Copy Key Parameters
- Username: Directly displayed in the "Access Information" module, used for client authentication.
-
Intranet Address: The format is
<font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">Domain:Port</font>(e.g.,<font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">agcp-drcn.hispace.dbankcloud.cn:16380</font>).
1.2 Password Management
- Initial Password: The password set when activating Cloud Cache.
- Forgot Password: Modify it through the "Reset Access Password" function in the console. Note that the new password takes 30 minutes to take effect and will affect deployed services.
-
Password Policy:
Must start with a letter, be 8-32 characters long, and contain a combination of uppercase and lowercase letters, numbers, and special characters (e.g.,
<font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">Redis@2024</font>).
II. Hands-on Code: Node.js
Connect to Cloud Cache using the <font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">ioredis</font> library in just three steps:
const Redis = require('ioredis');
const redisClient = new Redis({
port: 16380,
host: 'agcp-drcn.hispace.dbankcloud.cn',
username: 'your-project-id', // Replace with the username from the console
password: 'your-password', // Fill in your Cloud Cache password
enableReadyCheck: false // Disable ready check (required)
});
// Example: Read a key-value pair
async function getData(key) {
return await redisClient.get(key);
}
Key Points:
- Using
<font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">enableReadyCheck:false</font>avoids protocol validation issues during connection. - In a production environment, it is recommended to configure a connection pool (refer to the
<font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">generic-pool</font>library).
III. Three Ways to Integrate with Java
3.1 Native Jedis (Suitable for lightweight projects)
Dependency Configuration:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.8.0</version>
</dependency>
Connection Pool Initialization:
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxWait(Duration.ofSeconds(5)); // Max wait 5 seconds
JedisPool pool = new JedisPool(config, "agcp-drcn...", 16380, 3000, "username", "password");
try (Jedis jedis = pool.getResource()) {
String value = jedis.get("name");
}
3.2 Spring RedisTemplate (Recommended for enterprise use)
Advantages: Supports advanced features like transactions and serialization.
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("domain", 16380);
config.setUsername("username");
config.setPassword(RedisPassword.of("password"));
JedisConnectionFactory factory = new JedisConnectionFactory(config);
factory.afterPropertiesSet();
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
return template;
}
3.3 Spring Boot Auto-configuration (The simplest solution)
application.properties Configuration:
spring.redis.host=agcp-drcn.hispace.dbankcloud.cn
spring.redis.port=16380
spring.redis.username=your-username
spring.redis.password=your-password
spring.redis.timeout=3000
Direct Injection in Business Code:
@Autowired
private StringRedisTemplate redisTemplate;
public void getData() {
redisTemplate.opsForValue().get("name");
}
IV. Pitfall Guide
-
Connection Timeout
It is recommended to set
<font style="color:rgb(255, 80, 44);background-color:rgb(255, 245, 245);">timeout=3000</font>(3 seconds). If timeouts are frequent, check if the security group allows port 16380. -
Performance Optimization
- Avoid large keys (String type < 5KB, collection elements < 200).
- Monitor hot keys (keys accessed over 200 times per second need to be split).
- Impact of Password Reset After changing the password, you must update the configuration of all clients synchronously, otherwise it will lead to connection failures.
V. Summary
Through this article, I believe you now have a comprehensive understanding of integrating with AGC Cloud Cache. Whether it's the lightweight solution for Node.js or the three flexible options in Java, they can all help your business quickly implement high-performance caching. If you encounter problems in practice, remember to check the "Usage Statistics" and "Hot Key Monitoring" features in the console—they are great helpers for troubleshooting!
If this tutorial was helpful, feel free to share your experiences in the comments section. See you next time! 🚀
Top comments (0)