2.0.6 • Published 1 year ago

@ksfcore/kedis-helper v2.0.6

Weekly downloads
-
License
BSD-3-Clause
Repository
-
Last release
1 year ago

说明

Kedis是一个基于KSF框架开发的分布式NoSQL存储系统,数据采用内存存储,支持连接后端DB实现数据持久化。Kedis采用集群模式,具有高扩展、高可用的特点。

@ksfcore/kedis-helper是Kedis调用的nodejs实现。

同时从2.1.0版本开始,不再依赖Q模块,而使用原生Promise.

$ npm i @ksfcore/kedis-helper

初始化

const Kedis = require('@ksfcore/kedis-helper')
const kedisProxy = new Kedis({
  proxy: 'Kedis.ADProxyServer.ProxyObj',
  moduleName: 'ADUserProfile'
})

只需要在对应的proxy添加ip端口既可以实现通过代理方式访问

const kedisProxy = new Kedis({
  proxy: 'Kedis.ADProxyServer.ProxyObj@tcp -h 172.16.8.125 -p 8888 -t 6000',
  moduleName: 'ADUserProfile'
})

默认情况下,Kedis的rpc调用保持长连接,即每20秒会给服务端发送一个心跳,如果不想保持长连接,可以通过设置keepAlive为false来实现,例如:

const kedisProxy = new Kedis({
  keepAlive: false,
  proxy: 'Kedis.ADProxyServer.ProxyObj',
  moduleName: 'ADUserProfile'
})

调用

Kedis相关数据结构请参照 CacheShare.jceProxyShare.jce,对应的js对象分别为 cacheShareProxyproxyShareProxy

const { cacheShareProxy, proxyShareProxy } = require('@ksfcore/kedis-helper')

Kedis调用API请参照 Proxy.jce,下例将给出多个DEMO

const KsfStream = require("@ksfcore/stream")
const Kedis = require('@ksfcore/kedis-helper')
const { cacheShareProxy, proxyShareProxy } = Kedis

const kedisProxy = new Kedis({
  proxy: 'Kedis.ADProxyServer.ProxyObj@tcp -h 172.16.8.125 -p 8888 -t 6000',
  moduleName: 'ADUserProfile'
})

// 单次不带版本调用
kedisProxy.getString('testKey').then.then(console.log).catch(console.error);

// 单次不带版本写入
kedisProxy.setString('testKey', 'testValue').then(console.log).catch(console.error);


// 批量查询全量数据
const vec = new KsfStream.List(KsfStream.String)
vec.readFromObject(['testKey1', 'testKey2'])
kedisProxy.getKVBatch(vec).then(console.log).catch(console.error)
//...

指定master

如下列举两种服务调用方式:

1.http server

自由传递context上下文,参照如下httpdemo

const KsfStream = require('@ksfcore/stream')
const kedisProxy = new Kedis({
  proxy: 'Kedis.ADProxyServer.ProxyObj',
  moduleName: 'ADUserProfile',
})

const context = new KsfStream.Map(KsfStream.String, KsfStream.String)
context.insert('idcAreaSpecified', 'force-master')
// http server or other environment
kedisProxy.getKV('a', { context }).then(console.log).catch(console.error)

2.ksf server

ksf server 从当前服务的上下文中获取上下文,并进行传递 当然也支持自己构造context上下文方式(参照上述hhtp方式)

// ksf server
CRM.TestImp.prototype.test = async function (current, req, rsp) {

  // ...

  //////////// 透传context DEMO start ////////
  const context = current.getContext();
  context.insert('idcAreaSpecified', 'force-master')

  kedisProxy.getString('a', { context }).then(console.log).catch(console.error);

  //////////// 透传context DEMO end ////////

  // ...
}
2.0.6

1 year ago

2.0.5

1 year ago

2.0.4

1 year ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.1

1 year ago