Created
December 23, 2012 09:11
-
-
Save luin/4362684 to your computer and use it in GitHub Desktop.
Redis Scheme Specification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
redis_scheme = require 'redis_scheme' | |
# HASH: users:<id>(name, password, books, __blog.id) | |
# HASH: __blog:<id>(url) | |
# LIST: __blog:<id>:subscribers | |
# ZSET: users:<id>:books | |
redis_scheme.define_model('user', { | |
name: redis_scheme.type.string, | |
password: redis_scheme.type.string, | |
books: redis_scheme.foreign_key(redis_scheme.model('book'), 'readers'), | |
blog: redis_scheme.type.hash({ | |
url: redis_scheme.type.string, | |
subscribers: redis_scheme.type.list(redis_scheme.model('user')) | |
}) | |
}) | |
redis_scheme.define_model('book', { | |
title: redis_scheme.type.string, | |
readers: redis_scheme.foreign_key(redis_scheme.model('user'), 'books'), | |
isbn: redis_scheme.type.string | |
}) | |
redis_scheme.connect('127.0.0.1', '6379') | |
# Fetch a existed user. | |
bob_userid = '23125' | |
bob = redis_scheme.fetch_model('user', bob_userid) | |
for book in bob.books do | |
p book.title | |
p bob.blog.subscribers[0..5] | |
# Create a new user. | |
new_user = new redis_scheme.model('user', redis_scheme.id.autoincrement) | |
new_user.name = 'jeff' | |
new_user.password = '123' | |
bible = redis_scheme.fetch_model('book', 'bible') | |
new_user.books.push(bible) | |
new_user.blog.url = 'http://a.cn/' | |
new_user.blog.subscribers.push(bob) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment