1
1
# cacache [ ![ npm version] ( https://img.shields.io/npm/v/cacache.svg )] ( https://npm.im/cacache ) [ ![ license] ( https://img.shields.io/npm/l/cacache.svg )] ( https://npm.im/cacache ) [ ![ Travis] ( https://img.shields.io/travis/zkat/cacache.svg )] ( https://travis-ci.org/zkat/cacache ) [ ![ AppVeyor] ( https://ci.appveyor.com/api/projects/status/github/zkat/cacache?svg=true )] ( https://ci.appveyor.com/project/zkat/cacache ) [ ![ Coverage Status] ( https://coveralls.io/repos/github/zkat/cacache/badge.svg?branch=latest )] ( https://coveralls.io/github/zkat/cacache?branch=latest )
2
2
3
3
[ ` cacache ` ] ( https://github.com/zkat/cacache ) is a Node.js library for managing
4
- caches of keyed data that can be looked up both by key and by a digest of the
5
- content itself. This means that by-content lookups can be very very fast, and
6
- that stored content is shared by different keys if they point to the same data.
4
+ local key and content address caches. It's really fast, really good at
5
+ concurrency, and it will never give you corrupted data, even if cache files
6
+ get corrupted or manipulated.
7
+
8
+ It was originally written to be used as [ npm] ( https://npm.im ) 's local cache, but
9
+ can just as easily be used on its own
7
10
8
11
## Install
9
12
@@ -41,19 +44,14 @@ const key = 'my-unique-key-1234'
41
44
let tarballDigest = null
42
45
43
46
// Cache it! Use `cachePath` as the root of the content cache
44
- fs .createReadStream (
45
- tarball
46
- ).pipe (
47
- cacache .put .stream (
48
- cachePath, key
49
- ).on (' digest' , (d ) => tarballDigest = d)
50
- ).on (' finish' , function () {
51
- console .log (` Saved ${ tarball} to ${ cachePath} .` )
47
+ cacache .put (cachePath, key, ' 10293801983029384' ).then (digest => {
48
+ console .log (` Saved content to ${ cachePath} .` )
52
49
})
53
50
54
51
const destination = ' /tmp/mytar.tgz'
55
52
56
53
// Copy the contents out of the cache and into their destination!
54
+ // But this time, use stream instead!
57
55
cacache .get .stream (
58
56
cachePath, key
59
57
).pipe (
@@ -63,23 +61,24 @@ cacache.get.stream(
63
61
})
64
62
65
63
// The same thing, but skip the key index.
66
- cacache .get .stream .byDigest (
67
- cachePath, tarballDigest
68
- ).pipe (
69
- fs .createWriteStream (destination)
70
- ).on (' finish' , () => {
71
- console .log (' done extracting using sha1!' )
64
+ cacache .get .byDigest (cachePath, tarballSha512).then (data => {
65
+ fs .writeFile (destination, data, err => {
66
+ console .log (' tarball data fetched based on its sha512sum and written out!' )
67
+ })
72
68
})
73
69
```
74
70
75
71
### Features
76
72
77
- * Extraction by key or by content digest (shasum, etc).
78
- * Deduplicated content by digest -- two inputs with same key are only saved once
79
- * Consistency checks, both on insert and extract.
80
- * (Kinda) concurrency-safe and fault tolerant.
81
- * Streaming support.
82
- * Metadata storage.
73
+ * Extraction by key or by content address (shasum, etc)
74
+ * Multi-hash support - safely host sha1, sha512, etc, in a single cache
75
+ * Automatic content deduplication
76
+ * Fault tolerance and consistency guarantees for both insertion and extraction
77
+ * Lockless, high-concurrency cache access
78
+ * Streaming support
79
+ * Promise support
80
+ * Arbitrary metadata storage
81
+ * Garbage collection and additional offline verification
83
82
84
83
### Contributing
85
84
@@ -102,7 +101,8 @@ cacache.ls(cachePath).then(console.log)
102
101
' my-thing' : {
103
102
key: ' my-thing' ,
104
103
digest: ' deadbeef' ,
105
- path: ' .testcache/content/deadbeef' ,
104
+ hashAlgorithm: ' sha512' ,
105
+ path: ' .testcache/content/deadbeef' , // joined with `cachePath`
106
106
time: 12345698490 ,
107
107
metadata: {
108
108
name: ' blah' ,
@@ -113,18 +113,21 @@ cacache.ls(cachePath).then(console.log)
113
113
' other-thing' : {
114
114
key: ' other-thing' ,
115
115
digest: ' bada55' ,
116
+ hashAlgorithm: ' whirlpool' ,
116
117
path: ' .testcache/content/bada55' ,
117
118
time: 11992309289
118
119
}
119
120
}
120
121
```
121
122
122
- #### <a name =" get-data " ></a > ` > cacache.get(cache, key, [opts]) -> Promise({data, metadata, digest}) `
123
+ #### <a name =" get-data " ></a > ` > cacache.get(cache, key, [opts]) -> Promise({data, metadata, digest, hashAlgorithm }) `
123
124
124
125
Returns an object with the cached data, digest, and metadata identified by
125
126
` key ` . The ` data ` property of this object will be a ` Buffer ` instance that
126
127
presumably holds some data that means something to you. I'm sure you know what
127
- to do with it! cacache just won't care.
128
+ to do with it! cacache just won't care. ` hashAlgorithm ` is the algorithm used
129
+ to calculate the ` digest ` of the content. This algorithm must be used if you
130
+ fetch later with ` get.byDigest ` .
128
131
129
132
If there is no content identified by ` key ` , or if the locally-stored data does
130
133
not pass the validity checksum, the promise will be rejected.
@@ -250,9 +253,7 @@ cache entry has been successfully written.
250
253
fetch (
251
254
' https://registry.npmjs.org/cacache/-/cacache-1.0.0.tgz'
252
255
).then (data => {
253
- cacache .put (
254
- cachePath, ' registry.npmjs.org|cacache@1.0.0' , data
255
- )
256
+ return cacache .put (cachePath, ' registry.npmjs.org|cacache@1.0.0' , data)
256
257
}).then (digest => {
257
258
console .log (' digest is' , digest)
258
259
})
0 commit comments