nodejs 操作 redis

// node.js访问redis前提条件,安装redis模块 
// -- npm install redis
//导入redis模块
var redis = require('redis');
//新建连接 redis.createClient(端口,ip地址0); 端口参数可以是数值也可以是字符串
var client = redis.createClient(6379, 'ip地址');
//如果有密码,请认证
client.auth("密码");

//如果想要选择其他库,例如3
/**
 * 说明:
 *      1.Redis不支持自定义数据库的名字;
 *      2.Redis也不支持为每个数据库设置不同的访问密码;
 *      3.多个数据库之间并不是完全隔离的,比如FLUSHALL命令可以清空全部数据库的数据;
 *      4.数据库更像是一种命名空间,而不适宜存储不同应用程序的数据,不同的应用应该使用不同的Redis实例存储数据;
 *      5.一个空Redis实例占用的内在只有1M左右,所以不用担心多个Redis实例会额外占用很多内存。
 */
client.select(3,function(error){
    if(error) {console.log(error)};
    console.log('已切换到数据库3');
})
//一下操作都是在数据库3下执行
// client.set('测试',"在数据库3中插入数据",function(error,info){
//     if(error) return error;
//     if(info === 'OK'){
//         console.log('插入成功')
//     }
// })
// client.get('测试',function(error,data){
//     if(error) return error;
//     console.log(data);
// })
// client.get('http://spgz.com/162304',function(error,data){
//     if(error) return error;
//     console.log(data);
// })

//切换为数据库1
client.select(1,function(error,data){
    if(error) return error;
    console.log('已切换到数据库1');
})


//-------------------------String 操作--------------------------//
// set 设置存储在给定键中的值 返回值'OK'
// client.set("string key", 20,function(error,info){
//     if(error){
//         console.log(error);
//     }
//     //"=="相等,但是不严格(字符串'456'和数字456相等);"===":严格相等 
//     if(info == 'OK'){
//         console.log('在数据库1中set成功')
//     }
// });
// get 获取存储在给定键中的值 返回值 value或空
// client.get('string key',function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })
// del 删除存储在给定键中的值(任意类型) 返回值 0或1 0:库中没有要删除的数据;1:删除成功
// client.del('list',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// });
// incrby 将键存储的值加上整数,返回值 value最后的值,key中的值需要为整数。不然会报错
// client.incrby('string key',10,function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info)
//     client.get('string key',function(error,data){
//         console.log(data);
//     });
// })
// decrby 将键存储的值减去整数

// incrbyfloat 将键存储的值加上浮点数

// append  将值value追加到给定键当前存储值的末尾 返回值 value最后的值 ,这个操作是单纯的拼接,例如整数30 拼 80 等于 3080
// client.append('string key',80,function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

// getrange 获取指定键的index范围内的所有字符组成的子串 返回值 指定范围的子串
// client.getrange('string key',0,2,function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })

// setrange 将指定键值从指定偏移量开始的子串设为指定值 ,如果指定的key不存在,其效果与set类似,整数会转为字符串
// client.setrange('string key1',0,0,function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

//--------------------------list 操作---------------------------//
// rpush 将给定值推入列表的右端 返回值 当前列表长度 
// client.rpush("list",['test1','test2','test3','test4','test5'],function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

// lrange 获取列表在给定范围上的所有值 返回值 当前所有值 如果超出范围则返回所有,不报错。
// client.lrange('list',0,1000,function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })

// lindex 获取列表在给定位置上的单个元素

// lpop 从列表左端弹出一个值,并返回被弹出的值

// rpop 从列表右端弹出一个值,并返回被弹出的值

// 将列表按指定的index范围裁减, 返回值 OK ,只保留指定范围内的数据
// client.ltrim('list',10,15,function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// });

//---------------------------set 操作---------------------------//
// 先清空集合,方便测试
// client.del('setTest',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log('清空集合');
// })

// sadd 将给定元素添加到集合 返回值 插入元素数量
// client.sadd('setTest',['setTest1','setTest2','setTest3','setTest4'],function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// });

// smembers 返回集合中包含的所有元素 返回值 array(无序),插入和读出的顺序不一定一致
// client.smembers('setTest',function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// });

// sismenber 检查给定的元素是否存在于集合中 返回值 0/1 0:不存在,1:存在
// client.sismember('setTest','setTest3',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

// srem 如果给定的元素在集合中,则移除此元素 返回值 0/1 0:没有匹配上,1:匹配上并删除
// client.srem('setTest','setTest37',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

// scad 返回集合包含的元素的数量 返回值 元素的数量
// client.scard('setTest',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

// spop 随机地移除集合中的一个元素,并返回此元素 返回值 被删除的元素
// client.spop('setTest',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

// smove 将指定成员 member 元素从 source 集合移动到 destination 集合 返回值 0/1 0:元素不是 source 集合的成员,并且没有任何操作对 destination 集合执行;1:如果成员元素被成功移除,返回 1 
// client.smove('setTest','setTest01','setTest4',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

// sdiff 返回那些存在于第一个集合,但不存在于其他集合的元素(差集)
// client.sdiff('setTest','setTest01',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info)
// })

// sdiffstore 将sdiff操作的结果存储到指定的键中 第一个参数是存储到的指定集合
// client.sdiffstore('setTest02','setTest','setTest01',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

// sinter 返回那些同时存在于所有集合中的元素(交集)
// client.sinter('setTest','setTest01',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

// sinterstore 将sinter操作的结果存储到指定的键中
// client.sinterstore('setTest03','setTest','setTest01',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

// sunion 返回那些至少存在于一个集合中的元素(并集)
// client.sunion('setTest','setTest01',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

// sunionstore 将sunion操作的结果存储到指定的键中
// client.sunionstore('setTest04','setTest','setTest01',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })


//---------------------------hash 操作---------------------------//
// hset 在散列里面关联起给定的键值对 1(新增)/0(更新) 返回结果为0说明已存在相同 key 的值
// 以下两种写法效果想同
// client.hset("hash key", "hashtest 3", "some value", redis.print);
// client.hset(["hash key", "hashtest 2", "some other value"], redis.print);

// hget 获取指定散列键的值,需要指定到里面一层,不然报错
// client.hget("hash key","hashtest 3",function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })

// hgetall 获取散列包含的键值对 返回值 hash键里面的所有键值对,格式为json;
// client.hgetall("hash key",function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })

// hdel 如果给定键 存在于散列里面,则移除这个键 返回值 0/1 0:没有存在 1:已移除
// client.hdel("hash key","hashtest 3",function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })

// hmset 为散列里面的一个或多个键设置值 返回值 不报错就返回OK
// 以下两条命令效果相同
// client.hmset("hash key",{"hashtest 6":"hashtest 4","hashtest 7":"hashtest 5"},function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })
// client.hmset("hash key","hashtest 6","hashtest 4","hashtest 7","hashtest 5",function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })

// hmget 从散列里面获取一个或多个键的值 返回值 对应结果的列表,格式list
// client.hmget("hash key",["hashtest 1","hashtest 2"],function(error,data){
//     if(error){
//         console.log(error)
//     }
//     console.log(data);
// })

// hlen 返回散列包含的键值对数量 返回值 键值对数量
// client.hlen("hash key",function(error,data){
//     if(error){
//         console.log(error)
//     }
//     console.log(data);
// })

// hexists 检查给定键是否在散列中 返回值 0/1 0:不存在 1:存在
// client.hexists('hash key','hashtest 1',function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })

// hkeys 获取散列包含的所有键 返回值 散列中所有键,格式list
// client.hkeys('hash key',function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })

// hvals 获取散列包含的所有值 返回值 散列中所有值,格式list
// client.hvals('hash key',function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })

// hincrby 为哈希表中的字段值加上指定增量值 返回值 运算后的值 传入的值要为int ,指定的键的值也要为int,浮点数也不可以,如果键不存在,则新增该键并将其值初始化为0后加上指定增量值
// client.hincrby('hash key',1,123,function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

// hincrbyfloat 将存储的键值以指定浮点数增加 返回值 运算后的值 可以加整数
// client.hincrbyfloat('hash key',1,1,function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })

//---------------------------zset 操作---------------------------//
// zadd 有序集合插入,序号可以一致,如果值一致,更新
// client.zadd('zsetTest',4,'test5',6,'test6',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

// zrange 根据元素在有序排列中的位置,从中取出元素
// client.zrange('zsetTest',0,2,function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })

// zrangebyscore 获取有序集合在给定分值范围内的所有元素,下面语句获取4 <=score <= 10 的数据,返回值为list
// 有序集合结构
// zsetKey
    //score : munber
// client.zrangebyscore('zsetTest',4,10,function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data);
// })

// zrem 如果给定成员存在于有序集合,则移除 返回值 移除的个数 
// client.zrem('zsetTest','test1','test2', 'test3',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

// zcard 获取一个有序集合中的成员数量 有序集的元素个数 返回值 成员数量
// client.zcard('zsetTest',function(error,data){
//     if(error){
//         console.log(error);
//     }
//     console.log(data)
// })

//---------------------------keys 操作---------------------------//
// del 删除一个(或多个)keys 返回值 0/1 0:没有该键不存在删除;1:已删除
// client.del('zsetTest',function(error,info){
//     if(error){
//         console.log(error); 
//     }
//     console.log(info);
// })

// exists 查询一个key是否存在 返回值 0/1 0:不存在 1:存在
// client.exists('zsetTest',function(error,info){
//     if(error){
//         console.log(error); 
//     }
//     console.log(info);
// })

// expire 设置一个key的过期的秒数 返回值 0/1 0:不存在 1:已设置 
// client.expire('setTest02',300,function(error,info){
//     if(error){
//         console.log(error); 
//     }
//     console.log(info);
// })

// pexpire 设置一个key的过期的毫秒数 返回值 0/1 0:不存在 1:已设置

// expireat 设置一个UNIX时间戳的过期时间 返回值 0/1 0:不存在 1:已设置

// pexpireat 设置一个UNIX时间戳的过期时间(毫秒) 返回值 0/1 0:不存在 1:已设置

// persist 移除key的过期时间 返回值 0/1 0:不存在 1:已设置
// client.persist('setTest02',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

// sort (集合,列表,有序集合)将指定键中的值排序输出,如果要排序字符串,需要加入参数alpha
// client.sort('setTest02','alpha',function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

// flushdb 清空当前数据库
// client.flushdb(function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

// flushall 清空全部数据库
// client.flushall(function(error,info){
//     if(error){
//         console.log(error);
//     }
//     console.log(info);
// })

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值