cache-grpc-server
TypeScript icon, indicating that this package has built-in type declarations

0.0.8 • Public • Published

Overall

Server for fast save and read data from cache. Use GRPC stream for connection and Redis as storage. Ready for TLS and mTLS.

Features:

  • GRPC connection
  • GET and SET method as stream
  • Reflection
  • HealthProbe
  • Redis as storage
  • NestJs framework

Client create one connection via stream, and send get or set command via grpc stream. This is very quick solution Server has implemented grpc.health.v1 for readinessprobe too

Run server

Via docker

docker run -it --rm -e REDIS_HOST=redis://redis:6379 -p 3000:3000 gawsoft/cache-grpc-server
# Or via docker-compose 
docker-compose up

For check health

If you want to run this in kubernetes run

grpc_health_probe --addr localhost:3000

Via nodejs/typescript

yarn build
yarn start

Env

HOST=0.0.0.0:3000
REDIS_HOST=127.0.0.1:6379

# Setup TLS
#TLS_INSECURE=false
#TLS_KEY_PATH=/etc/ssl/key.pem
#TLS_CERT_PATH=/etc/ssl/cert.pem

# For mutual tls
#TLS_CA_CERT_PATH=/etc/ssl/ca.cert
#TLS_VERIFY_CLIENT_CERT=true

ProtoBuf

rpc Get (stream GetRequest) returns (stream GetResponse);
rpc Exists (stream GetRequest) returns (stream ExistsResponse);
rpc Set (stream SetRequest) returns (stream SetResponse);

Use as a library (Custom storage strategy)

If you dont want to use Redis as a storage you can write custom storage class with Interface StorageStrategyInterface

npm install cache-grpc-server
import { NestFactory } from '@nestjs/core';
import { AppModule, GrpcClientOptions, SetRequestInterface, StorageStrategyInterface } from 'cache-grpc-server'
import { MicroserviceOptions } from '@nestjs/microservices';
import { Injectable } from '@nestjs/common';

@Injectable()
class CustomStorageStrategy implements StorageStrategyInterface
{
  async existsMulti(keys: string[]): Promise<boolean[]> {
    return [true, false];
  }
  async getMulti(keys: string[]): Promise<string[]> {
    return ['a','b'];
  }
  async save(data: SetRequestInterface): Promise<string> {
    return "test"
  }
}

export async function bootstrap() {
  // Here inject custom storage strategy
  const app = await NestFactory.create(AppModule.register(CustomStorageStrategy));
  const grpcClientOptions = app.get(GrpcClientOptions);
  app.connectMicroservice<MicroserviceOptions>(grpcClientOptions.getOptions());

  await app.startAllMicroservices();
  await app.init();
  return app;
}

bootstrap();

Tests

E2E tests

yarn test:e2e

Example client

ts-node examples/client-set.ts
ts-node examples/client-get.ts

Readme

Keywords

none

Package Sidebar

Install

npm i cache-grpc-server

Homepage

gawsoft.com

Weekly Downloads

0

Version

0.0.8

License

MIT

Unpacked Size

293 kB

Total Files

81

Last publish

Collaborators

  • gawsoft