Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
84d2161
chore(npm): install typescript
SoftwareSing Aug 8, 2022
31e2384
chore(npm): update eslint and typescript config
SoftwareSing Aug 9, 2022
4fccf20
feat: refactor with typescript
SoftwareSing Sep 29, 2022
5440e2a
chore(npm): update package
SoftwareSing Sep 29, 2022
ab047c9
chore: lint Bridge
SoftwareSing Sep 29, 2022
dd379d0
refactor(Cache): make data converter type reusable
SoftwareSing Apr 26, 2023
00a0e1c
fix(CacheClient): get() and getMany() return value may be undefined w…
SoftwareSing Apr 26, 2023
8de83a8
refactor(test): rewrite tests with typescript
SoftwareSing Apr 26, 2023
181ba8d
chore: upgrade to typescript@5.0.4
SoftwareSing Apr 26, 2023
79ffda0
chore: upgrade to eslint@8.39.0 eslint-config-standard-with-typescrip…
SoftwareSing Apr 26, 2023
48fe1be
chore: upgrade to uuid@9.0.0
SoftwareSing Apr 26, 2023
399a3d8
chore: upgrade to lru-cache@9.1.1
SoftwareSing Apr 26, 2023
1300639
chore: upgrade to mocha@10.2.0 chai@4.3.7
SoftwareSing Apr 26, 2023
67b2509
chore: upgrade types
SoftwareSing Apr 26, 2023
5ed36fb
chore: npm update
SoftwareSing Apr 26, 2023
c9a34a3
feat(LruCacheClient): implement LruCacheClient
SoftwareSing Apr 27, 2023
3dfd245
test(CacheClient): add CacheClient standard test
SoftwareSing May 5, 2023
f4a31f2
feat(RedisCacheClient): implement RedisCacheClient
SoftwareSing May 5, 2023
935ca74
feat(IoRedisCacheClient): implement IoRedisCacheClient
SoftwareSing May 6, 2023
4e0e270
feat(RedisCacheClient): default use pipeline on setMany
SoftwareSing May 6, 2023
a8f1e81
feat(generate): add generate
SoftwareSing May 6, 2023
f7ab77a
chore: update package info
SoftwareSing May 6, 2023
f7c74f1
chore: change publish way
SoftwareSing May 6, 2023
a37c26b
chore: change file structure
SoftwareSing May 7, 2023
2a6ea61
chore: update README
SoftwareSing May 7, 2023
037e81e
chore: upgrade dev package
SoftwareSing May 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
/dist/
10 changes: 3 additions & 7 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
module.exports = {
env: {
commonjs: true,
es2021: true,
node: true
},
extends: [
'standard'
'standard-with-typescript'
],
parserOptions: {
ecmaVersion: 'latest'
project: './tsconfig.json'
},
plugins: [],
rules: {
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules/
/dist/
66 changes: 61 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,67 @@
# cache-bridge

Makes managing data between cache and DB easier
Simplify managing data between cache and database.

## **This package is still testing**
## Features

so be careful if you want to use it in production.
- Get data from cache, and automatically copy it from database when cache miss.
- Will acquire the lock before accessing database, avoid [Cache stampede](https://en.wikipedia.org/wiki/Cache_stampede).
- Supports [lru-cache](https://www.npmjs.com/package/lru-cache).
- Supports Redis, via [node-redis](https://www.npmjs.com/package/redis) or [ioredis](https://www.npmjs.com/package/ioredis).

## Features
## Example

```typescript
import { createClient } from 'redis'
import { MongoClient, ObjectId } from 'mongodb'
import { createBridge, RedisCacheClient } from './src'

const redis = createClient()
const mongo = new MongoClient('mongodb://localhost:27017')

async function main (): Promise<void> {
await redis.connect()
await mongo.connect()

const { bridge } = createBridge({
cacheClient: new RedisCacheClient({ client: redis }),
prefix: 'cache',
ttl: 5000,
// set how to get data from the DB
get: async (id) => {
return await mongo.db().collection('a').findOne({ _id: new ObjectId(id) })
},
// set how to get multiple data from the DB
getMany: async (idList) => {
const list = await mongo.db().collection('a')
.find({
_id: { $in: idList.map((id) => new ObjectId(id)) }
})
.toArray()
return new Map(
list.map((data) => [data._id.toHexString(), data])
)
}
})

const consoleData = async (id: string): Promise<void> => {
console.log(await bridge.get(id))
}

// when called sequentially
const id1 = '000000000000000000000001'
await consoleData(id1) // get data from the DB
await consoleData(id1) // get data from the cache
await consoleData(id1) // get data from the cache

// when called simultaneously
const id2 = '000000000000000000000002'
await Promise.all([
consoleData(id2), // get data from the DB
consoleData(id2), // wait for the previous line to store the data in the cache and get data from the cache
consoleData(id2) // wait for and get data from the cache
])
}

- fill cache from DB when cache miss, including locking to avoid cache stampede
main()
```
15 changes: 0 additions & 15 deletions index.js

This file was deleted.

117 changes: 0 additions & 117 deletions lib/Bridge.js

This file was deleted.

164 changes: 0 additions & 164 deletions lib/Cache.js

This file was deleted.

Loading