1. 在工程中使用Redis仓颉语言客户端
1.1 通过源码方式引入Redis客户端依赖
仓颉0.51.4以上版本:在项目的cjpm.toml中添加dependencies引入redis_sdk依赖:
[dependencies]
redis_sdk = {git = "https://gitcode.com/Cangjie-TPC/redis-sdk.git", branch = "master", version = "1.0.0"}
仓颉0.45.2版本:在项目的module.json中添加requires引入redis_sdk依赖:
"requires": {
"redis_sdk": {
"organization": "Beijing Baolande Software Corporation",
"version": "1.0.0",
"branch": "master",
"git": "https://gitcode.com/Cangjie-TPC/redis-sdk.git"
}
},
更新依赖,运行cjpm update会自动下载依赖redis_sdk项目到~/.cjpm目录下
$> cjpm update
1.2 编译Redis仓颉语言客户端并导入静态库依赖
编译Redis仓颉语言客户端请参考:[用户手册]
引入编译好的静态库依赖和通过源码方式引入依赖,任意选取一种方式即可。 参考"1.1 通过源码方式引入Redis客户端依赖"。
仓颉0.51.4以上版本,需要先确定平台对应的target-name:
例如Windows X64平台执行cjc -v
命令返回如下:
$cjc -v
Cangjie Compiler: 0.51.4 (cjnative)
Target: x86_64-w64-mingw32
例如Linux X64平台执行cjc -v
命令返回如下:
$cjc -v
Cangjie Compiler: 0.51.4 (cjnative)
Target: x86_64-unknown-linux-gnu
在工程的cjpm.toml中添加平台对应的二进制依赖,以Linux X64为例:
[target.x86_64-unknown-linux-gnu.bin-dependencies.package-option]
"hyperion_hyperion.buffer" = "${path_to_redis_sdk}/build/release/hyperion/hyperion.buffer.cjo"
"hyperion_hyperion.logadapter" = "${path_to_redis_sdk}/build/release/hyperion/hyperion.logadapter.cjo"
"hyperion_hyperion.objectpool" = "${path_to_redis_sdk}/build/release/hyperion/hyperion.objectpool.cjo"
"hyperion_hyperion.threadpool" = "${path_to_redis_sdk}/build/release/hyperion/hyperion.threadpool.cjo"
"hyperion_hyperion.transport" = "${path_to_redis_sdk}/build/release/hyperion/hyperion.transport.cjo"
"redis_sdk_redis.client.api" = "${path_to_redis_sdk}/build/release/redis_sdk/redis.client.api.cjo"
"redis_sdk_redis.client.commands" = "${path_to_redis_sdk}/build/release/redis_sdk/redis.client.commands.cjo"
"redis_sdk_redis.client.commands.impl" = "${path_to_redis_sdk}/build/release/redis_sdk/redis.client.commands.impl.cjo"
"redis_sdk_redis.client" = "${path_to_redis_sdk}/build/release/redis_sdk/redis.client.cjo"
仓颉0.45.2版本,在工程的module.json中引入Redis仓颉语言客户端的静态库:
"package_requires": {
"package_option": {
"hyperion_hyperion.buffer": "${path_to_redis_sdk}/build/release/hyperion/hyperion.buffer.cjo",
"hyperion_hyperion.logadapter": "${path_to_redis_sdk}/build/release/hyperion/hyperion.logadapter.cjo",
"hyperion_hyperion.objectpool": "${path_to_redis_sdk}/build/release/hyperion/hyperion.objectpool.cjo",
"hyperion_hyperion.threadpool": "${path_to_redis_sdk}/build/release/hyperion/hyperion.threadpool.cjo",
"hyperion_hyperion.transport": "${path_to_redis_sdk}/build/release/hyperion/hyperion.transport.cjo",
"redis_sdk_redis.client.api": "${path_to_redis_sdk}/build/release/redis_sdk/redis.client.api.cjo",
"redis_sdk_redis.client.commands": "${path_to_redis_sdk}/build/release/redis_sdk/redis.client.commands.cjo",
"redis_sdk_redis.client.commands.impl": "${path_to_redis_sdk}/build/release/redis_sdk/redis.client.commands.impl.cjo",
"redis_sdk_redis.client": "${path_to_redis_sdk}/build/release/redis_sdk/redis.client.cjo"
},
"path_option": []
},
1.3 创建RedisClient用于执行Redis命令
from redis_sdk import redis.client.api.*
from redis_sdk import redis.client.commands.*
from redis_sdk import redis.client.*
public func getRedisClient(): RedisClient {
let redisClient = RedisClientBuilder.builder()
.host("127.0.0.1")
.port(6379)
.readTimeout(Duration.second * 60)
.writeTimeout(Duration.second * 30)
.receiveBufferSize(32768)
.sendBufferSize(32768)
.build()
return redisClient
}
1.4 使用RedisClient执行Redis命令操作
from redis_sdk import redis.client.api.*
from redis_sdk import redis.client.commands.*
from redis_sdk import redis.client.*
main() {
// 获取Redis客户端
let client = getRedisClient()
// 执行 SET testKey1 testValue1 XX命令
let result = client.set("testKey1", "testValue1", SetParams().xx())
if (let Some(result) <- result) {
println(result)
} else {
println("nil")
}
// 执行 GET testKey1命令
var getResult = client.get("testKey1")
if (let Some(getResult) <- getResult) {
println(getResult)
} else {
println("nil")
}
}