Skip to content

Commit 8a8c0fc

Browse files
committed
release: v3.0.0
1 parent ddab490 commit 8a8c0fc

18 files changed

Lines changed: 2460 additions & 4817 deletions

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
**/*.d.ts
2+
node_modules
3+
/node_modules/

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
## v3.0.0
4+
5+
### Breaking Changes
6+
7+
- Creating a cache store now doesn't require a type for the keys. It is assumed to be a string.
8+
9+
### Other Changes
10+
11+
- Setting store.set on a key that already exists will also override the value.
12+
- Updates internal build dependencies
13+
314
## v2.1.1
415

516
- Remove debug statements

README.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
<p align="center">A no dependency, <b>high performance</b>, near optimal javascript caching library</p>
88

99
<p align="center">
10-
<a href="https://github.com/tusharf5/runtime-memcache">
10+
<a href="https://github.com/tusharf5/runtime-memcache">
1111
<img src="https://img.shields.io/npm/l/runtime-memcache" height="20"/>
1212
</a>
13-
<a href="https://github.com/tusharf5/runtime-memcache">
13+
<a href="https://github.com/tusharf5/runtime-memcache">
1414
<img src="https://img.shields.io/npm/v/runtime-memcache" height="20"/>
1515
</a>
16-
<a href="https://github.com/tusharf5/runtime-memcache">
16+
<a href="https://github.com/tusharf5/runtime-memcache">
1717
<img src="https://img.shields.io/npm/dt/runtime-memcache" height="20"/>
1818
</a>
19-
<a href="https://github.com/tusharf5/runtime-memcache">
19+
<a href="https://github.com/tusharf5/runtime-memcache">
2020
<img src="https://img.shields.io/bundlephobia/minzip/runtime-memcache" height="20"/>
2121
</a>
2222
</p><br/><br/>
@@ -126,7 +126,7 @@ type Keys = 'key1' | 'key2';
126126

127127
const store = createStore<Keys, Response>(config);
128128

129-
store.set('key1', { name : 'name' }); // store the object and associate it with the provided key
129+
store.set('key1', { name: 'name' }); // store the object and associate it with the provided key
130130

131131
store.get('key1'); // retrieves the object associated with this key
132132

@@ -142,23 +142,27 @@ store.remove('key1'); // deletes the object associated with this key
142142
</br>
143143

144144
```typescript
145-
import createStore from 'runtime-memcache';
145+
import createStore, { Config } from 'runtime-memcache';
146146

147-
const config = {
147+
const config: Config = {
148148
policy: 'lru',
149149
lruSize: 300, // cache a maximum of 300 users at a given time
150150
};
151151

152-
const userCache = createStore(config);
152+
interface User {
153+
name: string;
154+
}
155+
156+
const userCache = createStore<User>(config);
153157

154158
async function loginUser(userId: string) {
155-
if (userCache.has(id)) {
156-
return userCache.get(id);
159+
if (userCache.has(userId)) {
160+
return userCache.get(userId);
157161
}
158162

159-
const user = await UserService.getUser(id);
163+
const user = await UserService.getUser(userId);
160164

161-
userCache.set(id, user);
165+
userCache.set(userId, user);
162166

163167
return user;
164168
}
@@ -183,5 +187,6 @@ runtime-memcache uses a combination of modified doubly-linked lists and hashmap
183187
- Least Frequently Used Policy (LFU)
184188
- <s>Time Aware Least Recently Used Policy (TLRU)</s>
185189
- Random Eviction Policy (RR)
190+
- Add a warmup period for new items
186191

187192
For more information on caching policies read [this](https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU)

package.json

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "runtime-memcache",
3-
"version": "2.1.1",
3+
"version": "3.0.0",
44
"description": "A no dependency javascript runtime key-value cache store for small chunks of arbitrary data (strings, objects, numbers)",
55
"homepage": "https://github.com/tusharf5/runtime-memcache",
66
"main": "./dist/umd/index.js",
@@ -42,28 +42,28 @@
4242
"mru"
4343
],
4444
"devDependencies": {
45-
"@types/jest": "^26.0.0",
46-
"@types/node": "^14.0.0",
47-
"@typescript-eslint/eslint-plugin": "^4.0.0",
48-
"@typescript-eslint/parser": "^4.0.0",
49-
"babel-loader": "^8.0.0",
50-
"copy-webpack-plugin": "^5.0.0",
51-
"eslint": "^6.0.0",
52-
"eslint-config-airbnb": "^18.0.0",
53-
"eslint-config-prettier": "^6.0.0",
54-
"eslint-config-typescript": "^3.0.0",
55-
"eslint-formatter-pretty": "^3.0.0",
56-
"eslint-import-resolver-typescript": "^2.0.0",
57-
"eslint-loader": "^4.0.0",
58-
"eslint-plugin-import": "^2.0.0",
59-
"eslint-plugin-prettier": "^3.0.0",
60-
"eslint-plugin-typescript": "^0.14.0",
61-
"jest": "^26.0.0",
62-
"prettier": "^2.0.0",
63-
"ts-node": "^8.0.0",
64-
"typescript": "^3.0.0",
65-
"typescript-eslint-parser": "^22.0.0",
66-
"webpack": "^4.0.0",
67-
"webpack-cli": "^3.0.0"
45+
"@types/jest": "~28.1.0",
46+
"@types/node": "^16.0.0",
47+
"@typescript-eslint/eslint-plugin": "~5.30.0",
48+
"@typescript-eslint/parser": "~5.30.0",
49+
"babel-loader": "~8.2.0",
50+
"copy-webpack-plugin": "11.0.0",
51+
"eslint": "~8.20.0",
52+
"eslint-config-airbnb": "~19.0.0",
53+
"eslint-config-prettier": "~8.5.0",
54+
"eslint-config-typescript": "~3.0.0",
55+
"eslint-formatter-pretty": "~4.1.0",
56+
"eslint-import-resolver-typescript": "~3.2.0",
57+
"eslint-loader": "~4.0.0",
58+
"eslint-plugin-import": "~2.26.0",
59+
"eslint-plugin-prettier": "~4.2.0",
60+
"eslint-plugin-typescript": "~0.14.0",
61+
"jest": "~28.1.0",
62+
"prettier": "~2.7.0",
63+
"ts-node": "~10.9.0",
64+
"typescript": "~4.6.2",
65+
"typescript-eslint-parser": "~22.0.0",
66+
"webpack": "~5.73.0",
67+
"webpack-cli": "~4.10.0"
6868
}
6969
}

src/lru-store.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
import { Cache, GlobalConfig } from './types';
22
import { LRULinkedList } from './utils/LRULinkedList';
33

4-
function createStore<K, V>(config: Required<GlobalConfig>): Cache<K, V> {
5-
const store = new LRULinkedList<K, V>(config);
4+
function createStore<V>(config: Required<GlobalConfig>): Cache<V> {
5+
const store = new LRULinkedList<V>(config);
66

7-
function get(key: K) {
7+
function get(key: string): V {
88
const val = store.get(key);
99
if (val) {
1010
return val;
1111
}
1212
return null;
1313
}
1414

15-
function remove(key: K) {
15+
function remove(key: string) {
1616
store.remove(key);
1717
return true;
1818
}
1919

20-
function has(key: K) {
20+
function has(key: string) {
2121
return store.has(key);
2222
}
2323

2424
function size(): number {
2525
return store.size;
2626
}
2727

28-
function keys(): K[] {
28+
function keys(): string[] {
2929
return store.keys();
3030
}
3131

32-
function set(key: K, value: any) {
32+
function set(key: string, value: V) {
3333
store.addNodeToHead(key, value);
3434
return true;
3535
}

src/mru-store.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
import { Cache, GlobalConfig } from './types';
22
import { MRULinkedList } from './utils/MRULinkedList';
33

4-
function createStore<K, V>(config: Required<GlobalConfig>): Cache<K, V> {
5-
const store = new MRULinkedList<K, V>(config);
4+
function createStore<V>(config: Required<GlobalConfig>): Cache<V> {
5+
const store = new MRULinkedList<V>(config);
66

7-
function get(key: K) {
7+
function get(key: string): V {
88
const val = store.get(key);
99
if (val) {
1010
return val;
1111
}
1212
return null;
1313
}
1414

15-
function remove(key: K) {
15+
function remove(key: string) {
1616
store.remove(key);
1717
return true;
1818
}
1919

20-
function has(key: K) {
20+
function has(key: string) {
2121
return store.has(key);
2222
}
2323

2424
function size(): number {
2525
return store.size;
2626
}
2727

28-
function keys(): K[] {
28+
function keys(): string[] {
2929
return store.keys();
3030
}
3131

32-
function set(key: K, value: any) {
32+
function set(key: string, value: V) {
3333
store.addNodeToHead(key, value);
3434
return true;
3535
}

src/store.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const defaultConfig: Required<GlobalConfig> = {
1313
mruSize: 500,
1414
};
1515

16-
function createStore<K extends string, V = any>(userConfig?: Config): Cache<K, V> {
16+
function createStore<V = any>(userConfig?: Config): Cache<V> {
1717
let userConfigVerf: Config = {};
1818
if (typeof userConfig === 'object') {
1919
userConfigVerf = userConfig;
@@ -23,13 +23,13 @@ function createStore<K extends string, V = any>(userConfig?: Config): Cache<K, V
2323

2424
switch (config.policy) {
2525
case 'timeout':
26-
return createTimeoutStore<K, V>(config);
26+
return createTimeoutStore<V>(config);
2727
case 'lru':
28-
return createLruStore<K, V>(config);
28+
return createLruStore<V>(config);
2929
case 'mru':
30-
return createMruStore<K, V>(config);
30+
return createMruStore<V>(config);
3131
case 'tlru':
32-
return createTLruStore<K, V>(config);
32+
return createTLruStore<V>(config);
3333
default:
3434
throw new Error(config.policy + ' is not a supported policy.');
3535
}

src/timeout-store.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import { Cache, GlobalConfig } from './types';
22

33
import { createTimeAwareMapObserver } from './utils/TimeAwareMap';
44

5-
function createStore<K, V>(config: GlobalConfig): Cache<K, V> {
6-
const store = new Map<K, any>();
7-
const timer = createTimeAwareMapObserver<K>(config.timeToClear);
8-
function get(key: K) {
5+
function createStore<V>(config: GlobalConfig): Cache<V> {
6+
const store = new Map<string, any>();
7+
const timer = createTimeAwareMapObserver(config.timeToClear);
8+
function get(key: string): V {
99
const val = store.get(key);
1010
if (val) {
1111
return val;
1212
}
1313
return null;
1414
}
1515

16-
function remove(key: K) {
16+
function remove(key: string) {
1717
store.delete(key);
1818
timer.cancel(key);
1919
return true;
@@ -23,15 +23,15 @@ function createStore<K, V>(config: GlobalConfig): Cache<K, V> {
2323
return store.size;
2424
}
2525

26-
function has(key: K): boolean {
26+
function has(key: string): boolean {
2727
return store.has(key);
2828
}
2929

30-
function keys(): K[] {
30+
function keys(): string[] {
3131
return Array.from(store.keys());
3232
}
3333

34-
function set(key: K, value: V) {
34+
function set(key: string, value: V) {
3535
store.set(key, value);
3636
timer.create(key, remove);
3737
return true;

src/tlru-store.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
import { Cache, GlobalConfig } from './types';
22
import { TLRULinkedList } from './utils/TLRULinkedList';
33

4-
function createStore<K, V>(config: Required<GlobalConfig>): Cache<K, V> {
5-
const store = new TLRULinkedList<K, V>(config);
4+
function createStore<V>(config: Required<GlobalConfig>): Cache<V> {
5+
const store = new TLRULinkedList<V>(config);
66

7-
function get(key: K) {
7+
function get(key: string): V {
88
const val = store.get(key);
99
if (val) {
1010
return val;
1111
}
1212
return null;
1313
}
1414

15-
function remove(key: K) {
15+
function remove(key: string) {
1616
store.remove(key);
1717
return true;
1818
}
1919

20-
function has(key: K) {
20+
function has(key: string) {
2121
return store.has(key);
2222
}
2323

2424
function size(): number {
2525
return store.size;
2626
}
2727

28-
function keys(): K[] {
28+
function keys(): string[] {
2929
return store.keys();
3030
}
3131

32-
function set(key: K, value: any) {
32+
function set(key: string, value: V) {
3333
store.addNodeToHead(key, value);
3434
return true;
3535
}

src/types.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ export interface Config {
66
mruSize?: number;
77
}
88

9-
export interface CreateTimeoutResult<K> {
10-
create(key: K, removeFromStore: (key: K) => any): void;
11-
cancel(key: K): void;
9+
export interface CreateTimeoutResult {
10+
create(key: string, removeFromStore: (key: string) => any): void;
11+
cancel(key: string): void;
1212
}
1313

14-
export interface Cache<K, V> {
15-
keys(): K[];
14+
export interface Cache<V = any> {
15+
keys(): string[];
1616
size(): number;
17-
has(key: K): boolean;
18-
get(key: K): V | null;
19-
set(key: K, value: V): void;
20-
remove(key: K): void;
17+
has(key: string): boolean;
18+
get(key: string): V | null;
19+
set(key: string, value: V): void;
20+
remove(key: string): void;
2121
}
2222

2323
export interface GlobalConfig extends Config {}

0 commit comments

Comments
 (0)