在Node.js应用中读写Redis数据库的简单方法
来源:互联网 发布:嵌入式软件著作权吗 编辑:程序博客网 时间:2024/06/06 09:17
在开始本文之前请确保安装好 Redis 和 Node.js 以及 Node.js 的 Redis 扩展 —— node_redis
首先创建一个新文件夹并新建文本文件 app.js 文件内容如下:
var redis = require("redis") , client = redis.createClient(); client.on("error", function (err) { console.log("Error " + err);}); client.on("connect", runSample); function runSample() { // Set a value client.set("string key", "Hello World", function (err, reply) { console.log(reply.toString()); }); // Get a value client.get("string key", function (err, reply) { console.log(reply.toString()); });}
当连接到 Redis 后会调用 runSample 函数并设置一个值,紧接着便读出该值,运行的结果如下:
OKHello World
我们也可以使用 EXPIRE 命令来设置对象的失效时间,代码如下:
var redis = require('redis') , client = redis.createClient(); client.on('error', function (err) { console.log('Error ' + err);}); client.on('connect', runSample); function runSample() { // Set a value with an expiration client.set('string key', 'Hello World', redis.print); // Expire in 3 seconds client.expire('string key', 3); // This timer is only to demo the TTL // Runs every second until the timeout // occurs on the value var myTimer = setInterval(function() { client.get('string key', function (err, reply) { if(reply) { console.log('I live: ' + reply.toString()); } else { clearTimeout(myTimer); console.log('I expired'); client.quit(); } }); }, 1000);}
注意: 上述使用的定时器只是为了演示 EXPIRE 命令,你必须在 Node.js 项目中谨慎使用定时器。
运行上述程序的输出结果是:
Reply: OKI live: Hello WorldI live: Hello WorldI live: Hello WorldI expired
接下来我们检查一个值在失效之前存留了多长时间:
var redis = require('redis') , client = redis.createClient(); client.on('error', function (err) { console.log('Error ' + err);}); client.on('connect', runSample); function runSample() { // Set a value client.set('string key', 'Hello World', redis.print); // Expire in 3 seconds client.expire('string key', 3); // This timer is only to demo the TTL // Runs every second until the timeout // occurs on the value var myTimer = setInterval(function() { client.get('string key', function (err, reply) { if(reply) { console.log('I live: ' + reply.toString()); client.ttl('string key', writeTTL); } else { clearTimeout(myTimer); console.log('I expired'); client.quit(); } }); }, 1000);} function writeTTL(err, data) { console.log('I live for this long yet: ' + data);}
运行结果:
Reply: OKI live: Hello WorldI live for this long yet: 2I live: Hello WorldI live for this long yet: 1I live: Hello WorldI live for this long yet: 0I expired
0 0
- 在Node.js应用中读写Redis数据库的简单方法
- 在 Node.js 应用中集成 Redis
- node.js应用Redis数据库
- 在 Node.js 中读写 Redis 的数据(失效时间和TTL) 【已翻译100%】
- Node.js中使用redis数据库的正确姿势
- 在Node.js中读写大文件
- Node.js操作Redis的简单示例
- Node.js中连接PostgreSql数据库的方法
- node.js应用Redis初步
- node.js应用Redis初步
- node.js的安装和简单应用
- node.js简单操作redis
- node.js+redis之一:redis简单介绍
- Node.js + Redis Sorted Set 实现简单的任务队列
- 基于Redis的简单消息队列模块(Node.js)
- node.js中express的应用
- Node.js中async的简单使用
- node.js中mysql的简单使用
- ACES学院色彩编码在影片中的应用
- CMD一键获取 所有连接过的WIFI密码
- Python:如何将文本里的字典里的unicode字符转换成中文
- iscroll.js listloading.js是一个移动端的上拉、下拉加载更多的组件
- Java中的位运算
- 在Node.js应用中读写Redis数据库的简单方法
- Qt之属性系统详解
- SDK、JDK、JRE的区别
- 浏览器拦截跨域请求处理方式
- CSS鼠标指针cursor样式
- RGB色彩模式
- 草稿待整理
- Java编程简单应用———1、HelloWorld(HelloWorld的来源);2、输出个人信息3、输出特殊图案
- 忘记svn密码怎么办