Giter VIP home page Giter VIP logo

redis-counter's Introduction

redis-counter

This is a trivial redis auto-increment counter implementation.

NPM info

Travis build status

Install

npm install redis-counter

Initialization

   // Initialization via redis URLs of the form: redis://user:password@host:port/
   var counters = require('redis-counter').createCounters({redisUrl: 'redis://user:password@host:port/database'});

   // Specifying collection name instead of defaulting to counters collection.
   // The collection name is the hash name to store the counters under
   var counters = require('redis-counter').createCounters({
     redisUrl: '...',
     collectionName: 'yoyo'
   });

API

   counters('requests').increment(); // increments by one, throws away the result
   counters('requests').increment(5); // increments by 5 throws away the result
   counters('requests').increment(5, function (err, currentCount) {
     // currentCount is the updated count.
   });

   // increment by one and get the new value
   counters('requests').increment(function (err, currentCount) {
     // currentCount is the updated count.
   });

   counters('requests').decrement(); // same options with decrement
   counters('requests').get(function (err, val) {
     // fetch the current value
   });
   counters('requests').set(52); // update the value.

   // if you only want to generate unique ids (for URL shortening for example),
   // a much faster method is getNextUniqueId.  It only hits the database every 100
   // calls.  Ofcourse, this has the side effect that the sequence is not strictly
   // monotonically increasing in order and there could be gaps when there are crashes
   // but uniqueness is guaranteed and the ordering will be almost monotonic.  This is
   // suitable for a lot of cases where you just want a small unique ID.

   function getShortUrl(longUrl, done) {
     counters('requests').getNextUniqueId(function (err, uniqueId) {
       if (err) return done(err);
       var shortUrl = '/' + uniqueId.toString(36);
       db.shortUrls.insert(
         {shortUrl: '/' + uniqueId.toString(36), longUrl: longUrl},
         function (err) { return done(err, !err && shortUrl); }
       );
     });
   });

redis-counter's People

Contributors

like-falling-leaves avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.