Git Product home page Git Product logo

redis-model's Introduction

redis-model

A simple object wrapper over the hash type in redis.

Usage

Define your own models, note the second argument in the super call is used to define the namespace of your model:

redis = (require 'redis').createClient()
RedisModel = require 'redis-model'

class Blog extends RedisModel
	constructor: ->
		super redis, 'Blog'
		@addFields 'url', 'date', 'content'

Create instances with automatic keys, or use your own:

Blog.newItem (err, blogInstance) ->
	# Do stuff with blogInstance with automatic key

Blog.withKey 'akey', (err, blogInstance) ->
	# Do stuff with blogInstance with set key

New: Sort command is available on your model to quickly get a list of your items from a list of keys

Blog.sort 'externalSetOfKeys', { alpha: true, skip: 10, take: 5 }, (err, blogs) ->
	# Sort by alphabetical order on the keys, skip 10 records and take 5 records
	blogs[0] == { key: 'key', url: 'url', date: 'date', content: 'content' }
	
Blog.sort 'externalSetOfKeys', { byField: 'url', alpha:true, asc: false }, (err, blogs) ->
	# Sort the keys by the url field of your blogs, in alphabetical descending order
	
Blog.sort 'externalSetOfKeys', { by: 'nosort', getKey: false }, (err, blogs) ->
	# Don't worry about sorting, and don't worry about pulling back the key

Get values simply:

blogInstance.url (err, url) ->
	console.log url
	
blogInstance.getAll (err, blog) ->
	console.log blog.url
	console.log blog.date
	console.log blog.content

You can save values one at a time, or lock for full advantage

# Save a value straight away
blogInstance.url 'http://www.google.com', (err) ->
	# Value is saved at this point
	
# Lock and save multiple values at once
blogInstance.lock()
blogInstance.url 'url'
blogInstance.date 'date'
blogInstance.content 'content'
blogInstance.unlock (err) ->
	# All values saved in one call - this is much faster than saving each individually
	
blogInstance.setAll { url: 'url', date: 'date', content: 'content' }, (err) ->
	# All values are saved in one call here too

NEW: Has a multi interface that lets you save your data at the same time as other keys. It adds two methods to the multi interface, setAll and setField.

# setField example
blogInstance.multi()
	.sadd('AllBogs', blogInstance.key)
	.setField('url', 'www.newUrl.com')
	.exec cb
	
# setAll example
blogInstance.multi()
	.sadd('AllBogs', blogInstance.key)
	.setAll({ url: 'www.url.com', date: 'date', content: 'content' })
	.exec cb

Installation

$ npm install redis-model

Benchmark test results

1000 normal sequential requests done in 79ms
✔ Pure redis (read)
1000 redis-model sequential requests done in 68ms
✔ Redis-Model (read)
500 redis-model sequential requests for two fields done in 46ms
✔ Redis-Model with locking (read)
1000 normal sequential requests done in 62ms
✔ Pure redis (save)
500 redis-model sequential requests for two fields done in 38ms
✔ Redis-Model with locking (save)

License

©2012 Felix Jorkowski and available under the MIT license:

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.

redis-model's People

Contributors

ajorkowski avatar

Watchers

 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.