An open API service indexing awesome lists of open source software.

https://github.com/blackglory/cache

🌳
https://github.com/blackglory/cache

docker-image esm microservice nodejs typescript

Last synced: about 14 hours ago
JSON representation

🌳

Awesome Lists containing this project

README

          

# Cache
提供以WebSocket为通讯协议的缓存服务, 受到Redis启发.

## Quickstart
```sh
docker run \
--detach \
--publish 8080:8080 \
blackglory/cache
```

## Install
### 从源代码运行
```sh
git clone https://github.com/BlackGlory/cache
cd cache
yarn install
yarn build
yarn bundle
yarn --silent start
```

### 从源代码构建
```sh
git clone https://github.com/BlackGlory/cache
cd cache
yarn install
yarn docker:build
```

### Recipes
#### docker-compose.yml
```yaml
version: '3.8'

services:
cache:
image: 'blackglory/cache'
restart: always
volumes:
- 'cache-data:/data'
ports:
- '8080:8080'

volumes:
cache-data:
```

## API
```ts
interface INamespaceStats {
items: number
}

interface IItem {
value: JSONValue
metadata: IItemMetadata
}

interface IItemMetadata {
updatedAt: number
timeToLive: number | null
}

interface IAPI {
getAllNamespaces(): string[]
getAllItemKeys(namespace: string): string[]

getNamespaceStats(namespace: string): INamespaceStats

hasItem(namespace: string, itemKey: string): boolean

getItem(namespace: string, itemKey: string): IItem | null
getItemValue(namespace: string, itemKey: string): JSONValue | null

setItem(
namespace: string
, itemKey: string
, itemValue: JSONValue
, timeToLive: number | null /* ms */
): null

removeItem(namespace: string, itemKey: string): null

clearItemsByNamespace(namespace: string): null
}
```

## 环境变量
### `CACHE_HOST`, `CACHE_PORT`
通过环境变量`CACHE_HOST`和`CACHE_PORT`决定服务器监听的地址和端口,
默认值为`localhost`和`8080`.

### `CACHE_WS_HEARTBEAT_INTERVAL`
通过环境变量`CACHE_WS_HEARTBEAT_INTERVAL`可以设置WS心跳包(ping帧)的发送间隔, 单位为毫秒.
在默认情况下, 服务不会发送心跳包,
半开连接的检测依赖于服务端和客户端的运行平台的TCP Keepalive配置.

当`CACHE_WS_HEARTBEAT_INTERVAL`大于零时,
服务会通过WS的ping帧按间隔发送心跳包.

### `CACHE_MEMORY_CACHE_LRU`
通过环境变量设置`CACHE_MEMORY_CACHE_LRU`可以设置内存缓存的LRU容量.
默认值为`0`, 即不使用内存缓存.

## 信号
### SIGUSR1
发送SIGUSR1执行heapdump:
```sh
kill -SIGUSR1 {pid}
```

## 客户端
- JavaScript/TypeScript(Node.js, Browser):