Skip to content
This repository was archived by the owner on Jul 3, 2019. It is now read-only.

Commit 31bc549

Browse files
committed
feat(cache): add versioning to content and index
Fixes: #50 This will allow us to do automated migrations in the future as the cache format changes. BREAKING CHANGE: index/content directories are now versioned. Previous caches are no longer compatible and cannot be migrated.
1 parent 03f81ba commit 31bc549

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

lib/content/path.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
'use strict'
22

3+
var contentVer = require('../../package.json')['cache-version'].content
34
var path = require('path')
45

6+
// Current format of content file path:
7+
//
8+
// ~/.my-cache/content-v1/sha512/ba/bada55deadbeefc0ffee
9+
//
510
module.exports = contentPath
611
function contentPath (cache, address, hashAlgorithm) {
712
address = address && address.toLowerCase()
813
hashAlgorithm = hashAlgorithm ? hashAlgorithm.toLowerCase() : 'sha512'
914
return path.join(
10-
cache, 'content', hashAlgorithm, address.slice(0, 2), address)
15+
cache,
16+
`content-v${contentVer}`,
17+
hashAlgorithm,
18+
address.slice(0, 2),
19+
address
20+
)
1121
}

lib/entry-index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const Promise = require('bluebird')
1212
const split = require('split')
1313
const through = require('mississippi').through
1414

15+
const indexV = require('../package.json')['cache-version'].index
16+
1517
module.exports.insert = insert
1618
function insert (cache, key, digest, opts) {
1719
opts = opts || {}
@@ -105,7 +107,7 @@ function del (cache, key) {
105107

106108
module.exports.lsStream = lsStream
107109
function lsStream (cache) {
108-
const indexDir = path.join(cache, 'index')
110+
const indexDir = bucketDir(cache)
109111
const stream = through.obj()
110112
fs.readdir(indexDir, function (err, buckets) {
111113
if (err && err.code === 'ENOENT') {
@@ -177,10 +179,14 @@ function notFoundError (cache, key) {
177179
return err
178180
}
179181

182+
function bucketDir (cache) {
183+
return path.join(cache, `index-v${indexV}`)
184+
}
185+
180186
module.exports._bucketPath = bucketPath
181187
function bucketPath (cache, key) {
182188
const hashed = hashKey(key)
183-
return path.join(cache, 'index', hashed.slice(0, 2), hashed)
189+
return path.join(bucketDir(cache), hashed.slice(0, 2), hashed)
184190
}
185191

186192
module.exports._hashKey = hashKey

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
{
22
"name": "cacache",
33
"version": "5.0.3",
4+
"cache-version": {
5+
"content": "1",
6+
"index": "1"
7+
},
48
"description": "General content-addressable cache system that maintains a filesystem registry of file data.",
59
"main": "index.js",
610
"files": [

0 commit comments

Comments
 (0)