Connecting to Redis to Cache Object Data
Applications are connected to Redis to implement the data cache capability. Before interconnection, obtain the URL and login password for Redis to provide services. For details, see Viewing and Modifying Basic Settings of a DCS Instance.
Billing
After connecting an application to the Redis connector, you must pay for the DCS service separately. For details, see Huawei Cloud Pricing. After an application is disabled, DCS is still billed based on the preset billing rules.
Creating a Redis Connector
- Log in to the application designer by referring to Logging In to the Application Designer.
- In the navigation pane, choose Integrations.
- Click Connector Instance under Connector.
- In the navigation pane, choose Data > Redis.
- In the right pane, click + and set the parameters.
Table 1 Parameters for creating a Redis connector Parameter
Description
Name
Name of the connector to be created. The naming requirements are as follows:
- The value cannot exceed 64 characters, including the prefix namespace.
To prevent duplicate data names among different tenants, each tenant must define a unique namespace when first creating an application. A tenant can create only one namespace. After being created, the namespace cannot be modified.
- Start with a letter and can contain only letters, digits, and underscores (_). Do not end with an underscore (_).
Addresses
URLs used by Redis to provide services. Enter at least two URLs in cluster mode, and enter one URL on a line. See Figure 1.
Password
Password for connecting to Redis.
Package Current Configuration
If this parameter is selected, the connector will be packed together with applications. For example, if an application package is released to the runtime environment, the current configuration is synchronized to the runtime environment by default. If this option is not selected, the connector will not be packaged and released to the runtime environment. In this case, you need to create the corresponding connector by referring to Managing Connectors in Applications.
This parameter is selected by default. You are advised not to select this option in information-sensitive scenarios.
- The value cannot exceed 64 characters, including the prefix namespace.
- Click Save.
Calling the Connector in the Script
Call the connector in the script to connect to the Redis.
- Create an empty script by referring to Creating a Blank Script.
- In the script editor, enter the following code to implement a Redis read/write operation and record logs:
import * as redis from 'redis'; try { let cli = redis.newClient("Namespace__Redis1"); // cli.set("A", "B", 0); console.log(cli.get("A")); console.log(cli.set("some_non_exist_key", "1", 0)); console.log(cli.get("some_non_exist_key")); console.log(cli.set("some_non_exist_key", "2", 0)); console.log(cli.get("some_non_exist_key")); } catch (e) { console.log(e.message); }
In redis.newClient("Namespace __Redis1"), Namespace __Redis1 is the connector name.
- Click
in the upper part of the script editor to save the script.
- Click
to execute the script.
- Click
in the upper right corner of the test window without setting input parameters.
- On the log tab page, you can view the updated Redis data.
0829 14:09:10.889|debug|vm[7]>>> AstroZero 1.2.8 - Production on 2019-08-28 14:28:06 1e6786b57891a6512b6922bf02cc90ad972a4e9a debug (<unknown>.ts:0) 0829 14:09:10.889|debug|vm[7]>>> script: Namespace__testsingleredis 1.0.1 (<unknown>.ts:0) 0829 14:09:10.889|debug|vm[7]>>> locale: zh_CN (<unknown>.ts:0) 0829 14:09:10.889|debug|vm[7]>>> timezone: (GMT+08:00) China Standard Time (Asia/A) (<unknown>.ts:0) 0829 14:09:10.897|debug|vm[7]>>> undefined (Namespace__testsingleredis.ts:6) 0829 14:09:10.898|debug|vm[7]>>> true (Namespace__testsingleredis.ts:7) 0829 14:09:10.899|debug|vm[7]>>> 1 (Namespace__testsingleredis.ts:8) 0829 14:09:10.901|debug|vm[7]>>> true (Namespace__testsingleredis.ts:9) 0829 14:09:10.902|debug|vm[7]>>> 2 (Namespace__testsingleredis.ts:10)
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot