Skip to content

Commit

Permalink
feat(namespace): use namespaces
Browse files Browse the repository at this point in the history
BREAKING CHANGE: requires setting a namespace
  • Loading branch information
Ahmad Nassri committed Nov 2, 2018
1 parent 4567b7f commit 7a94e8e
Show file tree
Hide file tree
Showing 19 changed files with 3,803 additions and 227 deletions.
17 changes: 0 additions & 17 deletions .codeclimate.yml

This file was deleted.

1 change: 0 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# http://editorconfig.org
root = true

[*]
Expand Down
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@ahmadnassri/eslint-config"
}
16 changes: 16 additions & 0 deletions .github/settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
_extends: .github

repository:
name: simple-file-cache
description: a simple and easy to use file-based cache
topics: simple,file,cache
homepage: https://github.com/ahmadnassri/simple-file-cache
private: false
has_issues: true
has_wiki: false
has_downloads: false
has_projects: false
default_branch: master
allow_squash_merge: true
allow_merge_commit: false
allow_rebase_merge: true
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
*.log
/node_modules
/.nyc_output
/.cache
/coverage
/node_modules
/test/.cache
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
access=public
3 changes: 3 additions & 0 deletions .releaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@ahmadnassri/semantic-release-config"
}
5 changes: 5 additions & 0 deletions .remarkrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": [
"@ahmadnassri/remark-config"
]
}
36 changes: 0 additions & 36 deletions .travis.yml

This file was deleted.

22 changes: 9 additions & 13 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
Copyright (c) 2015, Ahmad Nassri <ahmad@ahmadnassri.com>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
MIT License

Copyright (c) 2018 Ahmad Nassri <ahmad@ahmadnassri.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
63 changes: 25 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
# `simple-file-cache` [![version][npm-version]][npm-url] [![License][license-image]][license-url]
# Simple File Cache

> a simple and easy to use file-based cache
[![License][license-image]][license-url] [![version][npm-image]][npm-url] [![Build Status][circle-image]][circle-url]

[![Build Status][travis-image]][travis-url]
[![Downloads][npm-downloads]][npm-url]
[![Code Climate][codeclimate-quality]][codeclimate-url]
[![Coverage Status][codeclimate-coverage]][codeclimate-url]
[![Dependency Status][dependencyci-image]][dependencyci-url]
[![Dependencies][david-image]][david-url]
> a simple and easy to use file-based cache
## Install

```bash
# npm
npm install --production --save simple-file-cache

# yarn
yarn add simple-file-cache
npm install @ahmadnassri/simple-file-cache
```

## API

### `_(namespace)`

Create a new cache name space

```js
const simple = require('@ahmadnassri/simple-file-cache')

const cache = simple('my-awesome-app')

cache.set('foo', 'bar', 100)
```

### `set(key, value[, ttl])`

Creates a cache record using the `key`:`value` pair, and optionally sets an expiry date using `ttl` in milliseconds.

```js
const cache = require('simple-file-cache')

cache.set('foo', 'bar', 100)
```

Expand All @@ -36,37 +37,23 @@ cache.set('foo', 'bar', 100)
retrieves a cache record identified with `key`, if the expiry time has elapsed, the record will be deleted and return a value of `null`

```js
const cache = require('simple-file-cache')

cache.get('foo')
```

### `process.env.SIMPLE_CACHE_DIR`

path to store the cache records, defaults to `.cache`
path to store the cache records, defaults to `$XDG_CACHE_HOME`

---
> :copyright: [ahmadnassri.com](https://www.ahmadnassri.com/)  · 
> License: [ISC][license-url]  · 
> Github: [@ahmadnassri](https://github.com/ahmadnassri)  · 
> Author: [Ahmad Nassri](https://www.ahmadnassri.com) &bull;
> Github: [@ahmadnassri](https://github.com/ahmadnassri) &bull;
> Twitter: [@ahmadnassri](https://twitter.com/ahmadnassri)
[license-url]: http://choosealicense.com/licenses/isc/
[license-image]: https://img.shields.io/github/license/ahmadnassri/simple-file-cache.svg?style=flat-square

[travis-url]: https://travis-ci.org/ahmadnassri/simple-file-cache
[travis-image]: https://img.shields.io/travis/ahmadnassri/simple-file-cache.svg?style=flat-square

[npm-url]: https://www.npmjs.com/package/simple-file-cache
[npm-version]: https://img.shields.io/npm/v/simple-file-cache.svg?style=flat-square
[npm-downloads]: https://img.shields.io/npm/dm/simple-file-cache.svg?style=flat-square

[codeclimate-url]: https://codeclimate.com/github/ahmadnassri/simple-file-cache
[codeclimate-quality]: https://img.shields.io/codeclimate/github/ahmadnassri/simple-file-cache.svg?style=flat-square
[codeclimate-coverage]: https://img.shields.io/codeclimate/coverage/github/ahmadnassri/simple-file-cache.svg?style=flat-square
[license-url]: LICENSE
[license-image]: https://img.shields.io/github/license/ahmadnassri/simple-file-cache.svg?style=for-the-badge&logo=circleci

[david-url]: https://david-dm.org/ahmadnassri/simple-file-cache
[david-image]: https://img.shields.io/david/ahmadnassri/simple-file-cache.svg?style=flat-square
[circle-url]: https://circleci.com/gh/ahmadnassri/simple-file-cache
[circle-image]: https://img.shields.io/circleci/project/github/ahmadnassri/simple-file-cache/master.svg?style=for-the-badge&logo=circleci

[dependencyci-url]: https://dependencyci.com/github/ahmadnassri/simple-file-cache
[dependencyci-image]: https://dependencyci.com/github/ahmadnassri/simple-file-cache/badge?style=flat-square
[npm-url]: https://www.npmjs.com/package/@ahmadnassri/simple-file-cache
[npm-image]: https://img.shields.io/npm/v/@ahmadnassri/simple-file-cache.svg?style=for-the-badge&logo=npm
69 changes: 69 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
version: 2.1

executors:
app:
working_directory: ~/src

docker:
- image: ahmadnassri/build-essential

commands:
persist:
steps:
- persist_to_workspace: { root: ~/src, paths: . }

attach:
steps:
- attach_workspace: { at: "." }

install:
steps:
- run:
name: Install dependencies
command: npm ci

jobs:
build:
executor: app
steps:
- checkout
- install
- persist

lint:
executor: app
steps:
- attach
- run: npm run lint

unit:
executor: app
steps:
- attach
- run: npm run test

release:
executor: app
steps:
- attach
- run: npx semantic-release

workflows:
version: 2
run:
jobs:
- build:
context: npm

- lint:
requires: [ build ]

- unit:
requires: [ build ]

- release:
context: npm
requires: [ build, lint, unit ]
filters:
branches:
only: master
51 changes: 51 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const { join } = require('path')
const { writeFileSync, statSync, readFileSync, unlinkSync } = require('fs')
const dir = require('cache-directory')
const mkdirp = require('mkdirp')

const md5 = require('./lib/md5')

module.exports = namespace => {
/* istanbul ignore next */ const root = process.env.SIMPLE_CACHE_DIR || dir(namespace || 'simple-cache') || '.cache' // ignore line in coverage

function set (key, data, ttl) {
// construct the full path
const file = join(root, md5(key))

// ensure cache directory exists
mkdirp(root)

// create the file record
writeFileSync(file, JSON.stringify({ ttl, data }))
}

function get (key) {
// construct the full path
const file = join(root, md5(key))

// parse the file content
const record = JSON.parse(readFileSync(file))

if (record.ttl) {
// get time data
const stats = statSync(file)

// setup time in milliseconds
const now = new Date().getTime()
const mtime = new Date(stats.mtime).getTime()

// check file age
if (now - mtime > record.ttl) {
// delete if old
unlinkSync(file)

return null
}
}

// return the record data
return record.data
}

return { set, get }
}
File renamed without changes.

0 comments on commit 7a94e8e

Please sign in to comment.