Skip to content

Commit 6eefef4

Browse files
authored
Merge pull request #1 from SoftwareSing/epic/ver-0.1.0
epic: version `0.1.0`
2 parents 6a543f3 + 037e81e commit 6eefef4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2509
-3173
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules/
2+
/dist/

.eslintrc.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
module.exports = {
2-
env: {
3-
commonjs: true,
4-
es2021: true,
5-
node: true
6-
},
72
extends: [
8-
'standard'
3+
'standard-with-typescript'
94
],
105
parserOptions: {
11-
ecmaVersion: 'latest'
6+
project: './tsconfig.json'
127
},
8+
plugins: [],
139
rules: {
1410
}
1511
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/node_modules/
2+
/dist/

README.md

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,67 @@
11
# cache-bridge
22

3-
Makes managing data between cache and DB easier
3+
Simplify managing data between cache and database.
44

5-
## **This package is still testing**
5+
## Features
66

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

9-
## Features
12+
## Example
13+
14+
```typescript
15+
import { createClient } from 'redis'
16+
import { MongoClient, ObjectId } from 'mongodb'
17+
import { createBridge, RedisCacheClient } from './src'
18+
19+
const redis = createClient()
20+
const mongo = new MongoClient('mongodb://localhost:27017')
21+
22+
async function main (): Promise<void> {
23+
await redis.connect()
24+
await mongo.connect()
25+
26+
const { bridge } = createBridge({
27+
cacheClient: new RedisCacheClient({ client: redis }),
28+
prefix: 'cache',
29+
ttl: 5000,
30+
// set how to get data from the DB
31+
get: async (id) => {
32+
return await mongo.db().collection('a').findOne({ _id: new ObjectId(id) })
33+
},
34+
// set how to get multiple data from the DB
35+
getMany: async (idList) => {
36+
const list = await mongo.db().collection('a')
37+
.find({
38+
_id: { $in: idList.map((id) => new ObjectId(id)) }
39+
})
40+
.toArray()
41+
return new Map(
42+
list.map((data) => [data._id.toHexString(), data])
43+
)
44+
}
45+
})
46+
47+
const consoleData = async (id: string): Promise<void> => {
48+
console.log(await bridge.get(id))
49+
}
50+
51+
// when called sequentially
52+
const id1 = '000000000000000000000001'
53+
await consoleData(id1) // get data from the DB
54+
await consoleData(id1) // get data from the cache
55+
await consoleData(id1) // get data from the cache
56+
57+
// when called simultaneously
58+
const id2 = '000000000000000000000002'
59+
await Promise.all([
60+
consoleData(id2), // get data from the DB
61+
consoleData(id2), // wait for the previous line to store the data in the cache and get data from the cache
62+
consoleData(id2) // wait for and get data from the cache
63+
])
64+
}
1065

11-
- fill cache from DB when cache miss, including locking to avoid cache stampede
66+
main()
67+
```

index.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

lib/Bridge.js

Lines changed: 0 additions & 117 deletions
This file was deleted.

lib/Cache.js

Lines changed: 0 additions & 164 deletions
This file was deleted.

0 commit comments

Comments
 (0)