Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
perrin4869 committed May 22, 2016
0 parents commit d449325
Show file tree
Hide file tree
Showing 15 changed files with 341 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015-node4"]
}
6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "airbnb-base",
"rules": {
"no-param-reassign": 0
}
}
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Coverage directory used by tools like istanbul
coverage

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules

# Debug log from npm
npm-debug.log

# Build
lib
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: node_js
node_js:
- '6'
deploy:
api_key:
secure: QFWNaJIYNvoW56txvnqwuIBQjkw/V95IPChqpIG5rNTo+zpk3MS6D7FL2KI6E6WOKkkn0n8I4Hr3a7MtVlImRTiauZPxC6ZOttEJfJf3Kbuo0+aSPdR9r0gDGuPyijeyvrUCOB78l/OndkwqEyViG2OI964z/R/L5j/g9siPjIcwLi9JV08poXxAtbPQuMNSG42IZGvZ9iAuNGNZjFeUca08sjfW/3p4CBA2EJH6ayp6FTeebzkrPLsbRUy5qdf7o8aORrrDAyYOraCDP0nWc3pB7gIfZ+DdVKGWxILi4PCvqytYpUs9cz3DgBbWwLPUKCBUR4Vte1lQvzkn0XhkiPO7H5xf+t63zvuLAaU2gtpv4RQoKjHmSGOi7grqRi/H0cZE1O35QcNVzfHGasjVorOcM5kREIu884Sl/zLn8Ouia7hhvZ4hi+yBbDFbmf/hGcPs6obvIfnK5VoWf32wWEno2LQDbpbpXfovm+W9LTvPiAAMQ/AX+p6+1ObnMaccX3pR1rellhnX/zfUW8lo+V07xSrGZaYpoQzvYj7S70SaIjxBOri/ZnxvjIVt9N9XelDHTpLS+Dh9gDHCYji9fXbWhhcL/l2caOo2WZjz2fYBhe58cjpWhkRJxd8+Q3r5Fz1b2GF0iIglfqhXc9oZDd40qXNIqcH0i6KPKKKmxgk=
email: julian@dotcore.co.il
provider: npm
on:
tags: true
after_script:
- npm run coveralls
services:
- redis-server
19 changes: 19 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2016 Julian Grinblat

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.
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# redis-hmsetex

[![Travis][build-badge]][build]
[![npm package][npm-badge]][npm]
[![Coverage Status][coveralls-badge]][coveralls]
[![Dependency Status][dependency-status-badge]][dependency-status]
[![devDependency Status][dev-dependency-status-badge]][dev-dependency-status]

> hmsetex lua command for redis clients
## hmsetex redis command

Sets the value of a hash key and updates the expire date at the same time.

```
HMSETEX key 1 firstname walter lastname white
```

In the example above, key `key` expires in 1 second.

## Install

```
$ npm install --save redis-hmsetex
```

## Usage

The easiest usecase is to use with [https://github.com/luin/ioredis](ioredis) as follows:

```js
const Redis = require('ioredis');
const redis = new Redis();
require('redis-hmsetex').install(redis);
```

Additionally, the necessary information is exported:

```js
const Redis = require('ioredis');
const { name, lua, numberOfKeys } = require('hmsetex');
const redis = new Redis();
redis.defineCommand(name, { lua, numberOfKeys });
```

Then, just run like any other command:

```js
redis.hmsetex('some_key', 1, 'firstname', 'walter', 'lastname', 'white');
setTimeout(() => redis.hgetall('somekey'), 1100); // At this point, null is returned
```

## Tests

There are unit tests and integration tests. Needs Node 6+ to run. The integration tests require redis to be installed.

```bash
npm test:unit
npm test:integration
npm test # run both tests
```

## License

See the [LICENSE](LICENSE.md) file for license rights and limitations (MIT).

[build-badge]: https://img.shields.io/travis/perrin4869/redis-hmsetex/master.svg?style=flat-square
[build]: https://travis-ci.org/perrin4869/redis-hmsetex

[npm-badge]: https://img.shields.io/npm/v/redis-hmsetex.svg?style=flat-square
[npm]: https://www.npmjs.org/package/redis-hmsetex

[coveralls-badge]: https://img.shields.io/coveralls/perrin4869/redis-hmsetex/master.svg?style=flat-square
[coveralls]: https://coveralls.io/r/perrin4869/redis-hmsetex

[dependency-status-badge]: https://david-dm.org/perrin4869/redis-hmsetex.svg?style=flat-square
[dependency-status]: https://david-dm.org/perrin4869/redis-hmsetex

[dev-dependency-status-badge]: https://david-dm.org/perrin4869/redis-hmsetex/dev-status.svg?style=flat-square
[dev-dependency-status]: https://david-dm.org/perrin4869/redis-hmsetex#info=devDependencies
35 changes: 35 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const gulp = require('gulp');
const util = require('gulp-util');
const mocha = require('gulp-mocha');
const rename = require('gulp-rename');
const lua2js = require('gulp-redis-lua2js');
const compiler = require('babel-core/register');

const src = 'src/hmsetex.lua';

gulp.task('build', () => (
gulp.src(src)
.pipe(lua2js({ numberOfKeys: 1 }))
.pipe(rename(path => {
path.basename = 'index';
}))
.pipe(gulp.dest('lib'))
));

gulp.task('test', ['build'], () => (
gulp.src('test')
.pipe(mocha({
compilers: { // TODO: remove once gulp-mocha supports test/mocha.opts
js: compiler,
},
}))
.on('error', util.log)
));

gulp.task('watch', () => {
gulp.watch(src, ['test']);
});

gulp.task('develop', ['watch']);

gulp.task('default', ['develop']);
54 changes: 54 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "redis-hmsetex",
"version": "1.0.0",
"description": "hmsetex lua command for redis clients",
"main": "lib/index.js",
"scripts": {
"test": "gulp test",
"test:unit": "mocha test/unit",
"test:integration": "mocha test/integration",
"build": "gulp build",
"cover": "babel-node node_modules/isparta/bin/isparta cover --report text --report html node_modules/mocha/bin/_mocha",
"coveralls": "npm run cover -- --report lcovonly && cat ./coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf ./coverage",
"prepublish": "npm run build"
},
"files": [
"lib"
],
"repository": {
"type": "git",
"url": "git+https://github.com/perrin4869/redis-hmsetex.git"
},
"keywords": [
"redis"
],
"author": "Julian Grinblat <julian@dotcore.co.il>",
"license": "MIT",
"bugs": {
"url": "https://github.com/perrin4869/redis-hmsetex/issues"
},
"homepage": "https://github.com/perrin4869/redis-hmsetex#readme",
"devDependencies": {
"babel-cli": "^6.9.0",
"babel-core": "^6.9.0",
"babel-eslint": "^6.0.4",
"babel-plugin-transform-async-to-generator": "^6.8.0",
"babel-preset-es2015-node4": "^2.1.0",
"babel-preset-es2015-node6": "^0.2.0",
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"eslint": "^2.10.2",
"eslint-config-airbnb-base": "^3.0.1",
"eslint-plugin-import": "^1.8.0",
"gulp": "^3.9.1",
"gulp-mocha": "^2.2.0",
"gulp-redis-lua2js": "^1.0.0",
"gulp-rename": "^1.2.2",
"gulp-util": "^3.0.7",
"ioredis": "^1.15.1",
"isparta": "^4.0.0",
"mocha": "^2.4.5",
"redis-pdel": "^1.0.2",
"sinon": "^1.17.4"
}
}
7 changes: 7 additions & 0 deletions src/hmsetex.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--!/usr/bin/env lua

local expire = table.remove(ARGV, 1)
local result = redis.call("HMSET", KEYS[1], unpack(ARGV))
redis.call("EXPIRE", KEYS[1], expire)

return result
4 changes: 4 additions & 0 deletions test/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["es2015-node6"],
"plugins": ["transform-async-to-generator"]
}
10 changes: 10 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"parser": "babel-eslint",
"env": {
"mocha": true
},
"rules": {
"global-require": 0,
"func-names": 0
}
}
4 changes: 4 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
describe('redis-hmsetex', () => {
require('./unit');
require('./integration');
});
54 changes: 54 additions & 0 deletions test/integration/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import Redis from 'ioredis';
import Promise from 'bluebird';
import { expect } from 'chai';
import { install as pdel } from 'redis-pdel';
import { install as hmsetex } from '../../lib';

describe('integration', () => {
const keyPrefix = 'hmsetex:test:';
const redis = new Redis({ keyPrefix });
pdel(redis);
hmsetex(redis);

beforeEach(async () => {
await redis.pdel('*');

const res = await redis.hmsetex('testhash', 1, 'foo', 'bar', 'baz', 'baq');
expect(res).to.equal('OK');
});

after(async () => {
await redis.disconnect();
});

it('should not have expired yet', async () => {
await Promise.delay(100);

const res = await redis
.multi()
.hget('testhash', 'foo')
.hget('testhash', 'baz')
.pttl('testhash')
.exec();

expect(res[0][1]).to.equal('bar');
expect(res[1][1]).to.equal('baq');
expect(res[2][1]).to.be.lessThan(1000).and.greaterThan(500);
});

it('should have expired yet', async function() {
this.slow(1100);

await Promise.delay(1000);
const res = await redis
.multi()
.hget('testhash', 'foo')
.hget('testhash', 'baz')
.pttl('testhash')
.exec();

expect(res[0][1]).to.equal(null);
expect(res[1][1]).to.equal(null);
expect(res[2][1]).to.be.lessThan(0);
});
});
3 changes: 3 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-R spec
--ui bdd
--compilers js:babel-core/register
36 changes: 36 additions & 0 deletions test/unit/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import path from 'path';
import sinon from 'sinon';
import { expect } from 'chai';
import { readFileSync } from 'fs';

import { name, lua, numberOfKeys, install } from '../../lib';

const expectation = {
name: 'hmsetex',
lua: readFileSync(path.join(__dirname, '..', '..', 'src', 'hmsetex.lua'), 'utf8'),
numberOfKeys: 1,
};

describe('unit', () => {
it('should export correct object literal', () => {
expect(name).to.equal(expectation.name);
expect(lua).to.equal(expectation.lua);
expect(numberOfKeys).to.equal(expectation.numberOfKeys);
});

it('should install command into ioredis', () => {
const ioredis = {
defineCommand: sinon.spy(),
};

install(ioredis);
expect(ioredis.defineCommand.calledOnce).to.equal(true);
expect(ioredis.defineCommand.firstCall.args).to.deep.equal([
expectation.name,
{
lua: expectation.lua,
numberOfKeys: expectation.numberOfKeys,
},
]);
});
});

0 comments on commit d449325

Please sign in to comment.