使用 nodejs +node_redis操作redis
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 var url = require('url'); 2 var redis = require('redis'); 3 4 var redisUrl = 'redis://10.10.131.126:6388'; 5 var redisUrlConfig = url.parse(redisUrl); 6 7 console.log(redisUrlConfig); 8 9 var redisClient = redis.createClient(redisUrlConfig.port, redisUrlConfig.hostname); 10 11 redisClient.on('error', function(err) { 12 console.log('Error while connecting redis %j', err); 13 }).on('connect', function() { 14 console.log('Connect to redis'); 15 }).on('ready', function() { 16 console.log('Redis Client Ready'); 17 }) 18 19 redisClient.select(10); 20 21 for (var i = 0; i < 10; i++) { 22 redisClient.set('testkey', 'testvalue', function(){ 23 console.log('set ok'); 24 }) 25 }; 26 27 28 29 redisClient.get('testkey', function(err,reply){ 30 console.log('result is ' + reply); 31 })