Skip to content

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bun-KV-Shm (Bun-KeyValue-SharedMemory)

🚨⚠️ DISCLAIMER⚠️🚨

Bun-KV-Shm is neither an official library nor a major development project. It is rather a small utility that I developed as part of my work. It was designed to provide a minimal way to share memory between bun processes. The main priority was to develop it quickly. As a result, it may lack many features one would expect from a key-value library. And to preempt any comments, there are other solutions that can help you achieve your goal. I wanted something minimal, without any special configuration or external dependencies. In any case, use this tool at your own risk, keeping in mind that bun:ffi is experimental.

Description

Bun-KV-Shm is a small utility that allows data to be shared between bun processes using the POSIX shared memory API. As a result, this tool works only on Linux-based operating systems. Furthermore, as the name suggests, the Bun JavaScript runtime is required. The entire design is based on the use of bun:ffi. Essentially, all the logic related to the POSIX API will be implemented in the C programming language. Through the foreign function interface (FFI) provided by Bun, the C code (TinyCC) will be compiled and called from within JavaScript with low overhead. This project however has been developed in TypeScript, so wherever JavaScript is used, it is TypeScript.

Folder structure

bun-kv-shm/
├── src/
│   ├── benchmarks.ts			
│   ├── bun-kv-shm.ts			# Core API of bun-kv-shm 
│   ├── index.ts				# A simple use case
│   └── native/   				# Contains the native code written in C
│       └── shm.c
├── test/
│   ├── export.test.ts
│   └── index.test.ts

Bun-KV-Shm API

When initializing a BunKVShm instance, you can specify a custom prefix to be used in the shared memory keys for storage, retrieval, and deletion.

    constructor(_prefix:string='bun-kv-shm')

Set a key-value pair, with an optional compress flag.

    set(key:string,content:any,compress:boolean=false):void

Get a value by key. Returns the entry or undefined if it does not exist.

    get<T>(key:string):Promise<T|undefined>

Retrieves all entries. Returns an array containing all the entries. (based on the prefix specified during instance initialization; if no prefix has been specified, the default will be used)

    getAll<T>():Promise<T[]>

Get all keys. Returns an array containing all the keys.

    keys():Promise<Array<string>>

Checks whether a key exists. Returns a BunFile Blob or undefined if the key does not exist.

    exists(key:string):Promise<Bun.BunFile|undefined>

Get a entry from the cache by key and delete it. If the key does not exist it will return undefined.

    take<T>(key:string):Promise<T|undefined>

Deletes an entry using its key. Returns true if the record was deleted.

    del(key:string):Promise<boolean>

Delete all entries. (based on the prefix specified during instance initialization; if no prefix has been specified, the default will be used)

clear():Promise<void>

Benchmarks - Performance

Bun-KV-Shm was certainly not designed with speed in mind. This is understandable, given the additional overhead it entails. More specifically, compared to Redis, which was 14 to 20 times faster than Bun-KV-Shm. As you can see, it is not a safe choice for applications where speed is critical.

benchmark

Is this tool unnecessary?

Maybe!🤔😅

External dependencies

  1. anomalyco/bun-ffi-structs C-style structs with memory layout control for FFI calls.

About

Bun-KV-Shm is a small utility that allows data to be shared between bun processes using the POSIX shared memory API

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages