鸿蒙仓颉语言【在工程中使用Redis仓颉语言客户端】

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")
    }
}
相关推荐
dawn3 小时前
鸿蒙ArkTS中的获取网络数据
华为·harmonyos
桃花键神3 小时前
鸿蒙5.0时代:原生鸿蒙应用市场引领开发者服务新篇章
华为·harmonyos
鸿蒙自习室3 小时前
鸿蒙多线程开发——并发模型对比(Actor与内存共享)
华为·harmonyos
水月梦镜花3 小时前
redis:list列表命令和内部编码
数据库·redis·list
JavaPub-rodert4 小时前
鸿蒙生态崛起:开发者的机遇与挑战
华为·harmonyos
掘金-我是哪吒4 小时前
微服务mysql,redis,elasticsearch, kibana,cassandra,mongodb, kafka
redis·mysql·mongodb·elasticsearch·微服务
帅比九日6 小时前
【HarmonyOS Next】封装一个网络请求模块
前端·harmonyos
ketil276 小时前
Ubuntu 安装 redis
redis
王佑辉7 小时前
【redis】redis缓存和数据库保证一致性的方案
redis·面试
yilylong7 小时前
鸿蒙(Harmony)实现滑块验证码
华为·harmonyos·鸿蒙