in-memory-services 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +114 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# In memory services
|
|
2
|
+
|
|
3
|
+
## Summary
|
|
4
|
+
|
|
5
|
+
A library that mimics services and tools for distributed systems, but in memory, for single deployment units.
|
|
6
|
+
|
|
7
|
+
As of now mimics Redis caching, as pre-step to migrate to the actual service or if only caching in memory with expiration is needed.
|
|
8
|
+
|
|
9
|
+
## Installing and using
|
|
10
|
+
|
|
11
|
+
`npm i in-memory-services`
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { cache } from 'in-memory-services';
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Caching
|
|
18
|
+
|
|
19
|
+
A subset of operations intended to be like Redis. Mostly getting/setting and expiring
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
get('key'); // => returns stored key
|
|
23
|
+
|
|
24
|
+
get('unknown_key'); // => returns null
|
|
25
|
+
|
|
26
|
+
set('key', 'value'); // => sets <K,V> pair that won't expire
|
|
27
|
+
|
|
28
|
+
set('key', { obj1: { obj2: 'value' } }); // allows more types than string
|
|
29
|
+
|
|
30
|
+
set('key', 'value', 10); // set a <K,V> that will expire in 10 seconds
|
|
31
|
+
|
|
32
|
+
set('key', null); // throws an error
|
|
33
|
+
|
|
34
|
+
getOrSet('key', function, 10); // tries to get key, if it fails, uses function to set that value and returns it
|
|
35
|
+
|
|
36
|
+
del('key'); // deletes stored key
|
|
37
|
+
|
|
38
|
+
del(['key', 'keys']); // deletes stored keys
|
|
39
|
+
|
|
40
|
+
exists('key'); // => 1 if key exists, 0 otherwise
|
|
41
|
+
|
|
42
|
+
exists(['key', 'key2', 'unknown_key']); //2 => returns amount of keys that exist
|
|
43
|
+
|
|
44
|
+
expire('key', 10); // adds TTL to key, will expire in 10s
|
|
45
|
+
|
|
46
|
+
ttl('key'); // returns how much time until key expires in seconds
|
|
47
|
+
|
|
48
|
+
persist('key'); // removes TTL of key, it won't be deleted automatically
|
|
49
|
+
|
|
50
|
+
keys(); // returns all cache keys
|
|
51
|
+
|
|
52
|
+
size(); // returns number of cache keys
|
|
53
|
+
|
|
54
|
+
clear(); // clears cache
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Example
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
>> cache.set('key', 'test', 10);
|
|
61
|
+
>> (void)
|
|
62
|
+
>> cache.get('key');
|
|
63
|
+
>> 'test'
|
|
64
|
+
>> cache.get('another-key');
|
|
65
|
+
>> null
|
|
66
|
+
>> cache.ttl('key');
|
|
67
|
+
>> 10 (or a bit less depending on time elapsed)
|
|
68
|
+
>> cache.persist('key')
|
|
69
|
+
>> (void)
|
|
70
|
+
>> cache.ttl('key')
|
|
71
|
+
>> -1
|
|
72
|
+
>> cache.del('key')
|
|
73
|
+
>> 1
|
|
74
|
+
>> cache.del('key')
|
|
75
|
+
>> 0
|
|
76
|
+
>> cache.get('key')
|
|
77
|
+
>> null
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Use case
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { cache } from 'in-memory-services';
|
|
84
|
+
|
|
85
|
+
let value = cache.get('KEY');
|
|
86
|
+
|
|
87
|
+
if (!value){
|
|
88
|
+
value = await fetchValue(...);
|
|
89
|
+
|
|
90
|
+
if (value) cache.set('KEY', value, 3600); // cache for an hour
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Use case using getOrSet
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
import { cache } from 'in-memory-services';
|
|
98
|
+
|
|
99
|
+
// tries to get a key, if it fails it uses the provided function to get the value and sets timers
|
|
100
|
+
// this can throw if provided function throws or rejects (if promise)
|
|
101
|
+
const fetchValue = () => 0;
|
|
102
|
+
let value = cache.getOrSet('KEY', fetchValue, 3600);
|
|
103
|
+
|
|
104
|
+
const fetchValuePromise = async () => {
|
|
105
|
+
/* example: fetch call promise */
|
|
106
|
+
};
|
|
107
|
+
let value2 = cache.getOrSet('KEY2', fetchValuePromise, 3600);
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Roadmap (?)
|
|
111
|
+
|
|
112
|
+
- Caching ✓
|
|
113
|
+
- Message Broker X
|
|
114
|
+
- Queue X
|
package/package.json
CHANGED