node-cache
From: https://www.npmjs.com/package/node-cache
Simple and fast NodeJS internal caching.
A simple caching module that has set, get and delete methods and works a little bit like memcached. Keys can have a timeout (ttl) after which they expire and are deleted from the cache. All keys are stored in a single object so the practical limit is at around 1m keys.
Since 4.1.0: Key-validation: The keys can be given as either string or number, but are casted to a string internally anyway. All other types will either throw an error or call the callback with an error.
Install
Or just require the node_cache.js file to get the superclass
Examples:
Initialize (INIT):
Options
stdTTL: (default:0) the standard ttl as number in seconds for every generated cache element.0= unlimitedcheckperiod: (default:600) The period in seconds, as a number, used for the automatic delete check interval.0= no periodic check.errorOnMissing: (default:false) en/disable throwing or passing an error to the callback if attempting to.geta missing or expired value.useClones: (default:true) en/disable cloning of variables. Iftrueyou'll get a copy of the cached variable. Iffalseyou'll save and get just the reference. Note:trueis recommended, because it'll behave like a server-based caching. You should setfalseif you want to save mutable objects or other complex types with mutability involved and wanted. Here's a simple code exmaple showing the different behaviordeleteOnExpire: (default:true) whether variables will be deleted automatically when they expire. Iftruethe variable will be deleted. Iffalsethe variable will remain. You are encouraged to handle the variable upon the eventexpiredby yourself.
Store a key (SET):
myCache.set( key, val, [ ttl ], [callback] )
Sets a key value pair. It is possible to define a ttl (in seconds). Returns true on success.
Note: If the key expires based on it's
ttlit will be deleted entirely from the internal data object.
Since 1.0.0: Callback is now optional. You can also use synchronous syntax.
Retrieve a key (GET):
myCache.get( key, [callback] )
Gets a saved value from the cache. Returns a undefined if not found or expired. If the value was found it returns an object with the key value pair.
Since 1.0.0: Callback is now optional. You can also use synchronous syntax.
Since 2.0.0:
The return format changed to a simple value and a ENOTFOUND error if not found ( as callback( err ) or on sync call as result instance of Error ).
Since 2.1.0:
The return format changed to a simple value, but a due to discussion in #11 a miss shouldn't return an error. So after 2.1.0 a miss returns undefined.
Since 3.1.0 errorOnMissing option added
Get multiple keys (MGET):
myCache.mget( [ key1, key2, ... ,keyn ], [callback] )
Gets multiple saved values from the cache. Returns an empty object {} if not found or expired. If the value was found it returns an object with the key value pair.
Since 1.0.0: Callback is now optional. You can also use synchronous syntax.
Since 2.0.0:
The method for mget changed from .get( [ "a", "b" ] ) to .mget( [ "a", "b" ] )
Delete a key (DEL):
myCache.del( key, [callback] )
Delete a key. Returns the number of deleted entries. A delete will never fail.
Since 1.0.0: Callback is now optional. You can also use synchronous syntax.
Delete multiple keys (MDEL):
myCache.del( [ key1, key2, ... ,keyn ], [callback] )
Delete multiple keys. Returns the number of deleted entries. A delete will never fail.
Since 1.0.0: Callback is now optional. You can also use synchronous syntax.
Change TTL (TTL):
myCache.ttl( key, ttl, [callback] )
Redefine the ttl of a key. Returns true if the key has been found and changed. Otherwise returns false. If the ttl-argument isn't passed the default-TTL will be used.
The key will be deleted when passing in a ttl < 0.
Get TTL (getTTL):
myCache.getTtl( key, [callback] )
Receive the ttl of a key. You will get:
undefinedif the key does not exist0if this key has no ttl- a timestamp in ms until the key expires
List keys (KEYS)
myCache.keys( [callback] )
Returns an array of all existing keys.
Statistics (STATS):
myCache.getStats()
Returns the statistics.
Flush all data (FLUSH):
myCache.flushAll()
Flush all data.
Close the cache:
myCache.close()
This will clear the interval timeout which is set on check period option.
Events
set
Fired when a key has been added or changed. You will get the key and the value as callback argument.
del
Fired when a key has been removed manually or due to expiry. You will get the key and the deleted value as callback arguments.
expired
Fired when a key expires. You will get the key and value as callback argument.
flush
Fired when the cache has been flushed.
node-cache的更多相关文章
- LeetCode: LRU Cache [146]
[题目] Design and implement a data structure for Least Recently Used (LRU) cache. It should support th ...
- Curator Recipes(Cache&Counter)
Cache 路径缓存(Path Cache) 监视一个ZNode,当子节点增加.更新.删除改变状态时,路径缓存会在本地保存当前子节点及其数据和状态. public PathChildrenCache( ...
- [Swift]LeetCode146. LRU缓存机制 | LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- 深入浅出Cache
章节 ① 什么是Cache? Cache的目标? ② Caching住哪些内容? ③ 我们想要的Cache产品 ④ Cache使用方式 ⑤ 对于总体系统的提高 ⑥ 关于Sharding ⑦ Cache ...
- LeetCode题解: LRU Cache 缓存设计
LeetCode题解: LRU Cache 缓存设计 2014年12月10日 08:54:16 邴越 阅读数 1101更多 分类专栏: LeetCode 版权声明:本文为博主原创文章,遵循CC 4 ...
- nodejs安装和环境部署
windows 下: 1. 下载windows平台nodejs环境安装包,百度一下nodejs官网,找到DOWNLOADS点击,找到Windows Installer 如果为64位电脑可以选择64位版 ...
- Apache Curator: Zookeeper客户端
Apache Curator Framework url: http://curator.apache.org/curator-framework/ The Curator Framework is ...
- 【ToolKit】轻量级JS库
优点: 丢弃了一些不常用的方法(jQuery.fn):slideUp.fadeIn.animate等: 新增获取子节点的方法(ToolKit.fn):firstChild,lastChild等: 新增 ...
- vue-cli安装失败问题
1.尝试 管理员权限 安装,看是否能解决问题 2.仍未解决问题, 系统变量增加: C:\Program Files\nodejs\node cache\node_modules\vue-cli\bi ...
- peersim中BT网络核心代码解析
首先大概介绍BT网络运行的整体流程: 开始阶段,一个节点加入到网络中,并向tracker节点发送信息,tracker返回若干个邻居的列表 得到列表后,向每个邻居发送bitfiled信息,来获取他们的文 ...
随机推荐
- AtCoder Grand Contest 002 (AGC002) F - Leftmost Ball 动态规划 排列组合
原文链接https://www.cnblogs.com/zhouzhendong/p/AGC002F.html 题目传送门 - AGC002F 题意 给定 $n,k$ ,表示有 $n\times k$ ...
- BZOJ2084 [Poi2010]Antisymmetry Manachar
题目传送门 - BZOJ2084 题解 对于一个0我们把它看作01,1看作10,然后只要原串中的某个子串可以通过这两个变换成为回文串就可以满足条件了. 对于转换过的串,Manachar随便弄几下就可以 ...
- 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。
给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股票. 示例 ...
- day11 装饰器---函数的使用方法
这个是一个难点,以后面试会经常出现的,要搞懂! 装饰器升级版,进阶内容1: def outer(flag): def wrapper(func): def inner(*args,**kwargs): ...
- 开启vue源码的解析攻略---认识flow
javascript 是动态类型的代码,有很多的写法很不容易报错,想引入静态类型检查的flow,之前做项目的时候用的typascript的,看的大概的写法和 typescript 类似,因为规范避免了 ...
- unity打aar包工具
需求: unity将游戏导出android工程之后,打成aar包的工具 第一种: 高版本的unity导出的android工程是android studio版的,那么打成aar的流程就是 1.build ...
- Linux学习笔记 11
移除文件 #rm -i file 有信息确认的文件删除 #rm file1 file2 有信息确认的文件删除 #rm -rf file 强制删除file文件
- linux 学习笔记 执行脚本篇章
r w x 读 写 执行 4 2 1 如果要启动tomcat服务器 即执行startup.sh 脚本文件 1) #chmod 444 startup.sh <----增加文本读权限 即 ...
- c/c++保存日志程序模板
//输出日志 int PrintRunInfo(char *fmt, ...) { FILE* fp; fp = fopen("cgi_log.txt","a+&qu ...
- [MySQL] MySQL联表查询的执行顺序优化查询
SELECT t4.orgName, t3.projectName, t3.Partner, t1.type, COUNT(DISTINCT t1.imei) AS count FROM `t_tem ...