Skip to content

Commit

Permalink
Refactor to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
taoyuan committed Feb 6, 2019
1 parent df87097 commit 2c9f253
Show file tree
Hide file tree
Showing 35 changed files with 2,846 additions and 1,318 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -14,3 +14,4 @@ results
npm-debug.log
node_modules
.idea
lib
85 changes: 37 additions & 48 deletions examples/example.js
@@ -1,59 +1,48 @@
var kvs = require('kvs');
const PromiseA = require('bluebird');
const kvs = require('..');

var redisStore = kvs.store('redis', {db: 1});
var redisBucket = redisStore.createBucket({ttl: 100/*seconds*/});
const TTL = 2;
const redisStore = kvs.store('redis', {db: 1});

var memoryStore = kvs.store('memory');
var memoryBucket = memoryStore.createBucket({max: 100, ttl: 10/*seconds*/});
(async () => {
const redisBucket = await redisStore.createBucket({ttl: TTL/*seconds*/});
// const memoryBucket = await memoryStore.createBucket({max: 100, ttl: 10/*seconds*/});

await redisBucket.set('foo', 'bar');
let result = await redisBucket.get('foo');
console.log(result);
await redisBucket.del('foo');

redisBucket.set('foo', 'bar', function (err) {
if (err) {
throw err;
}
const userId = 123;
const key = '#' + userId; // for test if key is not the parameter (user_id) to load.

redisBucket.get('foo', function (err, result) {
console.log(result);
// >> 'bar'
redisBucket.del('foo', function (err) {
});
});
});

function getUser(id, cb) {
setTimeout(function () {
console.log("Returning user from slow database.");
cb(null, {id: id, name: 'Bob'});
}, 100);
}
// Using namespace "user"
const redisLoadBucket = await redisStore.createBucket('user', {
ttl: TTL, /*seconds*/

var user_id = 123;
var key = '#' + user_id; // for test if key is not the parameter (user_id) to load.

// Using namespace "user"
var redisLoadBucket = redisStore.createBucket('user', {
ttl: 100, /*seconds*/

// method to load a thing if it's not in the bucket.
load: function (user_id, cb) {
// this method will only be called if it's not already in bucket, and will
// store the result in the bucket store.
getUser(user_id, cb);
}
});
// method to load a thing if it's not in the bucket.
load: loadUser
});

// `user_id` is the parameter used to load.
// if no parameter is specified for loading, the `key` will be used.
redisLoadBucket.get(key, user_id, function (err, user) {
// `user_id` is the parameter used to load.
// if no parameter is specified for loading, the `key` will be used.
let user = await redisLoadBucket.get(key, userId);
console.log(user);

// Second time fetches user from redisLoadBucket
redisLoadBucket.get(key, user_id, function (err, user) {
console.log(user);
});
});
user = await redisLoadBucket.get(key, userId);
console.log(user);

// Outputs:
// Returning user from slow database.
// { id: 123, name: 'Bob' }
// { id: 123, name: 'Bob' }

// Outputs:
// Returning user from slow database.
// { id: 123, name: 'Bob' }
// { id: 123, name: 'Bob' }
await redisStore.close();
})();

async function loadUser(id) {
await PromiseA.fromCallback(cb => setTimeout(cb, 100));
console.log("Returning user from slow database.");
return {id: id, name: 'Bob'};
}
8 changes: 0 additions & 8 deletions index.js

This file was deleted.

78 changes: 0 additions & 78 deletions lib/adapters/memory.js

This file was deleted.

204 changes: 0 additions & 204 deletions lib/adapters/redis.js

This file was deleted.

0 comments on commit 2c9f253

Please sign in to comment.