docker.compose.yml
version : '3.1'
services : redis : image : redisports : - 6379: 6379
命令行:docker-compose up
查看: docker ps
进入redis-cli,输入以下 docker exec -it 7dc0a redis-cli -h localhost -p 6379
操作Redis数据
设置 name=marron
set name marron
获取name
get name
使用node连接redis,并进行数据的存储
若docker未开启redis,会提示如下 连接redis
const redis = require ( 'redis' ) ;
const client = redis. createClient ( 6379 , '127.0.0.1' ) ;
client. on ( 'error' , async ( err) => { console. log ( `[Error]: ${ err} ` ) ;
} )
client. set ( 'name' , 'marron' , redis. print) ;
client. get ( 'name' , async ( err, value) => { if ( err) throw err; console. log ( 'Name: ' + value) ;
} ) ;
client. hmset ( 'marron' , { 'item' : 'koaDemo' , 'chapter' : 'redisDemo'
} ) ;
client. hgetall ( 'marron' , async ( err, obj) => { console. log ( obj) ;
} ) ;
client. hkeys ( 'marron' , async ( err, replies) => { replies. forEach ( ( reply, i) => { console. log ( i + ":" + reply) ; } ) ;
} )
client. lpush ( 'marron1' , 'koa' , redis. print)
client. lpush ( 'marron1' , 'redisDemo' , redis. print) ;
client. lrange ( 'marron1' , 0 , - 1 , async ( err, items) => { if ( err) throw err; items. forEach ( async ( item, i) => { console. log ( item) ; } )
} )