├── .editorconfig ├── .gitattributes ├── .gitignore ├── .travis.yml ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [{package.json,*.yml}] 11 | indent_style = space 12 | indent_size = 2 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '4' 4 | - '0.12' 5 | - '0.10' 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var fs = require('fs'); 4 | var path = require('path'); 5 | var homeOrTmp = require('home-or-tmp'); 6 | var PinkiePromise = require('pinkie-promise'); 7 | var mkdirp = require('mkdirp'); 8 | var pathIsAbsolute = require('path-is-absolute'); 9 | 10 | var pify = require('pify'); 11 | var fsP = pify.all(fs, PinkiePromise); 12 | 13 | function handleEnoent(err) { 14 | if (err.code === 'ENOENT') { 15 | return undefined; 16 | } 17 | 18 | throw err; 19 | } 20 | 21 | function Cacha(namespace, opts) { 22 | if (!(this instanceof Cacha)) { 23 | return new Cacha(namespace, opts); 24 | } 25 | 26 | if (typeof namespace !== 'string') { 27 | throw new TypeError('namespace expected to be a string'); 28 | } 29 | 30 | this.ns = namespace; 31 | 32 | if (pathIsAbsolute(this.ns)) { 33 | this.path = this.ns; 34 | } else { 35 | this.path = path.join(homeOrTmp, this.ns); 36 | } 37 | 38 | this.opts = opts || {}; 39 | this.opts.ttl = this.opts.ttl === undefined ? 86400000 : this.opts.ttl; 40 | 41 | mkdirp.sync(this.path); 42 | } 43 | 44 | Cacha.prototype.set = function set(id, content, opts) { 45 | var entryPath = path.join(this.path, id); 46 | 47 | return fsP.writeFile(entryPath, content, opts).then(function () { 48 | return content; 49 | }); 50 | }; 51 | 52 | Cacha.prototype.setSync = function setSync(id, content, opts) { 53 | var entryPath = path.join(this.path, id); 54 | fs.writeFileSync(entryPath, content, opts); 55 | return content; 56 | }; 57 | 58 | Cacha.prototype.get = function get(id, opts) { 59 | var self = this; 60 | var entryPath = path.join(this.path, id); 61 | 62 | return fsP.stat(entryPath) 63 | .then(function (stats) { 64 | if (Date.now() - Number(stats.atime) > self.opts.ttl) { 65 | return undefined; 66 | } 67 | 68 | return fsP.readFile(entryPath, opts); 69 | }) 70 | .catch(handleEnoent); 71 | }; 72 | 73 | Cacha.prototype.getSync = function getSync(id, opts) { 74 | var self = this; 75 | var entryPath = path.join(this.path, id); 76 | var stats; 77 | 78 | try { 79 | stats = fs.statSync(entryPath); 80 | } catch (err) { 81 | if (err.code !== 'ENOENT') { 82 | throw err; 83 | } 84 | 85 | return undefined; 86 | } 87 | 88 | if (Date.now() - Number(stats.atime) > self.opts.ttl) { 89 | return undefined; 90 | } 91 | 92 | return fs.readFileSync(entryPath, opts); 93 | }; 94 | 95 | Cacha.prototype.clean = function clean() { 96 | var ttl = this.opts.ttl; 97 | var cacheDir = this.path; 98 | var files = fs.readdirSync(cacheDir); 99 | 100 | files.forEach(function (id) { 101 | var file = path.join(cacheDir, id); 102 | var atime = fs.statSync(file).atime; 103 | 104 | if (Date.now() - atime > ttl) { 105 | fs.unlinkSync(file); 106 | } 107 | }); 108 | }; 109 | 110 | module.exports = Cacha; 111 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Vsevolod Strukchinsky (github.com/floatdrop) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cacha", 3 | "version": "1.0.3", 4 | "description": "My hunky-dory module", 5 | "license": "MIT", 6 | "repository": "floatdrop/cacha", 7 | "author": { 8 | "name": "Vsevolod Strukchinsky", 9 | "email": "floatdrop@gmail.com", 10 | "url": "github.com/floatdrop" 11 | }, 12 | "engines": { 13 | "node": ">=0.10.0" 14 | }, 15 | "scripts": { 16 | "test": "xo && ava" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "" 23 | ], 24 | "dependencies": { 25 | "home-or-tmp": "^2.0.0", 26 | "mkdirp": "^0.5.1", 27 | "path-is-absolute": "^1.0.0", 28 | "pify": "^2.3.0", 29 | "pinkie-promise": "^2.0.0" 30 | }, 31 | "devDependencies": { 32 | "ava": "^0.4.0", 33 | "mock-fs": "^3.4.0", 34 | "xo": "^0.10.0" 35 | }, 36 | "xo": { 37 | "ignores": [ 38 | "test.js" 39 | ] 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # cacha [![Build Status](https://travis-ci.org/floatdrop/cacha.svg?branch=master)](https://travis-ci.org/floatdrop/cacha) 2 | 3 | > Cache on file-system 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install --save cacha 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```js 16 | const Cache = require('cacha'); 17 | const cache = new Cacha('.my/cache'); 18 | 19 | cache.set('id', 'content'); 20 | //=> Promise 21 | 22 | cache.get('id'); 23 | //=> Promise with 'content' 24 | ``` 25 | 26 | 27 | ## API 28 | 29 | ### cacha(namespace, [options]) 30 | 31 | #### namespace 32 | 33 | Type: `string` 34 | 35 | Directory in HOME or TMP directory of current user. 36 | 37 | If namespace begins with `/` it will be interpreted as absolute path. 38 | 39 | #### options 40 | 41 | ##### ttl 42 | 43 | Type: `Number` 44 | Default: `86400000` 45 | 46 | How long (in milliseconds) keep entries in cache. 47 | 48 | 49 | ### cache.get(id, [options]) 50 | 51 | ### cache.getSync(id, [options]) 52 | 53 | ### cache.set(id, content, [options]) 54 | 55 | ### cache.setSync(id, content, [options]) 56 | 57 | Get and set methods for cache entries. `options` are passed to `fs` write and read methods (for example to specify encoding). 58 | 59 | ### cache.clean() 60 | 61 | Removes outdated entries in cache. 62 | 63 | ## License 64 | 65 | MIT © [Vsevolod Strukchinsky](http://github.com/floatdrop) 66 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import fs from 'fs'; 2 | import test from 'ava'; 3 | import mockFs from 'mock-fs'; 4 | import homeOrTmp from 'home-or-tmp'; 5 | import Cacha from './'; 6 | 7 | function time(ms) { 8 | return new Promise(resolve => setTimeout(resolve, ms)) 9 | } 10 | 11 | mockFs({}); 12 | 13 | test('namespace is required', t => { 14 | t.throws(function () { 15 | Cacha(); 16 | }, /namespace expected to be a string/); 17 | t.end(); 18 | }); 19 | 20 | test('sets entity in cache by absolute path', async t => { 21 | const cache = new Cacha('/.absolutly'); 22 | 23 | t.is(await cache.set('id1', '1'), '1'); 24 | t.is(fs.readFileSync('/.absolutly/id1', 'utf8'), '1'); 25 | 26 | t.is(cache.setSync('id2', '2'), '2'); 27 | t.is(fs.readFileSync('/.absolutly/id2', 'utf8'), '2'); 28 | }); 29 | 30 | test('sets entity in cache by relative path', async t => { 31 | const cache = new Cacha('.relativly'); 32 | 33 | t.is(await cache.set('id1', '1'), '1'); 34 | t.is(fs.readFileSync(homeOrTmp + '/.relativly/id1', 'utf8'), '1'); 35 | 36 | t.is(cache.setSync('id2', '2'), '2'); 37 | t.is(fs.readFileSync(homeOrTmp + '/.relativly/id2', 'utf8'), '2'); 38 | }); 39 | 40 | test('gets entity', async t => { 41 | const cache = new Cacha('.gets'); 42 | 43 | t.is(await cache.set('id1', '1', 'utf8'), '1'); 44 | t.is(await cache.get('id1', 'utf8'), '1'); 45 | 46 | t.is(cache.setSync('id2', '2', 'utf8'), '2'); 47 | t.is(cache.getSync('id2', 'utf8'), '2'); 48 | 49 | t.is(cache.getSync('id3', 'utf8'), undefined); 50 | }); 51 | 52 | test('supports ttl', async t => { 53 | const cache = new Cacha('.ttl', {ttl: 10}); 54 | 55 | t.is(await cache.set('id1', '1', 'utf8'), '1'); 56 | 57 | await time(100); 58 | 59 | t.is(await cache.get('id1', 'utf8'), undefined); 60 | }); 61 | 62 | test('clean', async t => { 63 | const cache = new Cacha('.clean', {ttl: 50}); 64 | await cache.set('id1', '1'); 65 | await cache.set('id2', '2'); 66 | 67 | t.ok(await cache.get('id1')); 68 | t.ok(await cache.get('id2')); 69 | 70 | await time(100); 71 | 72 | cache.clean(); 73 | 74 | t.is(await cache.get('id1'), undefined); 75 | t.is(await cache.get('id2'), undefined); 76 | }); 77 | 78 | test('updates atime', async t => { 79 | const cache = new Cacha('/mtime'); 80 | 81 | t.is(await cache.set('id1', '1', 'utf8'), '1'); 82 | const oldTime = Number(fs.statSync('/mtime/id1').atime); 83 | 84 | await time(100); 85 | 86 | t.is(await cache.get('id1', 'utf8'), '1'); 87 | const newTime = Number(fs.statSync('/mtime/id1').atime); 88 | 89 | t.ok(newTime > oldTime) 90 | }); 91 | --------------------------------------------------------------------------------