内容简介:我们先来看看如何存储和获取键/值对。哈希表有点类似ES6中的Map。Redis链表类似JS数组,lpush向链表中添加值,lrange获取参数start和end范围内的链表元素, 参数end为-1,表明到链表中最后一个元素。 注意:随着链表长度的增长,数据获取也会逐渐变慢(大O表示法中的O(n))
我们先来看看如何存储和获取键/值对。
var redis = require('redis')
var client = redis.createClient(6379, '127.0.0.1')
client.on('error', function (err) {
console.log('Error ' + err);
});
// 1 键值对
client.set('color', 'red', redis.print);
client.get('color', function(err, value) {
if (err) throw err;
console.log('Got: ' + value)
client.quit();
})
复制代码
哈希表
哈希表有点类似ES6中的Map。
client.hmset('kitty', {
'age': '2-year-old',
'sex': 'male'
}, redis.print);
client.hget('kitty', 'age', function(err, value) {
if (err) throw err;
console.log('kitty is ' + value);
});
client.hkeys('kitty', function(err, keys) {
if (err) throw err;
keys.forEach(function(key, i) {
console.log(key, i);
});
client.quit();
});
复制代码
链表
Redis链表类似JS数组,lpush向链表中添加值,lrange获取参数start和end范围内的链表元素, 参数end为-1,表明到链表中最后一个元素。 注意:随着链表长度的增长,数据获取也会逐渐变慢(大O表示法中的O(n))
client.lpush('tasks', 'Paint the house red.', redis.print);
client.lpush('tasks', 'Paint the house green.', redis.print);
client.lrange('tasks', 0, -1, function(err, items) {
if (err) throw err;
items.forEach(function(item, i) {
console.log(' ' + item);
});
client.quit();
});
复制代码
集合
类似JS中的Set,集合中的元素必须是唯一的,其性能: 大O表示法中的O(1)
client.sadd('ip', '192.168.3.7', redis.print);
client.sadd('ip', '192.168.3.7', redis.print);
client.sadd('ip', '192.168.3.9', redis.print);
client.smembers('ip', function(err, members) {
if (err) throw err;
console.log(members);
client.quit();
});
复制代码
信道
Redis超越了数据存储的传统职责,它还提供了信道,信道是数据传递机制,提供了发布/预定功能。
var redis = require('redis')
var clientA = redis.createClient(6379, '127.0.0.1')
var clientB = redis.createClient(6379, '127.0.0.1')
clientA.on('message', function(channel, message) {
console.log('Client A got message from channel %s: %s', channel, message);
});
clientA.on('subscribe', function(channel, count) {
clientB.publish('main_chat_room', 'Hello world!');
});
clientA.subscribe('main_chat_room');
复制代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Intersectional Internet
Safiya Umoja Noble、Brendesha M. Tynes / Peter Lang Publishing / 2016
From race, sex, class, and culture, the multidisciplinary field of Internet studies needs theoretical and methodological approaches that allow us to question the organization of social relations that ......一起来看看 《The Intersectional Internet》 这本书的介绍吧!