在 Node.js 中读写 Redis 的数据(失效时间和TTL) 【已翻译100%】
来源:互联网 发布:最花钱的游戏知乎 编辑:程序博客网 时间:2024/06/06 11:40
英文原文:Working with Node.js and Redis (Expire and TTL)
在开始本文之前请确保安装好 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
1 0
- 在 Node.js 中读写 Redis 的数据(失效时间和TTL) 【已翻译100%】
- 在Node.js应用中读写Redis数据库的简单方法
- 翻译:如何在Node.js和Express中使用MySql
- 在Node.js中读写大文件
- 在 Node.js 应用中集成 Redis
- node.js中时间戳和日期之间的转换
- cocos2dx 3.x 中 Lua socket 和 node.js 利用scoket互相通信读写二进制数据
- redis 生存时间 expire ttl
- Node.js中使用redis数据库的正确姿势
- Node.js中connect-flash文档翻译
- 【实战】基于Nginx、Node.js和Redis的Docker工作流
- 关于mongoDB 失效数据 ttl ensureIndex
- Button提交数据后失效的JS(数据提交中……)
- Node.js在MySQL中存储数据 & 创建连接池
- 解决在IE中获取数据的缓存问题,运行环境为node.js
- 在基于Node.js的IoT框架Zetta中实现可变间隔发送数据
- Node.js + MongoDB + AngularJS - 5 在Node.js中处理数据I0-1
- Node.js + MongoDB + AngularJS - 5 在Node.js中处理数据I0-2
- Permutation Sequence
- 抽奖程序
- FutureTask 源代码深刻解读
- 拼接模型坐标系的理解(五)
- UIView层次切换(bringSubviewToFront,sendSubviewToBack)的使用
- 在 Node.js 中读写 Redis 的数据(失效时间和TTL) 【已翻译100%】
- Java 内存溢出(java.lang.OutOfMemoryError)的常见情况和处理方式总结
- 设计模式(一) 简单工厂模式
- C# 操控滚动条
- Andorid 布局layout_margin和padding分析
- 【干货分享】流程DEMO-事务呈批表
- c++ 函数指针
- Python yield解析
- 编写更好的 Java 单元测试的 7 个技巧