鸿蒙仓颉语言【在工程中使用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")
    }
}
相关推荐
菠萝咕噜肉i19 分钟前
超详细:Redis分布式锁
数据库·redis·分布式·缓存·分布式锁
登云时刻2 小时前
Kubernetes集群外连接redis集群和使用redis-shake工具迁移数据(二)
redis·容器·kubernetes
syj_1112 小时前
初识ArkUI
ui·arkts·arkui
Dlwyz5 小时前
redis-击穿、穿透、雪崩
数据库·redis·缓存
工业甲酰苯胺7 小时前
Redis性能优化的18招
数据库·redis·性能优化
Random_index7 小时前
#Uniapp篇:支持纯血鸿蒙&发布&适配&UIUI
uni-app·harmonyos
Oak Zhang10 小时前
sharding-jdbc自定义分片算法,表对应关系存储在mysql中,缓存到redis或者本地
redis·mysql·缓存
门牙咬脆骨10 小时前
【Redis】redis缓存击穿,缓存雪崩,缓存穿透
数据库·redis·缓存
门牙咬脆骨10 小时前
【Redis】GEO数据结构
数据库·redis·缓存
鸿蒙自习室11 小时前
鸿蒙多线程开发——线程间数据通信对象02
ui·harmonyos·鸿蒙